Skip to content

Commit

Permalink
Merge pull request #1455 from flexn-io/add_tests
Browse files Browse the repository at this point in the history
 add tests for getAndroidDeviceToRunOn
  • Loading branch information
pavjacko committed Mar 15, 2024
2 parents 10cd91b + 590e0db commit 3cbd336
Showing 1 changed file with 114 additions and 21 deletions.
135 changes: 114 additions & 21 deletions packages/sdk-android/src/__tests__/runner.test.ts
@@ -1,8 +1,11 @@
import { getAndroidTargets } from '../deviceManager';
import { getAndroidTargets, composeDevicesArray, connectToWifiDevice, checkForActiveEmulator } from '../deviceManager';
import { getAndroidDeviceToRunOn } from '../runner';
import { createRnvContext, getContext } from '@rnv/core';
import { createRnvContext, getContext, inquirerPrompt } from '@rnv/core';
import net from 'net';

jest.mock('../deviceManager');
jest.mock('@rnv/core');
jest.mock('net');

beforeEach(() => {
createRnvContext();
Expand All @@ -13,6 +16,78 @@ 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()).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()).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';
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = true;
ctx.runtime.target = 'defaultTarget';
ctx.program.device = targetToConnectWiFi;
net.isIP = jest.fn().mockReturnValue(true);

jest.mocked(connectToWifiDevice).mockResolvedValue(true);
const mockFoundDevice = { name: '192.168.0.1', isActive: true, udid: '', isDevice: true };
jest.mocked(getAndroidTargets).mockResolvedValue([mockFoundDevice]);
jest.mocked(checkForActiveEmulator).mockResolvedValue(mockFoundDevice);

//WHEN
const result = await getAndroidDeviceToRunOn();

//THEN
expect(connectToWifiDevice).toHaveBeenCalledWith(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();
//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 All @@ -22,26 +97,44 @@ describe('getAndroidDeviceToRunOn', () => {
const mockFoundDevice = { name: 'existingTarget', isActive: true, udid: '' };
jest.mocked(getAndroidTargets).mockResolvedValueOnce([mockFoundDevice]);
//WHEN
const result = await getAndroidDeviceToRunOn(ctx);
const result = await getAndroidDeviceToRunOn();
//THEN
expect(result).toEqual(mockFoundDevice);
});
// it('should ask from devices and sims if target is not provided - npx rnv -p android', async () => {
// //GIVEN
// const ctx = getContext();
// ctx.platform = 'android';
// ctx.program.target = false;
// ctx.runtime.target = 'defaultTarget';
// const mockDevicesAndEmulators = [
// { name: 'simulator1', udid: 'udid1', isActive: true },
// { name: 'simulator2', udid: 'udid2', isActive: false },
// // Add more mock targets as needed
// ];
// const { getAndroidTargets } = require('../deviceManager');
// getAndroidTargets.mockResolvedValueOnce(mockDevicesAndEmulators);
// //WHEN

// // const result = await getAndroidDeviceToRunOn(ctx);
// //THEN
// });
it('should ask devices and sims if no target is specified and the available target list does not include defaultTarget - npx rnv -p android - npx rnv -p android', async () => {
//GIVEN
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = false;
ctx.runtime.target = 'defaultTarget';

const mockDevicesAndEmulators = [
{ name: 'simulator1', udid: 'udid1', isActive: true },
{ name: 'simulator2', udid: 'udid2', isActive: false },
];

jest.mocked(getAndroidTargets).mockResolvedValue(mockDevicesAndEmulators);
jest.mocked(composeDevicesArray)
.mockReturnValueOnce([
{
key: 'simulator1',
name: 'simulator1',
value: 'simulator1',
icon: 'Phone 馃摫',
},
])
.mockReturnValueOnce([
{
key: 'simulator2',
name: 'simulator2',
value: 'simulator2',
icon: 'Phone 馃摫',
},
]);
jest.mocked(inquirerPrompt).mockResolvedValue({ chosenTarget: 'simulator1' });
//WHEN
const result = await getAndroidDeviceToRunOn();
//THEN
expect(result).toEqual(mockDevicesAndEmulators[0]);
});
});

0 comments on commit 3cbd336

Please sign in to comment.