-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.test.js
More file actions
292 lines (224 loc) · 9.89 KB
/
interactive.test.js
File metadata and controls
292 lines (224 loc) · 9.89 KB
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
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const { expect } = require('chai');
const sinon = require('sinon');
// Inquirer v12 CJS export is { default: { prompt, ... } }; use default so stubs apply to what lib uses
const inquirer = require('inquirer').default || require('inquirer');
const {
inquireApp,
inquireCloneDirectory,
inquireAppType,
inquireLivePreviewSupport,
inquireRunDevServer,
inquireGithubAccessToken,
continueBootstrapCommand,
} = require('../lib/bootstrap/interactive');
const messages = require('../messages/index.json');
const { pathValidator } = require('@contentstack/cli-utilities');
describe('Interactive Functions Tests', () => {
let sandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe('#inquireApp', () => {
it('should return selected app when user selects an app', async () => {
const mockApps = [
{ displayName: 'React.js Starter', configKey: 'reactjs-starter' },
{ displayName: 'Next.js Starter', configKey: 'nextjs-starter' },
];
const selectedApp = mockApps[0];
sandbox.stub(inquirer, 'prompt').resolves({ app: selectedApp });
const result = await inquireApp(mockApps);
expect(result).to.equal(selectedApp);
expect(inquirer.prompt.calledOnce).to.be.true;
const promptArgs = inquirer.prompt.getCall(0).args[0];
expect(promptArgs[0].type).to.equal('list');
expect(promptArgs[0].name).to.equal('app');
expect(promptArgs[0].message).to.equal(messages.CLI_BOOTSTRAP_APP_SELECTION_ENQUIRY);
expect(promptArgs[0].choices).to.include('Exit');
expect(promptArgs[0].choices.length).to.equal(mockApps.length + 1);
});
it('should throw error when user selects Exit', async () => {
const mockApps = [
{ displayName: 'React.js Starter', configKey: 'reactjs-starter' },
];
sandbox.stub(inquirer, 'prompt').resolves({ app: 'Exit' });
sandbox.stub(require('@contentstack/cli-utilities').cliux, 'print');
try {
await inquireApp(mockApps);
expect.fail('Should have thrown an error');
} catch (error) {
expect(error.message).to.equal('Exit');
}
});
it('should format apps correctly for display', async () => {
const mockApps = [
{ displayName: 'App 1', configKey: 'app1' },
{ displayName: 'App 2', configKey: 'app2' },
];
sandbox.stub(inquirer, 'prompt').resolves({ app: mockApps[0] });
await inquireApp(mockApps);
const promptArgs = inquirer.prompt.getCall(0).args[0];
const choices = promptArgs[0].choices;
// Verify that apps are formatted correctly
expect(choices[0]).to.have.property('name', 'App 1');
expect(choices[0]).to.have.property('value', mockApps[0]);
expect(choices[1]).to.have.property('name', 'App 2');
expect(choices[1]).to.have.property('value', mockApps[1]);
});
});
describe('#inquireCloneDirectory', () => {
it('should return current working directory when Current Folder is selected', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ path: 'Current Folder' });
const result = await inquireCloneDirectory();
expect(result).to.equal(process.cwd());
expect(inquirer.prompt.calledOnce).to.be.true;
});
it('should prompt for custom path when Other is selected', async () => {
const customPath = '/custom/path/to/project';
sandbox
.stub(inquirer, 'prompt')
.onFirstCall()
.resolves({ path: 'Other' })
.onSecondCall()
.resolves({ path: customPath });
const result = await inquireCloneDirectory();
expect(result).to.equal(pathValidator(customPath));
expect(inquirer.prompt.calledTwice).to.be.true;
});
it('should validate custom path using pathValidator', async () => {
const rawPath = '/some/path';
sandbox
.stub(inquirer, 'prompt')
.onFirstCall()
.resolves({ path: 'Other' })
.onSecondCall()
.resolves({ path: rawPath });
const result = await inquireCloneDirectory();
expect(result).to.equal(pathValidator(rawPath));
});
});
describe('#inquireAppType', () => {
it('should return sampleapp when Sample App is selected', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ type: 'sampleapp' });
const result = await inquireAppType();
expect(result).to.equal('sampleapp');
expect(inquirer.prompt.calledOnce).to.be.true;
const promptArgs = inquirer.prompt.getCall(0).args[0];
expect(promptArgs[0].type).to.equal('list');
expect(promptArgs[0].name).to.equal('type');
expect(promptArgs[0].message).to.equal(messages.CLI_BOOTSTRAP_TYPE_OF_APP_ENQUIRY);
expect(promptArgs[0].choices).to.have.length(2);
expect(promptArgs[0].choices[0].value).to.equal('sampleapp');
expect(promptArgs[0].choices[1].value).to.equal('starterapp');
});
it('should return starterapp when Starter App is selected', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ type: 'starterapp' });
const result = await inquireAppType();
expect(result).to.equal('starterapp');
});
});
describe('#inquireLivePreviewSupport', () => {
it('should return true when user confirms live preview', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ livePreviewEnabled: true });
const result = await inquireLivePreviewSupport();
expect(result).to.be.true;
expect(inquirer.prompt.calledOnce).to.be.true;
const promptArgs = inquirer.prompt.getCall(0).args[0];
expect(promptArgs.type).to.equal('confirm');
expect(promptArgs.name).to.equal('livePreviewEnabled');
expect(promptArgs.message).to.equal('Enable live preview?');
});
it('should return false when user denies live preview', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ livePreviewEnabled: false });
const result = await inquireLivePreviewSupport();
expect(result).to.be.false;
});
});
describe('#inquireRunDevServer', () => {
it('should return true when user confirms running dev server', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ runDevServer: true });
const result = await inquireRunDevServer();
expect(result).to.be.true;
expect(inquirer.prompt.calledOnce).to.be.true;
const promptArgs = inquirer.prompt.getCall(0).args[0];
expect(promptArgs.type).to.equal('confirm');
expect(promptArgs.name).to.equal('runDevServer');
expect(promptArgs.message).to.equal(messages.CLI_BOOTSTRAP_RUN_DEV_SERVER_ENQUIRY);
expect(promptArgs.default).to.be.true;
});
it('should return false when user denies running dev server', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ runDevServer: false });
const result = await inquireRunDevServer();
expect(result).to.be.false;
});
it('should have default value of true', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ runDevServer: true });
await inquireRunDevServer();
const promptArgs = inquirer.prompt.getCall(0).args[0];
expect(promptArgs.default).to.be.true;
});
});
describe('#inquireGithubAccessToken', () => {
it('should return the provided GitHub access token', async () => {
const testToken = 'ghp_test123456789';
sandbox.stub(inquirer, 'prompt').resolves({ token: testToken });
const result = await inquireGithubAccessToken();
expect(result).to.equal(testToken);
expect(inquirer.prompt.calledOnce).to.be.true;
const promptArgs = inquirer.prompt.getCall(0).args[0];
expect(promptArgs[0].type).to.equal('string');
expect(promptArgs[0].name).to.equal('token');
expect(promptArgs[0].message).to.equal(messages.CLI_BOOTSTRAP_NO_ACCESS_TOKEN_CREATED);
});
it('should handle empty token input', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ token: '' });
const result = await inquireGithubAccessToken();
expect(result).to.equal('');
});
});
describe('#continueBootstrapCommand', () => {
it('should return yes when user selects yes', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ shouldContinue: 'yes' });
const result = await continueBootstrapCommand();
expect(result).to.equal('yes');
expect(inquirer.prompt.calledOnce).to.be.true;
const promptArgs = inquirer.prompt.getCall(0).args[0];
expect(promptArgs.type).to.equal('list');
expect(promptArgs.name).to.equal('shouldContinue');
expect(promptArgs.choices).to.include('yes');
expect(promptArgs.choices).to.include('no');
expect(promptArgs.loop).to.be.false;
});
it('should return no when user selects no', async () => {
sandbox.stub(inquirer, 'prompt').resolves({ shouldContinue: 'no' });
const result = await continueBootstrapCommand();
expect(result).to.equal('no');
});
});
describe('Edge cases and error handling', () => {
it('should handle inquirer errors gracefully', async () => {
const error = new Error('Inquirer error');
sandbox.stub(inquirer, 'prompt').rejects(error);
try {
await inquireApp([{ displayName: 'Test', configKey: 'test' }]);
expect.fail('Should have thrown an error');
} catch (err) {
expect(err).to.equal(error);
}
});
it('should handle empty apps array in inquireApp', async () => {
const selectedApp = { displayName: 'Test', configKey: 'test' };
sandbox.stub(inquirer, 'prompt').resolves({ app: selectedApp });
const result = await inquireApp([]);
expect(result).to.equal(selectedApp);
const promptArgs = inquirer.prompt.getCall(0).args[0];
// Should still have Exit option
expect(promptArgs[0].choices).to.include('Exit');
expect(promptArgs[0].choices.length).to.equal(1);
});
});
});