react-native-integrate
Version:
Automate integration of additional code into React Native projects
161 lines (160 loc) • 5.64 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const prettier_1 = __importDefault(require("prettier"));
const mockPrettierResolve = jest.spyOn(prettier_1.default, 'resolveConfig');
const mockPrettierFormat = jest.spyOn(prettier_1.default, 'format');
require('../../mocks/mockAll');
const path_1 = __importDefault(require("path"));
const constants_1 = require("../../../constants");
const babelConfigTask_1 = require("../../../tasks/babelConfigTask");
const jsObjectParser_1 = require("../../../utils/jsObjectParser");
const mockFs_1 = require("../../mocks/mockFs");
describe('babelConfigTask', () => {
it('should modify blocks correctly', async () => {
let content = new jsObjectParser_1.JsObjectParser(`module.exports = {
presets: ['item1', ['item2'], {'item3'}],
};
`);
const task = {
task: 'babel_config',
actions: [
{
set: {
presets: [{ $prepend: 'item0' }],
},
},
{
set: {
presets: ['item4'],
},
},
{
set: {
presets: [{ $search: 'item2', $replace: 'item21' }],
},
},
{
mode: 'text',
prepend: 'require("something");',
},
],
};
content = await (0, babelConfigTask_1.babelConfigTask)({
configPath: 'path/to/config',
task: task,
content,
packageName: 'test-package',
});
expect(content.stringify()).toMatchInlineSnapshot(`
"require("something");
module.exports = {
presets: ["item0", 'item1', "item21", {'item3'}, "item4"],
};
"
`);
});
it('should skip when condition not met', async () => {
const content = new jsObjectParser_1.JsObjectParser(`module.exports = {
presets: ['item1', ['item2'], {'item3'}],
};
`);
const task = {
task: 'babel_config',
actions: [
{
when: { random: 'value' },
set: {
presets: [{ $prepend: 'item0' }],
},
},
],
};
const newContent = await (0, babelConfigTask_1.babelConfigTask)({
configPath: 'path/to/config',
task: task,
content,
packageName: 'test-package',
});
expect(newContent).toEqual(content);
});
describe('runTask', () => {
it('should read and write babel config file', async () => {
let content = `module.exports = {
presets: ['item1', 'item2', 'item3'],
};
`;
const babelConfigPath = path_1.default.resolve(__dirname, `../../mock-project/${constants_1.Constants.BABEL_CONFIG_FILE_NAME}`);
mockFs_1.mockFs.writeFileSync(babelConfigPath, content);
const task = {
task: 'babel_config',
actions: [
{
set: {
presets: ['test'],
},
},
],
};
await (0, babelConfigTask_1.runTask)({
configPath: 'path/to/config',
task: task,
packageName: 'test-package',
});
content = mockFs_1.mockFs.readFileSync(babelConfigPath);
// @ts-ignore
expect(content).toMatchInlineSnapshot(`
"module.exports = {
presets: ['item1', 'item2', 'item3', 'test'],
};
"
`);
});
it('should run prettier with default parser', async () => {
const content = `module.exports = {
presets: ['item1', 'item2', 'item3'],
};
`;
mockPrettierResolve.mockResolvedValueOnce(null);
const babelConfigPath = path_1.default.resolve(__dirname, `../../mock-project/${constants_1.Constants.BABEL_CONFIG_FILE_NAME}`);
mockFs_1.mockFs.writeFileSync(babelConfigPath, content);
const task = {
task: 'babel_config',
actions: [
{
set: {
presets: ['test'],
},
},
],
};
await (0, babelConfigTask_1.runTask)({
configPath: 'path/to/config',
task: task,
packageName: 'test-package',
});
expect(mockPrettierFormat).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
parser: 'babel',
}));
});
it('should throw when babel config does not exist', async () => {
const task = {
task: 'babel_config',
actions: [
{
set: {
presets: [{ $prepend: 'item0' }],
},
},
],
};
await expect((0, babelConfigTask_1.runTask)({
configPath: 'path/to/config',
task: task,
packageName: 'test-package',
})).rejects.toThrowError('babel.config.js file not found');
});
});
});