Skip to content

Commit

Permalink
add tests for getAndroidDeviceToRunOn
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaDiachenko committed Mar 12, 2024
1 parent 0997f9f commit 4a74d03
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
50 changes: 50 additions & 0 deletions packages/sdk-android/src/__tests__/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,42 @@ afterEach(() => {
});

describe('getAndroidDeviceToRunOn', () => {
it('should fail if a device is provided but no active device exists', async () => {
//GIVEN
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = true;
ctx.program.device = 'device1';
ctx.runtime.target = 'defaultTarget';
const mockFoundDevice = { name: 'simulator1', isActive: false, udid: '', isDevice: false };

jest.mocked(getAndroidTargets).mockResolvedValue([mockFoundDevice]);

//WHEN
await expect(getAndroidDeviceToRunOn(ctx)).resolves.toBe(undefined);

//THEN
});
it('should fail if targetToConnectWiFi is not a valid IP address - npx rnv -p android -t -d <invalidIPAdress>', async () => {
//GIVEN
const targetToConnectWiFi = 'invalidIPAdress';
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = true;
ctx.runtime.target = 'defaultTarget';
ctx.program.device = targetToConnectWiFi;
const mockFoundDevice = { name: 'simulator1', isActive: false, udid: '', isDevice: false };

net.isIP = jest.fn().mockReturnValue(false);

jest.mocked(getAndroidTargets).mockResolvedValue([mockFoundDevice]);

//WHEN
await expect(getAndroidDeviceToRunOn(ctx)).resolves.toBe(undefined);

//THEN
expect(connectToWifiDevice).not.toHaveBeenCalled();
});
it('should connect to WiFi device if targetToConnectWiFi is a string and a valid IP address', async () => {
//GIVEN
const targetToConnectWiFi = '192.168.0.1';
Expand All @@ -38,6 +74,20 @@ describe('getAndroidDeviceToRunOn', () => {
expect(connectToWifiDevice).toHaveBeenCalledWith(ctx, targetToConnectWiFi);
expect(result).toEqual(mockFoundDevice);
});
it('should return defaultTarget if it exists and -t is not specified', async () => {
//GIVEN
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = undefined;
ctx.runtime.target = 'defaultTarget';
const mockFoundDevice = { name: 'defaultTarget', isActive: true, udid: '', isDevice: false };

jest.mocked(getAndroidTargets).mockResolvedValue([mockFoundDevice]);

const result = await getAndroidDeviceToRunOn(ctx);
//THEN
expect(result).toEqual(mockFoundDevice);
});
it('should return found sim if target is provided and found - npx rnv -p android -t <target>', async () => {
//GIVEN
const ctx = getContext();
Expand Down
2 changes: 0 additions & 2 deletions packages/sdk-android/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ export const getAndroidDeviceToRunOn = async (c: Context) => {
}

const devicesAndEmulators = await getAndroidTargets(c, false, false, !!device);
console.log('devicesAndEmulators', devicesAndEmulators);
const activeDevices = devicesAndEmulators.filter((d) => d.isActive);
const inactiveDevices = devicesAndEmulators.filter((d) => !d.isActive);
const foundDevice = devicesAndEmulators.find(
(d) => d.udid.includes(target) || d.name.includes(target) || d.udid.includes(device) || d.name.includes(device)
);
console.log('foundDevice', foundDevice);

const askWhereToRun = async () => {
if (activeDevices.length || inactiveDevices.length) {
Expand Down

0 comments on commit 4a74d03

Please sign in to comment.