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

add tests for getAndroidDeviceToRunOn #1455

Merged
merged 4 commits into from Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
133 changes: 113 additions & 20 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(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';
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(ctx);

//THEN
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 All @@ -26,22 +101,40 @@ describe('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(ctx);
//THEN
expect(result).toEqual(mockDevicesAndEmulators[0]);
});
});
1 change: 0 additions & 1 deletion packages/sdk-android/src/runner.ts
Expand Up @@ -86,7 +86,6 @@ export const getAndroidDeviceToRunOn = async (c: Context) => {
}

const devicesAndEmulators = await getAndroidTargets(c, false, false, !!device);

const activeDevices = devicesAndEmulators.filter((d) => d.isActive);
const inactiveDevices = devicesAndEmulators.filter((d) => !d.isActive);
const foundDevice = devicesAndEmulators.find(
Expand Down