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
|
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper';
import * as actions from '~/ide/stores/modules/file_templates/actions';
import * as types from '~/ide/stores/modules/file_templates/mutation_types';
import createState from '~/ide/stores/modules/file_templates/state';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_INTERNAL_SERVER_ERROR } from '~/lib/utils/http_status';
describe('IDE file templates actions', () => {
let state;
let mock;
beforeEach(() => {
state = createState();
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
describe('requestTemplateTypes', () => {
it('commits REQUEST_TEMPLATE_TYPES', () => {
return testAction(
actions.requestTemplateTypes,
null,
state,
[{ type: types.REQUEST_TEMPLATE_TYPES }],
[],
);
});
});
describe('receiveTemplateTypesError', () => {
it('commits RECEIVE_TEMPLATE_TYPES_ERROR and dispatches setErrorMessage', () => {
return testAction(
actions.receiveTemplateTypesError,
null,
state,
[{ type: types.RECEIVE_TEMPLATE_TYPES_ERROR }],
[
{
type: 'setErrorMessage',
payload: {
action: expect.any(Function),
actionText: 'Please try again',
text: 'Error loading template types.',
},
},
],
);
});
});
describe('receiveTemplateTypesSuccess', () => {
it('commits RECEIVE_TEMPLATE_TYPES_SUCCESS', () => {
return testAction(
actions.receiveTemplateTypesSuccess,
'test',
state,
[{ type: types.RECEIVE_TEMPLATE_TYPES_SUCCESS, payload: 'test' }],
[],
);
});
});
describe('fetchTemplateTypes', () => {
describe('success', () => {
const pages = [[{ name: 'MIT' }], [{ name: 'Apache' }], [{ name: 'CC' }]];
beforeEach(() => {
mock.onGet(/api\/(.*)\/templates\/licenses/).reply(({ params }) => {
const pageNum = params.page;
const page = pages[pageNum - 1];
const hasNextPage = pageNum < pages.length;
return [200, page, hasNextPage ? { 'X-NEXT-PAGE': pageNum + 1 } : {}];
});
});
it('rejects if selectedTemplateType is empty', async () => {
const dispatch = jest.fn().mockName('dispatch');
await expect(actions.fetchTemplateTypes({ dispatch, state })).rejects.toBeUndefined();
expect(dispatch).not.toHaveBeenCalled();
});
it('dispatches actions', () => {
state.selectedTemplateType = { key: 'licenses' };
return testAction(
actions.fetchTemplateTypes,
null,
state,
[],
[
{ type: 'requestTemplateTypes' },
{ type: 'receiveTemplateTypesSuccess', payload: pages[0] },
{ type: 'receiveTemplateTypesSuccess', payload: pages[0].concat(pages[1]) },
{
type: 'receiveTemplateTypesSuccess',
payload: pages[0].concat(pages[1]).concat(pages[2]),
},
],
);
});
});
describe('error', () => {
beforeEach(() => {
mock.onGet(/api\/(.*)\/templates\/licenses/).replyOnce(HTTP_STATUS_INTERNAL_SERVER_ERROR);
});
it('dispatches actions', () => {
state.selectedTemplateType = { key: 'licenses' };
return testAction(
actions.fetchTemplateTypes,
null,
state,
[],
[{ type: 'requestTemplateTypes' }, { type: 'receiveTemplateTypesError' }],
);
});
});
});
describe('setSelectedTemplateType', () => {
it('commits SET_SELECTED_TEMPLATE_TYPE', () => {
const commit = jest.fn().mockName('commit');
const options = {
commit,
dispatch() {},
rootGetters: { activeFile: { name: 'test', prevPath: '' } },
};
actions.setSelectedTemplateType(options, { name: 'test' });
expect(commit).toHaveBeenCalledWith(types.SET_SELECTED_TEMPLATE_TYPE, { name: 'test' });
});
it('dispatches discardFileChanges if prevPath matches templates name', () => {
const dispatch = jest.fn().mockName('dispatch');
const options = {
commit() {},
dispatch,
rootGetters: { activeFile: { name: 'test', path: 'test', prevPath: 'test' } },
};
actions.setSelectedTemplateType(options, { name: 'test' });
expect(dispatch).toHaveBeenCalledWith('discardFileChanges', 'test', { root: true });
});
it('dispatches renameEntry if file name doesnt match', () => {
const dispatch = jest.fn().mockName('dispatch');
const options = {
commit() {},
dispatch,
rootGetters: { activeFile: { name: 'oldtest', path: 'oldtest', prevPath: '' } },
};
actions.setSelectedTemplateType(options, { name: 'test' });
expect(dispatch).toHaveBeenCalledWith(
'renameEntry',
{ path: 'oldtest', name: 'test' },
{ root: true },
);
});
});
describe('receiveTemplateError', () => {
it('dispatches setErrorMessage', () => {
return testAction(
actions.receiveTemplateError,
'test',
state,
[],
[
{
type: 'setErrorMessage',
payload: {
action: expect.any(Function),
actionText: 'Please try again',
text: 'Error loading template.',
actionPayload: 'test',
},
},
],
);
});
});
describe('fetchTemplate', () => {
describe('success', () => {
beforeEach(() => {
mock
.onGet(/api\/(.*)\/templates\/licenses\/mit/)
.replyOnce(200, { content: 'MIT content' });
mock
.onGet(/api\/(.*)\/templates\/licenses\/testing/)
.replyOnce(200, { content: 'testing content' });
});
it('dispatches setFileTemplate if template already has content', () => {
const template = { content: 'already has content' };
return testAction(
actions.fetchTemplate,
template,
state,
[],
[{ type: 'setFileTemplate', payload: template }],
);
});
it('dispatches success', () => {
const template = { key: 'mit' };
state.selectedTemplateType = { key: 'licenses' };
return testAction(
actions.fetchTemplate,
template,
state,
[],
[{ type: 'setFileTemplate', payload: { content: 'MIT content' } }],
);
});
it('dispatches success and uses name key for API call', () => {
const template = { name: 'testing' };
state.selectedTemplateType = { key: 'licenses' };
return testAction(
actions.fetchTemplate,
template,
state,
[],
[{ type: 'setFileTemplate', payload: { content: 'testing content' } }],
);
});
});
describe('error', () => {
beforeEach(() => {
mock
.onGet(/api\/(.*)\/templates\/licenses\/mit/)
.replyOnce(HTTP_STATUS_INTERNAL_SERVER_ERROR);
});
it('dispatches error', () => {
const template = { name: 'testing' };
state.selectedTemplateType = { key: 'licenses' };
return testAction(
actions.fetchTemplate,
template,
state,
[],
[{ type: 'receiveTemplateError', payload: template }],
);
});
});
});
describe('setFileTemplate', () => {
it('dispatches changeFileContent', () => {
const dispatch = jest.fn().mockName('dispatch');
const commit = jest.fn().mockName('commit');
const rootGetters = { activeFile: { path: 'test' } };
actions.setFileTemplate({ dispatch, commit, rootGetters }, { content: 'content' });
expect(dispatch).toHaveBeenCalledWith(
'changeFileContent',
{ path: 'test', content: 'content' },
{ root: true },
);
});
it('commits SET_UPDATE_SUCCESS', () => {
const dispatch = jest.fn().mockName('dispatch');
const commit = jest.fn().mockName('commit');
const rootGetters = { activeFile: { path: 'test' } };
actions.setFileTemplate({ dispatch, commit, rootGetters }, { content: 'content' });
expect(commit).toHaveBeenCalledWith('SET_UPDATE_SUCCESS', true);
});
});
describe('undoFileTemplate', () => {
it('dispatches changeFileContent', () => {
const dispatch = jest.fn().mockName('dispatch');
const commit = jest.fn().mockName('commit');
const rootGetters = { activeFile: { path: 'test', raw: 'raw content' } };
actions.undoFileTemplate({ dispatch, commit, rootGetters });
expect(dispatch).toHaveBeenCalledWith(
'changeFileContent',
{ path: 'test', content: 'raw content' },
{ root: true },
);
});
it('commits SET_UPDATE_SUCCESS', () => {
const dispatch = jest.fn().mockName('dispatch');
const commit = jest.fn().mockName('commit');
const rootGetters = { activeFile: { path: 'test', raw: 'raw content' } };
actions.undoFileTemplate({ dispatch, commit, rootGetters });
expect(commit).toHaveBeenCalledWith('SET_UPDATE_SUCCESS', false);
});
it('dispatches discardFileChanges if file has prevPath', () => {
const dispatch = jest.fn().mockName('dispatch');
const rootGetters = { activeFile: { path: 'test', prevPath: 'newtest', raw: 'raw content' } };
actions.undoFileTemplate({ dispatch, commit() {}, rootGetters });
expect(dispatch).toHaveBeenCalledWith('discardFileChanges', 'test', { root: true });
});
});
});
|