diff --git a/lib/stencil-init.spec.js b/lib/stencil-init.spec.js index 6c2d42c6..4bd37296 100644 --- a/lib/stencil-init.spec.js +++ b/lib/stencil-init.spec.js @@ -26,24 +26,57 @@ const getStencilConfig = () => ({ accessToken: "accessToken_from_stencilConfig", githubToken: "githubToken_1234567890", }); -const getAnswers = () => ({ - normalStoreUrl: "https://url-from-answers.mybigcommerce.com", - port: 3003, - accessToken: "accessToken_from_answers", -}); + const getCliOptions = () => ({ url: "https://url-from-cli-options.mybigcommerce.com", port: 3002, token: "accessToken_from_CLI_options", }); +const getCliAnswers = () => ({ + normalStoreUrl: "https://url-from-cli-options.mybigcommerce.com", + port: 3002, + accessToken: "accessToken_from_CLI_options", +}); +const getPromptAnswers = () => ({ + normalStoreUrl: "https://url-from-prompt.mybigcommerce.com", + port: 3003, + accessToken: "accessToken_from_prompt", +}); afterEach(() => jest.restoreAllMocks()); describe('StencilInit integration tests', () => { describe('run', () => { - it('should perform all the actions, save the result and inform the user about the successful finish', async () => { + it('using cli prompts, should perform all the actions, save the result and inform the user about the successful finish', async () => { + const dotStencilFilePath = './test/_mocks/bin/dotStencilFile.json'; + const answers = getPromptAnswers(); + const expectedResult = JSON.stringify({ customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG, ...answers }, null, 2); + const fsWriteFileSyncStub = jest.spyOn(fs, "writeFileSync").mockImplementation(jest.fn()); + const inquirerPromptStub = jest.spyOn(inquirer, 'prompt').mockReturnValue(answers); + const consoleErrorStub = jest.spyOn(console, 'error').mockImplementation(jest.fn()); + const consoleLogStub = jest.spyOn(console, 'log').mockImplementation(jest.fn()); + + // Test with real entities, just some methods stubbed + const instance = new StencilInit({ + inquirer, + jsonLint, + fs, + serverConfig, + logger: console, + }); + await instance.run(dotStencilFilePath); + + expect(fsWriteFileSyncStub).toHaveBeenCalledTimes(1); + expect(inquirerPromptStub).toHaveBeenCalledTimes(1); + expect(consoleErrorStub).toHaveBeenCalledTimes(0); + expect(consoleLogStub).toHaveBeenCalledTimes(1); + + expect(fsWriteFileSyncStub).toHaveBeenCalledWith(dotStencilFilePath, expectedResult); + expect(consoleLogStub).toHaveBeenCalledWith('You are now ready to go! To start developing, run $ ' + 'stencil start'.cyan); + }), + it('using cli options, should perform all the actions, save the result and inform the user about the successful finish', async () => { const dotStencilFilePath = './test/_mocks/bin/dotStencilFile.json'; - const answers = getAnswers(); + const answers = getCliAnswers(); const expectedResult = JSON.stringify({ customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG, ...answers }, null, 2); const fsWriteFileSyncStub = jest.spyOn(fs, "writeFileSync").mockImplementation(jest.fn()); const inquirerPromptStub = jest.spyOn(inquirer, 'prompt').mockReturnValue(answers); @@ -87,7 +120,7 @@ describe('StencilInit unit tests', () => { error: jest.fn(), }; inquirerStub = { - prompt: jest.fn().mockReturnValue(getAnswers()), + prompt: jest.fn().mockReturnValue(getPromptAnswers()), }; jsonLintStub = { parse: jest.fn().mockReturnValue(getStencilConfig()), @@ -186,59 +219,45 @@ describe('StencilInit unit tests', () => { // eslint-disable-next-line jest/expect-expect it('should not mutate the passed objects', async () => { const stencilConfig = getStencilConfig(); - const cliOptions = getCliOptions(); const instance = getStencilInitInstance(); await assertNoMutations( - [stencilConfig, cliOptions], - () => instance.getDefaultAnswers(stencilConfig, cliOptions), + [stencilConfig], + () => instance.getDefaultAnswers(stencilConfig), ); }); - it('should pick values from cliOptions first if present', async () => { - const stencilConfig = getStencilConfig(); - const cliOptions = getCliOptions(); - const instance = getStencilInitInstance(); - - const res = instance.getDefaultAnswers(stencilConfig, cliOptions); - - expect(res.normalStoreUrl).toEqual(cliOptions.url); - expect(res.accessToken).toEqual(cliOptions.token); - expect(res.port).toEqual(cliOptions.port); - }); - - it('should pick values from stencilConfig if cliOptions are empty', async () => { + it('should pick values from stencilConfig if not empty', async () => { const stencilConfig = getStencilConfig(); - const cliOptions = {}; const instance = getStencilInitInstance(); - const res = instance.getDefaultAnswers(stencilConfig, cliOptions); + const res = instance.getDefaultAnswers(stencilConfig); expect(res.normalStoreUrl).toEqual(stencilConfig.normalStoreUrl); expect(res.accessToken).toEqual(stencilConfig.accessToken); expect(res.port).toEqual(stencilConfig.port); }); - it('should pick values from serverConfig if stencilConfig and cliOptions are empty', async () => { - const cliOptions = _.pick(getCliOptions(), ['url']); - const stencilConfig = _.pick(getStencilConfig(), ['accessToken']); + it('should pick values from serverConfig if stencilConfig are empty', async () => { + const stencilConfig = _.pick(getStencilConfig(), ['accessToken','url']); const instance = getStencilInitInstance(); - const res = instance.getDefaultAnswers(stencilConfig, cliOptions); + const res = instance.getDefaultAnswers(stencilConfig); expect(res.port).toEqual(serverConfigPort); - expect(res.normalStoreUrl).toEqual(cliOptions.url); + expect(res.normalStoreUrl).toEqual(stencilConfig.url); expect(res.accessToken).toEqual(stencilConfig.accessToken); }); }); describe('askQuestions', () => { - it('should call inquirer.prompt with correct arguments', async () => { - const defaultAnswers = getAnswers(); + it('should call inquirer.prompt with correct arguments when cliOptions are empty', async () => { + const defaultAnswers = getPromptAnswers(); + const cliOptions = {} const instance = getStencilInitInstance(); - await instance.askQuestions(defaultAnswers); + await instance.askQuestions(defaultAnswers, cliOptions); expect(inquirerStub.prompt).toHaveBeenCalledTimes(1); // We compare the serialized results because the objects contain functions which hinders direct comparison @@ -284,7 +303,7 @@ describe('StencilInit unit tests', () => { // eslint-disable-next-line jest/expect-expect it('should not mutate the passed objects', async () => { const stencilConfig = getStencilConfig(); - const answers = getAnswers(); + const answers = getPromptAnswers(); const instance = getStencilInitInstance(); await assertNoMutations( @@ -295,7 +314,7 @@ describe('StencilInit unit tests', () => { it('should correctly merge values from the passed objects', async () => { const stencilConfig = getStencilConfig(); - const answers = getAnswers(); + const answers = getPromptAnswers(); const instance = getStencilInitInstance(); const res = instance.applyAnswers(stencilConfig, answers); @@ -310,7 +329,7 @@ describe('StencilInit unit tests', () => { it('should add a customLayouts property with default empty values if it\'s absent in stencilConfig', async () => { const stencilConfig = _.omit(getStencilConfig(), 'customLayouts'); - const answers = getAnswers(); + const answers = getPromptAnswers(); const instance = getStencilInitInstance(); const res = instance.applyAnswers(stencilConfig, answers);