Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/UTs_getIosDeviceToRunOn #1768

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 114 additions & 19 deletions packages/sdk-apple/src/__tests__/runner.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
Expand All @@ -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 {
Expand All @@ -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 () => {
Expand All @@ -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 {
Expand Down Expand Up @@ -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');
});
});
Loading