bod
Version:
Boilerplate CLI App
97 lines (96 loc) • 4.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const ci_info_1 = require("ci-info");
const rimraf_1 = require("rimraf");
const utils_1 = require("../../utils");
const CreateCommand_1 = tslib_1.__importDefault(require("../CreateCommand"));
const appPath = 'bod-unit-tests';
describe('createCommand', () => {
beforeEach(() => (0, rimraf_1.sync)(appPath));
afterEach(() => (0, rimraf_1.sync)(appPath));
it('should extends [BaseCommand] fields', () => {
const createCommand = new CreateCommand_1.default();
expect(createCommand.getName()).toBe('create');
expect(createCommand.getDescription()).toBe('Create a new project powered by @sabertazimi/react-scripts');
expect(createCommand.getUsage()).toBe('create <appName>');
expect(createCommand.getAlias()).toBe('c');
});
it.each(CreateCommand_1.default.TemplateActions)('should get correct command/args and invoke [inquirer] via template choice [$name]', (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({ value }) {
const mockPrompt = jest
.spyOn(utils_1.inquirer, 'prompt')
.mockImplementation(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const promise = new Promise((resolve) => {
resolve({ templateName: value });
});
return promise;
}));
const mockSpawn = jest.spyOn(utils_1.spawn, 'sync').mockImplementation(() => {
return {
status: 0,
};
});
const additionalOptions = value === 'vue'
? ['--default']
: value === 'vite'
? ['--template', 'vue']
: [];
const createCommand = new CreateCommand_1.default();
yield expect(createCommand.run(appPath, additionalOptions)).resolves.toBeUndefined();
const { command, args, postCommands } = CreateCommand_1.default.TemplateActions.find(action => action.value === value);
expect(createCommand.getCommand()).toBe(command);
expect(createCommand.getCommandArgs()).toHaveLength(args.length + 1);
expect(createCommand.getCommandArgs()).toStrictEqual(args.concat(appPath));
expect(mockPrompt).toHaveBeenCalledTimes(1);
expect(mockSpawn).toHaveBeenCalledTimes(postCommands.length + 1);
mockPrompt.mockRestore();
mockSpawn.mockRestore();
}));
it.each(CreateCommand_1.default.TemplateActions)('should throw error when exited with non zero via template choice [$name]', (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({ value }) {
const mockPrompt = jest
.spyOn(utils_1.inquirer, 'prompt')
.mockImplementation(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const promise = new Promise((resolve) => {
resolve({ templateName: value });
});
return promise;
}));
const mockSpawn = jest.spyOn(utils_1.spawn, 'sync').mockImplementation(() => {
return {
status: 1,
};
});
const additionalOptions = value === 'vue'
? ['--default']
: value === 'vite'
? ['--template', 'vue']
: [];
const createCommand = new CreateCommand_1.default();
yield expect(createCommand.run(appPath, additionalOptions)).rejects.toThrow();
expect(mockPrompt).toHaveBeenCalledTimes(1);
expect(mockSpawn).toHaveBeenCalledTimes(1);
mockPrompt.mockRestore();
mockSpawn.mockRestore();
}));
it.each(CreateCommand_1.default.TemplateActions)('should initialize app directory via template choice [$name]', (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({ value }) {
const mockPrompt = jest
.spyOn(utils_1.inquirer, 'prompt')
.mockImplementation(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const promise = new Promise((resolve) => {
resolve({ templateName: value });
});
return promise;
}));
const additionalOptions = value === 'vue'
? ['--default']
: value === 'vite'
? ['--template', 'vue']
: [];
const createCommand = new CreateCommand_1.default();
if (ci_info_1.isCI) {
yield expect(createCommand.run(appPath, additionalOptions)).resolves.toBeUndefined();
expect(mockPrompt).toHaveBeenCalledTimes(1);
}
mockPrompt.mockRestore();
}));
});