Skip to content

Commit 9042079

Browse files
Merge pull request #35 from contentstack/CL-1605
Cl 1605
2 parents e938c40 + 8482b87 commit 9042079

File tree

5 files changed

+380
-30
lines changed

5 files changed

+380
-30
lines changed

src/adapters/base-class.test.ts

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
import BaseClass from './base-class';
2+
import { cliux as ux, ContentstackClient } from '@contentstack/cli-utilities';
3+
import config from '../config';
4+
import exp from 'constants';
5+
6+
jest.mock('@contentstack/cli-utilities', () => ({
7+
cliux: {
8+
inquire: jest.fn(),
9+
table: jest.fn(),
10+
},
11+
}));
12+
13+
describe('BaseClass', () => {
14+
let baseClass: BaseClass;
15+
let logMock: jest.Mock;
16+
let exitMock: jest.Mock;
17+
let managementSdkMock: jest.Mocked<ContentstackClient>;
18+
19+
beforeEach(() => {
20+
logMock = jest.fn();
21+
exitMock = jest.fn();
22+
});
23+
24+
afterEach(() => {
25+
jest.clearAllMocks();
26+
});
27+
28+
describe('handleEnvImportFlow', () => {
29+
beforeEach(() => {
30+
baseClass = new BaseClass({
31+
log: logMock,
32+
exit: exitMock,
33+
config: config.variablePreparationTypeOptions,
34+
} as any);
35+
});
36+
it('should exit if no options are selected', async () => {
37+
(ux.inquire as jest.Mock).mockResolvedValueOnce([]);
38+
39+
await baseClass.handleEnvImportFlow();
40+
41+
expect(logMock).toHaveBeenCalledWith(
42+
'Please select at least one option by pressing <space>, then press <enter> to proceed.',
43+
'error',
44+
);
45+
expect(exitMock).toHaveBeenCalledWith(1);
46+
});
47+
48+
it('should exit if "Skip adding environment variables" is selected with other options', async () => {
49+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
50+
(ux.inquire as jest.Mock).mockResolvedValueOnce([
51+
'Skip adding environment variables',
52+
'Import variables from a stack',
53+
]);
54+
55+
await baseClass.handleEnvImportFlow();
56+
57+
expect(logMock).toHaveBeenCalledWith(
58+
"The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options.",
59+
'error',
60+
);
61+
62+
expect(exitMock).toHaveBeenCalledWith(1);
63+
expect(importEnvFromStackMock).toHaveBeenCalled();
64+
});
65+
66+
it('should call importEnvFromStack if "Import variables from a stack" is selected', async () => {
67+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
68+
69+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from a stack']);
70+
71+
await baseClass.handleEnvImportFlow();
72+
73+
expect(importEnvFromStackMock).toHaveBeenCalled();
74+
});
75+
76+
it('should call promptForEnvValues if "Manually add custom variables to the list" is selected', async () => {
77+
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
78+
79+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Manually add custom variables to the list']);
80+
81+
await baseClass.handleEnvImportFlow();
82+
83+
expect(promptForEnvValuesMock).toHaveBeenCalled();
84+
});
85+
86+
it('should call importVariableFromLocalConfig if "Import variables from the .env.local file" is selected', async () => {
87+
const importVariableFromLocalConfigMock = jest
88+
.spyOn(baseClass, 'importVariableFromLocalConfig')
89+
.mockResolvedValueOnce();
90+
91+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from the .env.local file']);
92+
93+
await baseClass.handleEnvImportFlow();
94+
95+
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
96+
});
97+
98+
it('should set envVariables to an empty array if "Skip adding environment variables" is selected', async () => {
99+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Skip adding environment variables']);
100+
101+
await baseClass.handleEnvImportFlow();
102+
103+
expect(baseClass.envVariables).toEqual([]);
104+
expect(logMock).toHaveBeenCalledWith('Skipped adding environment variables.', 'info');
105+
});
106+
107+
it('should call importEnvFromStack and promptForEnvValues if "Import variables from a stack" and "Manually add custom variables to the list" both are selected.', async () => {
108+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
109+
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
110+
111+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from a stack', 'Manually add custom variables to the list']);
112+
113+
await baseClass.handleEnvImportFlow();
114+
115+
expect(importEnvFromStackMock).toHaveBeenCalled();
116+
expect(promptForEnvValuesMock).toHaveBeenCalled();
117+
expect(exitMock).not.toHaveBeenCalledWith(1);
118+
});
119+
120+
it('should call importVariableFromLocalConfig and importEnvFromStack if "Import variables from a stack" and "Import variables from the .env.local file" is selected', async () => {
121+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
122+
const importVariableFromLocalConfigMock = jest
123+
.spyOn(baseClass, 'importVariableFromLocalConfig')
124+
.mockResolvedValueOnce();
125+
(ux.inquire as jest.Mock).mockResolvedValueOnce([
126+
'Import variables from a stack',
127+
'Import variables from the .env.local file',
128+
]);
129+
130+
await baseClass.handleEnvImportFlow();
131+
132+
expect(importEnvFromStackMock).toHaveBeenCalled();
133+
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
134+
expect(exitMock).not.toHaveBeenCalledWith(1);
135+
});
136+
137+
it('should call promptForEnvValues and importVariableFromLocalConfig if "Manually add custom variables to the list" and "Import variables from the .env.local file" is selected', async () => {
138+
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
139+
const importVariableFromLocalConfigMock = jest
140+
.spyOn(baseClass, 'importVariableFromLocalConfig')
141+
.mockResolvedValueOnce();
142+
(ux.inquire as jest.Mock).mockResolvedValueOnce([
143+
'Manually add custom variables to the list',
144+
'Import variables from the .env.local file',
145+
]);
146+
147+
await baseClass.handleEnvImportFlow();
148+
149+
expect(promptForEnvValuesMock).toHaveBeenCalled();
150+
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
151+
expect(exitMock).not.toHaveBeenCalledWith(1);
152+
});
153+
154+
it('should call importEnvFromStack, promptForEnvValues and importVariableFromLocalConfig if all three options selected', async () => {
155+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
156+
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
157+
const importVariableFromLocalConfigMock = jest
158+
.spyOn(baseClass, 'importVariableFromLocalConfig')
159+
.mockResolvedValueOnce();
160+
(ux.inquire as jest.Mock).mockResolvedValueOnce([
161+
'Import variables from a stack',
162+
'Manually add custom variables to the list',
163+
'Import variables from the .env.local file',
164+
]);
165+
166+
await baseClass.handleEnvImportFlow();
167+
168+
expect(importEnvFromStackMock).toHaveBeenCalled();
169+
expect(promptForEnvValuesMock).toHaveBeenCalled();
170+
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
171+
expect(exitMock).not.toHaveBeenCalledWith(1);
172+
});
173+
174+
it('should call printAllVariables if envVariables has values', async () => {
175+
const expectedEnv = [{ key: 'API_KEY', value: '12345' }];
176+
const importVariableFromLocalConfigMock = jest
177+
.spyOn(baseClass, 'importVariableFromLocalConfig')
178+
.mockImplementationOnce(() => {
179+
baseClass.envVariables = expectedEnv;
180+
return Promise.resolve();
181+
});
182+
183+
const printAllVariablesMock = jest.spyOn(baseClass, 'printAllVariables').mockImplementation();
184+
185+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from the .env.local file']);
186+
187+
await baseClass.handleEnvImportFlow();
188+
189+
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
190+
expect(printAllVariablesMock).toHaveBeenCalled();
191+
expect(baseClass.envVariables).toEqual(expectedEnv);
192+
});
193+
});
194+
195+
describe('importVariableFromLocalConfig', () => {
196+
jest.mock('dotenv', () => ({
197+
config: jest.fn().mockReturnValueOnce({ parsed: null }),
198+
}));
199+
200+
beforeEach(() => {
201+
baseClass = new BaseClass({
202+
config: {
203+
projectBasePath: './baseClass',
204+
},
205+
log: logMock,
206+
exit: exitMock,
207+
} as any);
208+
});
209+
210+
afterEach(() => {
211+
jest.clearAllMocks();
212+
});
213+
214+
it('should log an error and exit if no .env.local file is found', async () => {
215+
jest.mock('dotenv', () => ({
216+
config: jest.fn().mockReturnValueOnce({ parsed: {} }),
217+
}));
218+
219+
await baseClass.importVariableFromLocalConfig();
220+
221+
expect(logMock).toHaveBeenCalledWith('No .env.local file found.', 'error');
222+
expect(exitMock).toHaveBeenCalledWith(1);
223+
});
224+
});
225+
226+
describe('selectStack', () => {
227+
beforeEach(() => {
228+
managementSdkMock = {
229+
stack: jest.fn().mockReturnThis(),
230+
query: jest.fn().mockReturnThis(),
231+
find: jest.fn(),
232+
} as unknown as jest.Mocked<ContentstackClient>;
233+
234+
baseClass = new BaseClass({
235+
log: logMock,
236+
exit: exitMock,
237+
managementSdk: managementSdkMock,
238+
config: {
239+
currentConfig: { organizationUid: 'org_uid' },
240+
},
241+
} as any);
242+
});
243+
244+
afterEach(() => {
245+
jest.clearAllMocks();
246+
});
247+
248+
it('should exit with error if no stacks are found', async () => {
249+
(managementSdkMock as any).find.mockResolvedValueOnce({ items: [] });
250+
exitMock.mockImplementation(() => {
251+
throw new Error('exit');
252+
});
253+
254+
await expect(baseClass.selectStack()).rejects.toThrow('exit');
255+
256+
expect(logMock).toHaveBeenCalledWith(
257+
'No stacks were found in your organization, or you do not have access to any. Please create a stack in the organization to proceed in the organization.',
258+
'error',
259+
);
260+
expect(exitMock).toHaveBeenCalledWith(1);
261+
});
262+
});
263+
264+
describe('selectDeliveryToken', () => {
265+
beforeEach(() => {
266+
managementSdkMock = {
267+
stack: jest.fn().mockReturnThis(),
268+
deliveryToken: jest.fn().mockReturnThis(),
269+
query: jest.fn().mockReturnThis(),
270+
find: jest.fn(),
271+
} as unknown as jest.Mocked<ContentstackClient>;
272+
273+
baseClass = new BaseClass({
274+
log: logMock,
275+
exit: exitMock,
276+
managementSdk: managementSdkMock,
277+
config: {
278+
selectedStack: { api_key: 'test_api_key' },
279+
},
280+
} as any);
281+
});
282+
283+
afterEach(() => {
284+
jest.clearAllMocks();
285+
});
286+
287+
it('should log an error and exit if no delivery tokens are found', async () => {
288+
(managementSdkMock as any).find.mockResolvedValueOnce({ items: [] });
289+
exitMock.mockImplementation(() => {
290+
throw new Error('exit');
291+
});
292+
293+
await expect(baseClass.selectDeliveryToken()).rejects.toThrow('exit');
294+
295+
expect(logMock).toHaveBeenCalledWith(
296+
'No delivery tokens were found in the selected stack. Please create a delivery token in the stack to continue.',
297+
'error',
298+
);
299+
expect(exitMock).toHaveBeenCalledWith(1);
300+
});
301+
});
302+
});

0 commit comments

Comments
 (0)