-
Notifications
You must be signed in to change notification settings - Fork 752
Expand file tree
/
Copy pathsidebar-file-tree-spec.tsx
More file actions
338 lines (249 loc) · 11.4 KB
/
sidebar-file-tree-spec.tsx
File metadata and controls
338 lines (249 loc) · 11.4 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
336
337
338
import * as React from 'react';
import { shallow } from 'enzyme';
import {
EditorValues,
MAIN_CJS,
MAIN_JS,
MAIN_MJS,
PACKAGE_NAME,
} from '../../../src/interfaces';
import { Editors } from '../../../src/renderer/components/editors';
import { SidebarFileTree } from '../../../src/renderer/components/sidebar-file-tree';
import {
EditorMosaic,
EditorPresence,
} from '../../../src/renderer/editor-mosaic';
import { AppState } from '../../../src/renderer/state';
import { createEditorValues } from '../../mocks/editor-values';
import { AppMock, StateMock } from '../../mocks/mocks';
describe('SidebarFileTree component', () => {
let store: AppState;
let editorMosaic: EditorMosaic;
let editorValues: EditorValues;
let stateMock: StateMock;
beforeEach(() => {
({ state: stateMock } = window.app as unknown as AppMock);
store = {} as unknown as AppState;
editorValues = createEditorValues();
editorMosaic = new EditorMosaic();
editorMosaic.set(editorValues);
(store as unknown as StateMock).editorMosaic = editorMosaic;
stateMock.editorMosaic = editorMosaic;
});
it('renders', () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
expect(wrapper).toMatchSnapshot();
});
it('reflects the visibility state of all icons', () => {
editorMosaic.hide('index.html');
const wrapper = shallow(<SidebarFileTree appState={store} />);
// snapshot has an 'eye-off' icon
expect(wrapper).toMatchSnapshot();
});
it('can bring up the Add File input', () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
instance.setState({ action: 'add' });
// snapshot has the input rendered
expect(wrapper).toMatchSnapshot();
});
it('can toggle editor visibility', () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
instance.toggleVisibility('index.html');
expect(editorMosaic.files.get('index.html')).toBe(EditorPresence.Hidden);
});
it('can create new editors', () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
expect(editorMosaic.files.get('tester.js')).toBe(undefined);
instance.createEditor('tester.js');
expect(editorMosaic.files.get('tester.js')).toBe(EditorPresence.Pending);
});
it('can delete editors', () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
expect(editorMosaic.files.get('index.html')).toBe(EditorPresence.Pending);
instance.removeEditor('index.html');
expect(editorMosaic.files.get('index.html')).toBe(undefined);
});
it('can rename editors', async () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
const EDITOR_NAME = 'index.html';
const EDITOR_NEW_NAME = 'new_index.html';
store.showInputDialog = jest.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
expect(editorMosaic.files.get(EDITOR_NAME)).toBe(EditorPresence.Pending);
await instance.renameEditor(EDITOR_NAME);
expect(editorMosaic.files.get(EDITOR_NAME)).toBe(undefined);
expect(editorMosaic.files.get(EDITOR_NEW_NAME)).toBe(
EditorPresence.Pending,
);
});
it('can rename one main entry point file to another main entry point file', async () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
const EDITOR_NAME = MAIN_JS;
const EDITOR_NEW_NAME = MAIN_CJS;
store.showInputDialog = jest.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
await instance.renameEditor(EDITOR_NAME);
expect(editorMosaic.files.get(EDITOR_NAME)).toBe(undefined);
expect(editorMosaic.files.get(EDITOR_NEW_NAME)).toBe(
EditorPresence.Pending,
);
});
it('fails if trying to rename an editor to package(-lock).json', async () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
const EDITOR_NAME = 'index.html';
const EDITOR_NEW_NAME = PACKAGE_NAME;
store.showInputDialog = jest.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
await instance.renameEditor(EDITOR_NAME);
expect(store.showErrorDialog).toHaveBeenCalledWith(
`Cannot add ${PACKAGE_NAME} or package-lock.json as custom files`,
);
expect(editorMosaic.files.get(EDITOR_NAME)).toBe(EditorPresence.Pending);
});
it('fails if trying to rename an editor to an unsupported name', async () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
const EDITOR_NAME = 'index.html';
const EDITOR_NEW_NAME = 'data.txt';
store.showInputDialog = jest.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
await instance.renameEditor(EDITOR_NAME);
expect(store.showErrorDialog).toHaveBeenCalledWith(
`Invalid filename "${EDITOR_NEW_NAME}": Must be a file ending in .cjs, .js, .mjs, .html, .css, or .json`,
);
expect(editorMosaic.files.get(EDITOR_NAME)).toBe(EditorPresence.Pending);
});
it('fails if trying to rename an editor to an existing name', async () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
const EXISTED_NAME = 'styles.css';
const TO_BE_NAMED = 'index.html';
const EDITOR_NEW_NAME = EXISTED_NAME;
store.showInputDialog = jest.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
await instance.renameEditor(TO_BE_NAMED);
expect(store.showErrorDialog).toHaveBeenCalledWith(
`Cannot rename file to "${EDITOR_NEW_NAME}": File already exists`,
);
expect(editorMosaic.files.get(TO_BE_NAMED)).toBe(EditorPresence.Pending);
});
it('fails if trying to rename an editor to another main entry point file', async () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
const TO_BE_NAMED = 'index.html';
const EDITOR_NEW_NAME = MAIN_CJS;
store.showInputDialog = jest.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
await instance.renameEditor(TO_BE_NAMED);
expect(store.showErrorDialog).toHaveBeenCalledWith(
`Cannot rename file to "${EDITOR_NEW_NAME}": Main entry point ${MAIN_JS} exists`,
);
expect(editorMosaic.files.get(TO_BE_NAMED)).toBe(EditorPresence.Pending);
});
it('can set a new main entry point file', () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
// Add MAIN_CJS to the mosaic
// NOTE: Using direct map manipulation for test setup only.
// In production code, files would be added through proper channels.
editorMosaic.files.set(MAIN_CJS, EditorPresence.Visible);
instance.setMainEntryPoint(MAIN_JS);
expect(instance.getMainEntryPoint()).toBe(MAIN_JS);
expect(editorMosaic.mainEntryPointFile()).toBe(MAIN_JS);
instance.setMainEntryPoint(MAIN_CJS);
expect(instance.getMainEntryPoint()).toBe(MAIN_CJS);
expect(editorMosaic.mainEntryPointFile()).toBe(MAIN_CJS);
});
it('fails when trying to set an invalid file as main entry point', () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
const REGULAR_FILE = 'index.html';
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
instance.setMainEntryPoint(REGULAR_FILE);
expect(store.showErrorDialog).toHaveBeenCalledWith(
`Cannot set main entry point to "${REGULAR_FILE}": Not a valid main entry point file`,
);
});
it('fails when trying to set a non-existent file as main entry point', () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
const NON_EXISTENT_FILE = 'non-existent-file.js';
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
instance.setMainEntryPoint(NON_EXISTENT_FILE);
expect(store.showErrorDialog).toHaveBeenCalledWith(
`Cannot set main entry point to "${NON_EXISTENT_FILE}": File does not exist`,
);
});
it('selects an available alternate main file after renaming active main file', async () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
// Add MAIN_CJS, MAIN_MJS to the mosaic
// NOTE: Using set() for test setup only.
// In production code, files would be added through proper channels.
editorMosaic.set({
...createEditorValues(),
[MAIN_CJS]: '// main.cjs content',
[MAIN_MJS]: '// main.mjs content',
});
instance.setMainEntryPoint(MAIN_JS);
expect(instance.getMainEntryPoint()).toBe(MAIN_JS);
store.showInputDialog = jest
.fn()
.mockResolvedValueOnce('not-a-main-file.js');
await instance.renameEditor(MAIN_JS);
const newMainFile = instance.getMainEntryPoint();
expect(newMainFile).not.toBe(MAIN_JS);
expect([MAIN_CJS, MAIN_MJS]).toContain(newMainFile);
});
it('sets main entry point to undefined when no main files exist after renaming', async () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
instance.setMainEntryPoint(MAIN_JS);
expect(instance.getMainEntryPoint()).toBe(MAIN_JS);
store.showInputDialog = jest
.fn()
.mockResolvedValueOnce('not-a-main-file.js');
await instance.renameEditor(MAIN_JS);
expect(instance.getMainEntryPoint()).toBeUndefined();
});
it('can reset the editor layout', () => {
const wrapper = shallow(<SidebarFileTree appState={store} />);
const instance: any = wrapper.instance();
editorMosaic.resetLayout = jest.fn();
instance.resetLayout();
expect(editorMosaic.resetLayout).toHaveBeenCalledTimes(1);
});
it('file is visible, click files tree, focus file content', async () => {
const sidebarFileTree = shallow(<SidebarFileTree appState={store} />);
const editors = shallow(
<Editors appState={stateMock as unknown as AppState} />,
);
const sidebarFileTreeInstance: any = sidebarFileTree.instance();
const editorsInstance: any = editors.instance();
sidebarFileTreeInstance.setFocusedFile('index.html');
setTimeout(() => {
expect(editorMosaic.files.get('index.html')).toBe(EditorPresence.Visible);
expect(editorsInstance.state.focused).toBe('index.html');
});
});
it('file is hidden, click files tree, make file visible and focus file content', function () {
const sidebarFileTree = shallow(<SidebarFileTree appState={store} />);
const editors = shallow(
<Editors appState={stateMock as unknown as AppState} />,
);
const sidebarFileTreeInstance: any = sidebarFileTree.instance();
const editorsInstance: any = editors.instance();
sidebarFileTreeInstance.toggleVisibility('index.html');
expect(editorMosaic.files.get('index.html')).toBe(EditorPresence.Hidden);
sidebarFileTreeInstance.setFocusedFile('index.html');
setTimeout(() => {
expect(editorMosaic.files.get('index.html')).toBe(EditorPresence.Visible);
expect(editorsInstance.state.focused).toBe('index.html');
});
});
});