Skip to content

Commit

Permalink
test: use better naming of methods and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
NickTolhurst26 committed Sep 11, 2020
1 parent 9e6131d commit 506be68
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions lib/stencil-init.spec.js
Original file line number Diff line number Diff line change
@@ -26,18 +26,12 @@ const getStencilConfig = () => ({
accessToken: "accessToken_from_stencilConfig",
githubToken: "githubToken_1234567890",
});

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 = () => ({
const getAnswers = () => ({
normalStoreUrl: "https://url-from-prompt.mybigcommerce.com",
port: 3003,
accessToken: "accessToken_from_prompt",
@@ -49,7 +43,7 @@ describe('StencilInit integration tests', () => {
describe('run', () => {
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 answers = getAnswers();
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);
@@ -76,10 +70,15 @@ describe('StencilInit integration tests', () => {
}),
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 = getCliAnswers();
const expectedResult = JSON.stringify({ customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG, ...answers }, null, 2);
const cliOptions = getCliOptions();
const cliOptionsAsAnswers = {
"normalStoreUrl": cliOptions.url,
"port": cliOptions.port,
"accessToken": cliOptions.token,
}
const expectedResult = JSON.stringify({ customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG, ...cliOptionsAsAnswers }, null, 2);
const fsWriteFileSyncStub = jest.spyOn(fs, "writeFileSync").mockImplementation(jest.fn());
const inquirerPromptStub = jest.spyOn(inquirer, 'prompt').mockReturnValue(answers);
const inquirerPromptStub = jest.spyOn(inquirer, 'prompt').mockReturnValue({});
const consoleErrorStub = jest.spyOn(console, 'error').mockImplementation(jest.fn());
const consoleLogStub = jest.spyOn(console, 'log').mockImplementation(jest.fn());

@@ -91,7 +90,7 @@ describe('StencilInit integration tests', () => {
serverConfig,
logger: console,
});
await instance.run(dotStencilFilePath, getCliOptions());
await instance.run(dotStencilFilePath, cliOptions);

expect(fsWriteFileSyncStub).toHaveBeenCalledTimes(1);
expect(inquirerPromptStub).toHaveBeenCalledTimes(1);
@@ -120,7 +119,7 @@ describe('StencilInit unit tests', () => {
error: jest.fn(),
};
inquirerStub = {
prompt: jest.fn().mockReturnValue(getPromptAnswers()),
prompt: jest.fn().mockReturnValue(getAnswers()),
};
jsonLintStub = {
parse: jest.fn().mockReturnValue(getStencilConfig()),
@@ -253,7 +252,7 @@ describe('StencilInit unit tests', () => {

describe('askQuestions', () => {
it('should call inquirer.prompt with correct arguments when cliOptions are empty', async () => {
const defaultAnswers = getPromptAnswers();
const defaultAnswers = getAnswers();
const cliOptions = {}
const instance = getStencilInitInstance();

@@ -303,7 +302,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 = getPromptAnswers();
const answers = getAnswers();
const instance = getStencilInitInstance();

await assertNoMutations(
@@ -314,7 +313,7 @@ describe('StencilInit unit tests', () => {

it('should correctly merge values from the passed objects', async () => {
const stencilConfig = getStencilConfig();
const answers = getPromptAnswers();
const answers = getAnswers();
const instance = getStencilInitInstance();

const res = instance.applyAnswers(stencilConfig, answers);
@@ -329,7 +328,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 = getPromptAnswers();
const answers = getAnswers();
const instance = getStencilInitInstance();

const res = instance.applyAnswers(stencilConfig, answers);

0 comments on commit 506be68

Please sign in to comment.