diff --git a/packages/sdk-apple/src/__tests__/runner.test.ts b/packages/sdk-apple/src/__tests__/runner.test.ts index 56e5ac11a..ec8c99c40 100644 --- a/packages/sdk-apple/src/__tests__/runner.test.ts +++ b/packages/sdk-apple/src/__tests__/runner.test.ts @@ -1,30 +1,44 @@ -import { inquirerPrompt, getContext, createRnvContext } from '@rnv/core'; +import { inquirerPrompt, getContext, createRnvContext, logSuccess } from '@rnv/core'; import type { PromptParams } from '@rnv/core'; import { getIosDeviceToRunOn } from '../runner'; import { getAppleDevices } from '../deviceManager'; -const simctlSimJson = [ +const simJson = [ { - lastBootedAt: '2023-10-04T15:50:14Z', - udid: 'A3CE2617-4071-4759-BC87-2F687FEA50A7', - isAvailable: true, - deviceTypeIdentifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-SE-3rd-generation', - state: 'Shutdown', + udid: '131BD0D2-8F85-4C34-83BB-C0A58E1B41B4', name: 'iPhone SE (3rd generation)', + icon: 'Phone 📱', + version: '17.0 (21A328)', + modelName: 'iPhone SE (3rd generation)', + isDevice: false, }, { - lastBootedAt: '2023-10-06T09:46:07Z', - udid: '0BEDB188-352D-4215-8471-E9E27C670486', - isAvailable: true, - deviceTypeIdentifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-14', - state: 'Shutdown', - name: 'iPhone 14', + udid: 'F70567A3-1F90-4EA8-B788-B5C6926CEFAF', + name: 'iPhone 15', + icon: 'Phone 📱', + version: '17.0 (21A328)', + modelName: 'iPhone 15', + isDevice: false, + }, +]; +const devicesJson = [ + { + udid: 'ABF470AF-2538-4047-94A8-D72E22EB15BF', + name: 'iPhone 15', + icon: 'Phone 📱', + version: '17.0 (21A328)', + modelName: 'iPhone 15', + isDevice: true, }, ]; jest.mock('@rnv/core'); jest.mock('../deviceManager'); - +jest.mock('chalk', () => ({ + bold: { + white: jest.fn((str) => str), + }, +})); beforeEach(() => { createRnvContext(); }); @@ -39,7 +53,7 @@ describe('getIosDeviceToRunOn', () => { const ctx = getContext(); ctx.runtime.isTargetTrue = true; ctx.platform = 'ios'; - jest.mocked(getAppleDevices).mockResolvedValueOnce(simctlSimJson); + jest.mocked(getAppleDevices).mockResolvedValueOnce(simJson); jest.mocked(inquirerPrompt).mockImplementation(async ({ type, name, choices }: PromptParams) => { if (type === 'confirm') { return { @@ -57,7 +71,7 @@ describe('getIosDeviceToRunOn', () => { const deviceArgs = await getIosDeviceToRunOn(ctx); //THEN expect(getAppleDevices).toHaveBeenCalledTimes(1); - expect(deviceArgs).toBe('--simulator iPhone\\ 14'); + expect(deviceArgs).toBe('--simulator iPhone\\ 15'); }); it('should return undefined if target is undefined and no devices available', async () => { @@ -76,7 +90,7 @@ describe('getIosDeviceToRunOn', () => { // GIVEN const ctx = getContext(); ctx.platform = 'ios'; - jest.mocked(getAppleDevices).mockResolvedValueOnce(simctlSimJson); + jest.mocked(getAppleDevices).mockResolvedValueOnce(simJson); jest.mocked(inquirerPrompt).mockImplementation(async ({ type, name, choices }: PromptParams) => { if (type === 'confirm') { return { @@ -110,12 +124,93 @@ describe('getIosDeviceToRunOn', () => { // GIVEN const ctx = getContext(); ctx.platform = 'ios'; + ctx.runtime.target = 'iPhone 15'; + jest.mocked(getAppleDevices).mockResolvedValueOnce(simJson); + // WHEN + const deviceArgs = await getIosDeviceToRunOn(ctx); + // THEN + expect(getAppleDevices).toHaveBeenCalledTimes(1); + expect(deviceArgs).toEqual('--simulator iPhone\\ 15'); + }); + it('should update the global default target when user opts in', async () => { + // GIVEN + const ctx = getContext(); + ctx.platform = 'ios'; + ctx.files.workspace.config = {}; ctx.runtime.target = 'iPhone 14'; - jest.mocked(getAppleDevices).mockResolvedValueOnce(simctlSimJson); + jest.mocked(getAppleDevices).mockResolvedValueOnce(simJson); + jest.mocked(inquirerPrompt).mockImplementation(async ({ type, name, choices }: PromptParams) => { + if (type === 'list' && choices?.includes('Update global default target for platform ios')) { + return { [name as string]: 'Update global default target for platform ios' }; + } + return { + [name as string]: (choices![0] as { name: string; value: any }).value || choices![0], + }; + }); + // WHEN + const deviceArgs = await getIosDeviceToRunOn(ctx); + // THEN + expect(getAppleDevices).toHaveBeenCalledTimes(1); + expect(deviceArgs).toBe('--simulator iPhone\\ SE\\ (3rd\\ generation)'); + expect(ctx.files.workspace.config?.defaultTargets?.ios).toBe('iPhone SE (3rd generation)'); + }); + it('should ask from active devices and return a device if --device and --target are true', async () => { + // GIVEN + const ctx = getContext(); + ctx.platform = 'ios'; + ctx.program.opts = jest.fn().mockReturnValue({ device: true, target: true }); + jest.mocked(getAppleDevices).mockResolvedValueOnce(devicesJson); + jest.mocked(inquirerPrompt).mockImplementation(async ({ name, choices }: PromptParams) => { + return { [name as string]: (choices![0] as { name: string; value: any }).value }; + }); // WHEN const deviceArgs = await getIosDeviceToRunOn(ctx); // THEN + + expect(getAppleDevices).toHaveBeenCalledTimes(1); + expect(deviceArgs).toBe('--udid ABF470AF-2538-4047-94A8-D72E22EB15BF'); + }); + it('should reject when -d and no devices are available', async () => { + // GIVEN + const ctx = getContext(); + ctx.platform = 'ios'; + ctx.program.opts = jest.fn().mockReturnValue({ device: true }); + jest.mocked(getAppleDevices).mockResolvedValueOnce([]); + // WHEN + // THEN + await expect(getIosDeviceToRunOn(ctx)).rejects.toMatch(`No ios devices connected!`); + expect(getAppleDevices).toHaveBeenCalledTimes(1); + }); + it('should ask for sims and active devices and return the selected sim or device if -t is specified', async () => { + // GIVEN + const ctx = getContext(); + ctx.platform = 'ios'; + ctx.program.opts = jest.fn().mockReturnValue({ target: true }); + jest.mocked(getAppleDevices).mockResolvedValueOnce([...devicesJson, ...simJson]); + jest.mocked(inquirerPrompt).mockImplementation(async ({ name, choices }: PromptParams) => { + return { + [name as string]: (choices![0] as { name: string; value: any }).value || choices![0], + }; + }); + // WHEN + const deviceArgs = await getIosDeviceToRunOn(ctx); + //THEN + expect(getAppleDevices).toHaveBeenCalledTimes(1); + expect(deviceArgs).toBe('--udid ABF470AF-2538-4047-94A8-D72E22EB15BF'); + }); + it('should return the active device if -t is the name of the active device', async () => { + // GIVEN + const ctx = getContext(); + ctx.platform = 'ios'; + ctx.program.opts = jest.fn().mockReturnValue({ target: 'iPhone 15' }); + jest.mocked(getAppleDevices).mockResolvedValueOnce([...devicesJson, ...simJson]); + // WHEN + const deviceArgs = await getIosDeviceToRunOn(ctx); + //THEN expect(getAppleDevices).toHaveBeenCalledTimes(1); - expect(deviceArgs).toEqual('--simulator iPhone\\ 14'); + expect(logSuccess).toHaveBeenCalledWith( + `Found device connected! Device name: iPhone 15 udid: ABF470AF-2538-4047-94A8-D72E22EB15BF` + ); + expect(deviceArgs).toBe('--udid ABF470AF-2538-4047-94A8-D72E22EB15BF'); }); });