-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.test.ts
More file actions
335 lines (278 loc) · 9.27 KB
/
interactive.test.ts
File metadata and controls
335 lines (278 loc) · 9.27 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
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
// Mock inquirer before importing anything
const mockInquirer = {
prompt: jest.fn(),
};
jest.mock('inquirer', () => ({
__esModule: true,
default: mockInquirer,
}));
import * as interactive from '../../src/seed/interactive';
import { Organization, Stack } from '../../src/seed/contentstack/client';
describe('Interactive', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('inquireRepo', () => {
it('should return single repo when only one is provided', async () => {
const repos = [
{
name: 'stack-test',
html_url: 'https://github.com/user/stack-test',
},
];
const result = await interactive.inquireRepo(repos);
expect(result.choice).toBe('stack-test');
expect(mockInquirer.prompt).not.toHaveBeenCalled();
});
it('should prompt user when multiple repos are available', async () => {
const repos = [
{
name: 'stack-repo1',
html_url: 'https://github.com/user/stack-repo1',
},
{
name: 'stack-repo2',
html_url: 'https://github.com/user/stack-repo2',
},
];
mockInquirer.prompt = jest.fn().mockResolvedValue({
choice: 'stack-repo1',
});
const result = await interactive.inquireRepo(repos);
expect(mockInquirer.prompt).toHaveBeenCalledWith([
expect.objectContaining({
type: 'list',
name: 'choice',
message: 'Select a Stack to Import',
}),
]);
expect(result.choice).toBe('stack-repo1');
});
it('should throw error when no repos are provided', async () => {
await expect(interactive.inquireRepo([])).rejects.toThrow(
'Precondition failed: No Repositories found.',
);
});
it('should throw error when repos is null', async () => {
await expect(interactive.inquireRepo(null as any)).rejects.toThrow(
'Precondition failed: No Repositories found.',
);
});
});
describe('inquireOrganization', () => {
it('should return single organization when only one is provided', async () => {
const organizations: Organization[] = [
{
uid: 'org-1',
name: 'Organization 1',
enabled: true,
},
];
const result = await interactive.inquireOrganization(organizations);
expect(result).toEqual(organizations[0]);
expect(mockInquirer.prompt).not.toHaveBeenCalled();
});
it('should prompt user when multiple organizations are available', async () => {
const organizations: Organization[] = [
{
uid: 'org-1',
name: 'Organization 1',
enabled: true,
},
{
uid: 'org-2',
name: 'Organization 2',
enabled: true,
},
];
mockInquirer.prompt = jest.fn().mockResolvedValue({
uid: 'org-2',
});
const result = await interactive.inquireOrganization(organizations);
expect(mockInquirer.prompt).toHaveBeenCalledWith([
expect.objectContaining({
type: 'list',
name: 'uid',
message: 'Select an Organization',
}),
]);
expect(result).toEqual(organizations[1]);
});
it('should throw error when no organizations are provided', async () => {
await expect(interactive.inquireOrganization([])).rejects.toThrow(
'Precondition failed: No Organizations found.',
);
});
it('should throw error when organizations is null', async () => {
await expect(interactive.inquireOrganization(null as any)).rejects.toThrow(
'Precondition failed: No Organizations found.',
);
});
});
describe('inquireProceed', () => {
it('should return true when user confirms', async () => {
mockInquirer.prompt = jest.fn().mockResolvedValue({
choice: true,
});
const result = await interactive.inquireProceed();
expect(mockInquirer.prompt).toHaveBeenCalledWith([
expect.objectContaining({
type: 'confirm',
name: 'choice',
message: 'This Stack contains content. Do you wish to continue?',
}),
]);
expect(result).toBe(true);
});
it('should return false when user declines', async () => {
mockInquirer.prompt = jest.fn().mockResolvedValue({
choice: false,
});
const result = await interactive.inquireProceed();
expect(result).toBe(false);
});
});
describe('inquireStack', () => {
it('should create new stack when stackName is provided', async () => {
const stacks: Stack[] = [];
const stackName = 'My New Stack';
const result = await interactive.inquireStack(stacks, stackName);
expect(result.isNew).toBe(true);
expect(result.name).toBe('My New Stack');
expect(mockInquirer.prompt).not.toHaveBeenCalled();
});
it('should prompt for new or existing when stacks exist and no stackName', async () => {
const stacks: Stack[] = [
{
uid: 'stack-1',
name: 'Existing Stack',
master_locale: 'en-us',
api_key: 'api-key-1',
org_uid: 'org-1',
},
];
(mockInquirer.prompt as jest.Mock)
.mockResolvedValueOnce({
choice: true, // New stack
})
.mockResolvedValueOnce({
name: 'My New Stack',
});
const result = await interactive.inquireStack(stacks);
expect(result.isNew).toBe(true);
expect(result.name).toBe('My New Stack');
});
it('should select existing stack when user chooses existing', async () => {
const stacks: Stack[] = [
{
uid: 'stack-1',
name: 'Existing Stack',
master_locale: 'en-us',
api_key: 'api-key-1',
org_uid: 'org-1',
},
{
uid: 'stack-2',
name: 'Another Stack',
master_locale: 'en-us',
api_key: 'api-key-2',
org_uid: 'org-1',
},
];
(mockInquirer.prompt as jest.Mock)
.mockResolvedValueOnce({
choice: false, // Existing stack
})
.mockResolvedValueOnce({
uid: 'stack-2',
});
const result = await interactive.inquireStack(stacks);
expect(result.isNew).toBe(false);
expect(result.name).toBe('Another Stack');
expect(result.uid).toBe('stack-2');
expect(result.api_key).toBe('api-key-2');
});
it('should prompt for stack name when creating new stack without stackName', async () => {
const stacks: Stack[] = [];
(mockInquirer.prompt as jest.Mock).mockResolvedValue({
name: 'User Entered Stack',
});
const result = await interactive.inquireStack(stacks);
expect(result.isNew).toBe(true);
expect(result.name).toBe('User Entered Stack');
expect(mockInquirer.prompt).toHaveBeenCalledWith([
expect.objectContaining({
type: 'input',
name: 'name',
message: 'Enter a stack name',
}),
]);
});
it('should validate stack name input', async () => {
const stacks: Stack[] = [];
const validateFn = jest.fn();
(mockInquirer.prompt as jest.Mock).mockImplementation((questions: any) => {
const question = questions[0];
if (question.validate) {
expect(question.validate('')).toBe('Required');
expect(question.validate(' ')).toBe('Required');
expect(question.validate('Valid Name')).toBe(true);
}
return Promise.resolve({ name: 'Valid Name' });
});
await interactive.inquireStack(stacks);
expect(mockInquirer.prompt).toHaveBeenCalled();
});
it('should trim stack name input', async () => {
const stacks: Stack[] = [];
const stackName = ' Trimmed Stack Name ';
const result = await interactive.inquireStack(stacks, stackName);
expect(result.name).toBe('Trimmed Stack Name');
});
it('should sort stack choices alphabetically', async () => {
const stacks: Stack[] = [
{
uid: 'stack-3',
name: 'Zebra Stack',
master_locale: 'en-us',
api_key: 'api-key-3',
org_uid: 'org-1',
},
{
uid: 'stack-1',
name: 'Alpha Stack',
master_locale: 'en-us',
api_key: 'api-key-1',
org_uid: 'org-1',
},
{
uid: 'stack-2',
name: 'Beta Stack',
master_locale: 'en-us',
api_key: 'api-key-2',
org_uid: 'org-1',
},
];
(mockInquirer.prompt as jest.Mock)
.mockResolvedValueOnce({
choice: false,
})
.mockResolvedValueOnce({
uid: 'stack-1',
});
await interactive.inquireStack(stacks);
const promptCall = (mockInquirer.prompt as jest.Mock).mock.calls[1][0][0];
const choices = promptCall.choices;
expect(choices[0].name).toBe('Alpha Stack');
expect(choices[1].name).toBe('Beta Stack');
expect(choices[2].name).toBe('Zebra Stack');
});
it('should handle empty stacks array with stackName', async () => {
const stacks: Stack[] = [];
const stackName = 'New Stack';
const result = await interactive.inquireStack(stacks, stackName);
expect(result.isNew).toBe(true);
expect(result.name).toBe('New Stack');
});
});
});