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 UTs #1650

Merged
merged 3 commits into from
Aug 14, 2024
Merged

add UTs #1650

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
164 changes: 164 additions & 0 deletions packages/sdk-react-native/src/__tests__/metroRunner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { createRnvContext, getContext, logInfo, chalk, executeAsync, logDefault, logRaw, logError } from '@rnv/core';
import { getEntryFile, confirmActiveBundler } from '@rnv/sdk-utils';
import { isBundlerActive } from '../common';
import { startReactNative } from '../metroRunner';
import { EnvVars } from '../env';

jest.mock('@rnv/core');
jest.mock('../common');
jest.mock('../env');
jest.mock('@rnv/sdk-utils');

beforeEach(() => {
createRnvContext();
jest.mocked(getEntryFile).mockReturnValue('index');
jest.spyOn(EnvVars, 'RCT_NO_LAUNCH_PACKAGER').mockReturnValue({ RCT_NO_LAUNCH_PACKAGER: 1 });
});

afterEach(() => {
jest.resetAllMocks();
});

const updateContext = () => {
const ctx = getContext();
ctx.platform = 'ios';
ctx.runtime.port = 8081;
ctx.runtime.localhost = 'localhost';
ctx.program.opts = jest.fn().mockReturnValue({ reset: false, resetHard: false });
return ctx;
};
describe('startReactNative tests', () => {
it('should return false if platform is not defined', async () => {
//GIVEN
//WHEN
const result = await startReactNative({});
//THEN
expect(logDefault).toHaveBeenCalledWith('startReactNative');
expect(result).toBe(false);
});
it('should execute command when bundler is active and the server is restarted ', async () => {
//GIVEN
updateContext();
jest.mocked(isBundlerActive).mockResolvedValue(true);
jest.mocked(confirmActiveBundler).mockResolvedValue(true);

const expectedCommand = `npx react-native start --port 8081 --no-interactive`;
//WHEN
await startReactNative({ waitForBundler: true });
//THEN
expect(isBundlerActive).toHaveBeenCalled();
expect(confirmActiveBundler).toHaveBeenCalled();
expect(executeAsync).toHaveBeenCalledWith(
expectedCommand,
expect.objectContaining({
env: expect.objectContaining({
RCT_NO_LAUNCH_PACKAGER: 1,
}),
})
);
expect(logRaw).toHaveBeenCalledWith(
expect.stringContaining('Dev server running at: http://localhost:8081/index.bundle?platform=ios')
);
});
it('should skip execute command when bundler is active and the server is not restarted', async () => {
//GIVEN
updateContext();
jest.mocked(isBundlerActive).mockResolvedValue(true);
jest.mocked(confirmActiveBundler).mockResolvedValue(false);
//WHEN
await expect(startReactNative({ waitForBundler: true })).resolves.toEqual(true);
//THEN
expect(executeAsync).not.toHaveBeenCalled();
});
it('should execute command with custom CLI path', async () => {
//GIVEN
updateContext();
jest.mocked(isBundlerActive).mockResolvedValue(true);
jest.mocked(confirmActiveBundler).mockResolvedValue(true);
const customCliPath = '/custom/path/to/react-native-cli';
const expectedCommand = `node ${customCliPath} start --port 8081 --no-interactive`;
//WHEN
await expect(startReactNative({ waitForBundler: true, customCliPath })).resolves.toEqual(undefined);
//THEN
expect(executeAsync).toHaveBeenCalledWith(
expectedCommand,
expect.objectContaining({
env: expect.objectContaining({
RCT_NO_LAUNCH_PACKAGER: 1,
}),
})
);
expect(logRaw).toHaveBeenCalledWith(
expect.stringContaining('Dev server running at: http://localhost:8081/index.bundle?platform=ios')
);
});
it('should skip bundler checks and execute command when waitForBundler is false', async () => {
//GIVEN
updateContext();
jest.mocked(executeAsync).mockResolvedValue('success');
const expectedCommand = `npx react-native start --port 8081 --no-interactive`;
//WHEN
await expect(startReactNative({ waitForBundler: false })).resolves.toEqual(true);
//THEN
expect(isBundlerActive).not.toHaveBeenCalled();
expect(confirmActiveBundler).not.toHaveBeenCalled();
expect(executeAsync).toHaveBeenCalledWith(
expectedCommand,
expect.objectContaining({
env: expect.not.objectContaining({
RCT_NO_LAUNCH_PACKAGER: 1,
}),
})
);
expect(logRaw).toHaveBeenCalledWith(
expect.stringContaining('Dev server running at: http://localhost:8081/index.bundle?platform=ios')
);
});
it('should execute command with metroConfigName and --reset-cache', async () => {
//GIVEN
const ctx = updateContext();
ctx.program.opts = jest.fn().mockReturnValue({ reset: true });
jest.mocked(executeAsync).mockResolvedValue('success');
const metroConfigName = 'metro.config.js';
const expectedCommand = `npx react-native start --port 8081 --no-interactive --config=${metroConfigName} --reset-cache`;
//WHEN
await startReactNative({ waitForBundler: false, metroConfigName });

//THEN
expect(executeAsync).toHaveBeenCalledWith(
expectedCommand,
expect.objectContaining({
env: expect.not.objectContaining({
RCT_NO_LAUNCH_PACKAGER: 1,
}),
})
);
expect(logRaw).toHaveBeenCalledWith(
expect.stringContaining('Dev server running at: http://localhost:8081/index.bundle?platform=ios')
);
expect(logInfo).toHaveBeenCalledWith(
`You passed ${chalk().bold.white('-r')} argument. --reset-cache will be applied to react-native`
);
});
it('should log error when executeAsync fails', async () => {
//GIVEN
updateContext();
const expectedCommand = `npx react-native start --port 8081 --no-interactive`;
const errorMessage = new Error('Something went wrong');
jest.mocked(executeAsync).mockRejectedValue(errorMessage);
//WHEN
await expect(startReactNative({ waitForBundler: false })).resolves.toEqual(true);
//THEN
expect(isBundlerActive).not.toHaveBeenCalled();
expect(confirmActiveBundler).not.toHaveBeenCalled();
expect(executeAsync).toHaveBeenCalledWith(
expectedCommand,
expect.objectContaining({
env: expect.not.objectContaining({
RCT_NO_LAUNCH_PACKAGER: 1,
}),
})
);
expect(logError).toHaveBeenCalledWith(errorMessage);
});
});
Loading