forked from bigcommerce/stencil-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstencil-init.spec.js
409 lines (345 loc) · 15.8 KB
/
stencil-init.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
const _ = require('lodash');
const inquirerModule = require('inquirer');
const StencilInit = require('./stencil-init');
const StencilConfigManager = require('./StencilConfigManager');
const { DEFAULT_CUSTOM_LAYOUTS_CONFIG, API_HOST } = require('../constants');
const { assertNoMutations } = require('../test/assertions/assertNoMutations');
const getStencilConfig = () => ({
customLayouts: {
brand: {
a: 'aaaa',
},
category: {},
page: {
b: 'bbbb',
},
product: {},
},
normalStoreUrl: 'https://url-from-stencilConfig.mybigcommerce.com',
port: 3001,
accessToken: 'accessToken_from_stencilConfig',
githubToken: 'githubToken_1234567890',
apiHost: API_HOST,
});
const getAnswers = () => ({
normalStoreUrl: 'https://url-from-answers.mybigcommerce.com',
port: 3003,
accessToken: 'accessToken_from_answers',
apiHost: API_HOST,
});
const getCliOptions = () => ({
normalStoreUrl: 'https://url-from-cli-options.mybigcommerce.com',
port: 3002,
accessToken: 'accessToken_from_CLI_options',
apiHost: API_HOST,
});
const getQuestions = () => [
{
type: 'input',
name: 'normalStoreUrl',
message: "What is the URL of your store's home page?",
validate: (val) => /^https?:\/\//.test(val) || 'You must enter a URL',
default: 'https://url-from-answers.mybigcommerce.com',
},
{
type: 'input',
name: 'accessToken',
message: 'What is your Stencil OAuth Access Token?',
default: 'accessToken_from_answers',
filter: (val) => val.trim(),
},
{
type: 'input',
name: 'port',
message: 'What port would you like to run the server on?',
default: 3003,
validate: (val) => {
if (Number.isNaN(val)) {
return 'You must enter an integer';
}
if (val < 1024 || val > 65535) {
return 'The port number must be between 1025 and 65535';
}
return true;
},
},
];
afterEach(() => jest.restoreAllMocks());
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 answers = getAnswers();
const expectedResult = {
customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG,
...answers,
};
const stencilConfigManager = new StencilConfigManager({
themePath: './test/_mocks/themes/valid/',
});
const saveStencilConfigStub = jest
.spyOn(stencilConfigManager, 'save')
.mockImplementation(jest.fn());
const inquirerPromptStub = jest
.spyOn(inquirerModule, '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: inquirerModule,
stencilConfigManager,
logger: console,
});
await instance.run();
expect(inquirerPromptStub).toHaveBeenCalledTimes(1);
expect(consoleErrorStub).toHaveBeenCalledTimes(0);
expect(consoleLogStub).toHaveBeenCalledTimes(3);
expect(saveStencilConfigStub).toHaveBeenCalledTimes(1);
expect(saveStencilConfigStub).toHaveBeenCalledWith(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 cliOptions = getCliOptions();
const expectedResult = {
customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG,
...cliOptions,
};
const stencilConfigManager = new StencilConfigManager({
themePath: './test/_mocks/themes/valid/',
});
const saveStencilConfigStub = jest
.spyOn(stencilConfigManager, 'save')
.mockImplementation(jest.fn());
const inquirerPromptStub = jest.spyOn(inquirerModule, 'prompt').mockReturnValue({});
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: inquirerModule,
stencilConfigManager,
logger: console,
});
await instance.run(cliOptions);
expect(inquirerPromptStub).toHaveBeenCalledTimes(0);
expect(consoleErrorStub).toHaveBeenCalledTimes(0);
expect(consoleLogStub).toHaveBeenCalledTimes(3);
expect(saveStencilConfigStub).toHaveBeenCalledTimes(1);
expect(saveStencilConfigStub).toHaveBeenCalledWith(expectedResult);
expect(consoleLogStub).toHaveBeenCalledWith(
'You are now ready to go! To start developing, run $ ' + 'stencil start'.cyan,
);
});
});
});
describe('StencilInit unit tests', () => {
const serverConfigPort = 3000;
const dotStencilFilePath = '/some/test/path/.stencil';
const getLoggerStub = () => ({
log: jest.fn(),
error: jest.fn(),
});
const getInquirerStub = () => ({
prompt: jest.fn().mockReturnValue(getAnswers()),
});
const getStencilConfigManagerStub = () => ({
read: jest.fn().mockReturnValue(getStencilConfig()),
save: jest.fn(),
});
const getServerConfigStub = () => ({
get: jest.fn(
(prop) =>
({
'/server/port': serverConfigPort,
}[prop]),
),
});
const createStencilInitInstance = ({
inquirer,
stencilConfigManager,
serverConfig,
logger,
} = {}) => {
const passedArgs = {
inquirer: inquirer || getInquirerStub(),
stencilConfigManager: stencilConfigManager || getStencilConfigManagerStub(),
serverConfig: serverConfig || getServerConfigStub(),
logger: logger || getLoggerStub(),
};
const instance = new StencilInit(passedArgs);
return {
passedArgs,
instance,
};
};
describe('constructor', () => {
it('should create an instance of StencilInit without options parameters passed', async () => {
const instance = new StencilInit();
expect(instance).toBeInstanceOf(StencilInit);
});
it('should create an instance of StencilInit with options parameters passed', async () => {
const { instance } = createStencilInitInstance();
expect(instance).toBeInstanceOf(StencilInit);
});
});
describe('readStencilConfig', () => {
it("should return an empty config if the file doesn't exist", async () => {
const loggerStub = getLoggerStub();
const stencilConfigManagerStub = getStencilConfigManagerStub();
stencilConfigManagerStub.read.mockReturnValue(null);
const { instance } = createStencilInitInstance({
stencilConfigManager: stencilConfigManagerStub,
logger: loggerStub,
});
const res = await instance.readStencilConfig(dotStencilFilePath);
expect(stencilConfigManagerStub.read).toHaveBeenCalledTimes(1);
expect(stencilConfigManagerStub.read).toHaveBeenCalledWith(true, true);
expect(loggerStub.error).toHaveBeenCalledTimes(0);
expect(res).toEqual({});
});
it('should read the file and return parsed results if the file exists and it is valid', async () => {
const parsedConfig = getStencilConfig();
const stencilConfigManagerStub = getStencilConfigManagerStub();
const loggerStub = getLoggerStub();
stencilConfigManagerStub.read.mockReturnValue(parsedConfig);
const { instance } = createStencilInitInstance({
stencilConfigManager: stencilConfigManagerStub,
logger: loggerStub,
});
const res = await instance.readStencilConfig(dotStencilFilePath);
expect(stencilConfigManagerStub.read).toHaveBeenCalledTimes(1);
expect(stencilConfigManagerStub.read).toHaveBeenCalledWith(true, true);
expect(loggerStub.error).toHaveBeenCalledTimes(0);
expect(res).toEqual(parsedConfig);
});
it('should read the file, inform the user that the file is broken and return an empty config', async () => {
const thrownError = new Error('invalid file');
const loggerStub = getLoggerStub();
const stencilConfigManagerStub = getStencilConfigManagerStub();
stencilConfigManagerStub.read.mockRejectedValue(thrownError);
const { instance } = createStencilInitInstance({
stencilConfigManager: stencilConfigManagerStub,
logger: loggerStub,
});
const res = await instance.readStencilConfig(dotStencilFilePath);
expect(stencilConfigManagerStub.read).toHaveBeenCalledTimes(1);
expect(stencilConfigManagerStub.read).toHaveBeenCalledWith(true, true);
expect(loggerStub.error).toHaveBeenCalledTimes(1);
expect(res).toEqual({});
});
});
describe('getDefaultAnswers', () => {
// eslint-disable-next-line jest/expect-expect
it('should not mutate the passed objects', async () => {
const stencilConfig = getStencilConfig();
const { instance } = createStencilInitInstance();
await assertNoMutations([stencilConfig], () =>
instance.getDefaultAnswers(stencilConfig),
);
});
it('should pick values from stencilConfig if not empty', async () => {
const stencilConfig = getStencilConfig();
const { instance } = createStencilInitInstance();
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 are empty', async () => {
const stencilConfig = _.pick(getStencilConfig(), ['accessToken', 'url']);
const { instance } = createStencilInitInstance();
const res = instance.getDefaultAnswers(stencilConfig);
expect(res.port).toEqual(serverConfigPort);
expect(res.normalStoreUrl).toEqual(stencilConfig.url);
expect(res.accessToken).toEqual(stencilConfig.accessToken);
});
});
describe('getQuestions', () => {
it('should get all questions if no cli options were passed', async () => {
const defaultAnswers = getAnswers();
const cliConfig = {};
const { instance } = createStencilInitInstance();
const res = instance.getQuestions(defaultAnswers, cliConfig);
// We compare the serialized results because the objects contain functions which hinders direct comparison
expect(JSON.stringify(res)).toEqual(JSON.stringify(getQuestions()));
});
});
describe('askQuestions', () => {
it('should call inquirer.prompt with correct arguments', async () => {
const questions = getQuestions();
const inquirerStub = getInquirerStub();
const { instance } = createStencilInitInstance({
inquirer: inquirerStub,
});
await instance.askQuestions(questions);
expect(inquirerStub.prompt).toHaveBeenCalledTimes(1);
// We compare the serialized results because the objects contain functions which hinders direct comparison
expect(JSON.stringify(inquirerStub.prompt.mock.calls)).toEqual(
JSON.stringify([[questions]]),
);
});
});
describe('applyAnswers', () => {
const cliOptions = getCliOptions();
// eslint-disable-next-line jest/expect-expect
it('should not mutate the passed objects', async () => {
const stencilConfig = getStencilConfig();
const answers = getAnswers();
const { instance } = createStencilInitInstance();
await assertNoMutations([stencilConfig, answers, cliOptions], () =>
instance.applyAnswers(stencilConfig, answers, cliOptions),
);
});
it('should correctly merge values from the passed objects', async () => {
const stencilConfig = getStencilConfig();
delete cliOptions.apiHost;
const answers = getAnswers();
const { instance } = createStencilInitInstance();
const res = instance.applyAnswers(stencilConfig, answers, cliOptions);
expect(res.normalStoreUrl).toEqual(answers.normalStoreUrl);
expect(res.accessToken).toEqual(answers.accessToken);
expect(res.port).toEqual(answers.port);
expect(res.apiHost).toEqual(answers.apiHost);
expect(res.githubToken).toEqual(stencilConfig.githubToken);
expect(res.customLayouts).toEqual(stencilConfig.customLayouts);
});
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 { instance } = createStencilInitInstance();
const res = instance.applyAnswers(stencilConfig, answers, cliOptions);
expect(res.customLayouts).toEqual(DEFAULT_CUSTOM_LAYOUTS_CONFIG);
// Make sure that other props aren't overwritten:
expect(res.accessToken).toEqual(answers.accessToken);
expect(res.githubToken).toEqual(stencilConfig.githubToken);
});
});
describe('updateApiHost', () => {
const options = getCliOptions();
const config = getStencilConfig();
it('should return the same config if it contains apiHost', async () => {
const { instance } = createStencilInitInstance();
delete options.apiHost;
const res = instance.updateApiHost(config, options);
expect(res).toEqual(config);
});
it('should add default apiHost if neither config, nor options contain apiHost', async () => {
const { instance } = createStencilInitInstance();
delete config.apiHost;
delete options.apiHost;
const res = instance.updateApiHost(config, options);
const expected = getStencilConfig();
expect(res).toEqual(expected);
});
it('should add custom apiHost', async () => {
const { instance } = createStencilInitInstance();
delete config.apiHost;
delete options.apiHost;
const cliOptions = { ...options, apiHost: 'https://custom.api.com' };
const expectedConfig = { ...config, apiHost: 'https://custom.api.com' };
const res = instance.updateApiHost(config, cliOptions);
expect(res).toEqual(expectedConfig);
});
});
});