-
Notifications
You must be signed in to change notification settings - Fork 752
Expand file tree
/
Copy pathsidebar-file-tree.spec.tsx
More file actions
418 lines (330 loc) · 14.7 KB
/
sidebar-file-tree.spec.tsx
File metadata and controls
418 lines (330 loc) · 14.7 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import * as React from 'react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
EditorValues,
MAIN_CJS,
MAIN_JS,
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 '../../tests/mocks/editor-values';
import { AppMock, StateMock } from '../../tests/mocks/mocks';
describe('SidebarFileTree component', () => {
let store: AppState;
let editorMosaic: EditorMosaic;
let editorValues: EditorValues;
let stateMock: StateMock;
beforeEach(async () => {
({ state: stateMock } = window.app as unknown as AppMock);
store = {
showErrorDialog: vi.fn(),
} as unknown as AppState;
editorValues = createEditorValues();
editorMosaic = new EditorMosaic();
await editorMosaic.set(editorValues);
(store as unknown as StateMock).editorMosaic = editorMosaic;
stateMock.editorMosaic = editorMosaic;
});
afterEach(() => {
vi.useRealTimers();
});
it('reflects the visibility state of all icons', () => {
editorMosaic.hide('index.html');
const { container } = render(<SidebarFileTree appState={store} />);
// Check that an 'eye-off' icon is present for the hidden file
const eyeOffIcon = container.querySelector('button .bp3-icon-eye-off');
expect(eyeOffIcon).toBeInTheDocument();
});
it('can bring up the Add File input', async () => {
const user = userEvent.setup();
const { container } = render(<SidebarFileTree appState={store} />);
// Click the "Add New File" button (by icon)
const addButton = container.querySelector(
'button .bp3-icon-add',
)?.parentElement;
expect(addButton).toBeInTheDocument();
await user.click(addButton!);
// Input should now be visible
const input = container.querySelector('#new-file-input');
expect(input).toBeInTheDocument();
});
it('can toggle editor visibility', async () => {
const user = userEvent.setup();
const { container } = render(<SidebarFileTree appState={store} />);
// Find and click the visibility toggle button (eye icon) for the first file
const visibilityButtons = container.querySelectorAll(
'button .bp3-icon-eye-open, button .bp3-icon-eye-off',
);
const firstButton = visibilityButtons[0]?.parentElement;
await user.click(firstButton!);
expect(editorMosaic.files.get('index.html')).toBe(EditorPresence.Hidden);
});
it('can create new editors', async () => {
const user = userEvent.setup();
const { container } = render(<SidebarFileTree appState={store} />);
expect(editorMosaic.files.get('tester.js')).toBe(undefined);
// Click the "Add New File" button (by icon)
const addButton = container.querySelector(
'button .bp3-icon-add',
)?.parentElement;
await user.click(addButton!);
// Type the filename and press Enter
const input = container.querySelector(
'#new-file-input',
) as HTMLInputElement;
await user.type(input, 'tester.js{Enter}');
expect(editorMosaic.files.get('tester.js')).toBe(EditorPresence.Pending);
});
it('fails to create new editors (file name inside subdir)', async () => {
const user = userEvent.setup();
const { container } = render(<SidebarFileTree appState={store} />);
const EDITOR_NEW_NAME = 'subdir/tester.js';
expect(editorMosaic.files.get(EDITOR_NEW_NAME)).toBe(undefined);
// Click the "Add New File" button (by icon)
const addButton = container.querySelector(
'button .bp3-icon-add',
)?.parentElement;
await user.click(addButton!);
// Type the filename and press Enter
const input = container.querySelector(
'#new-file-input',
) as HTMLInputElement;
await user.type(input, `${EDITOR_NEW_NAME}{Enter}`);
// Wait for error dialog to be called
await waitFor(() => {
console.log('store.showErrorDialog: ', store.showErrorDialog);
expect(store.showErrorDialog).toHaveBeenCalledWith(
`Invalid filename "${EDITOR_NEW_NAME}": inside subdir not supported yet`,
);
});
});
it('can delete editors', async () => {
const user = userEvent.setup();
const { container } = render(<SidebarFileTree appState={store} />);
expect(editorMosaic.files.get('index.html')).toBe(EditorPresence.Pending);
// Right-click on index.html to open context menu
const fileLabel = Array.from(container.querySelectorAll('.pointer')).find(
(el) => el.textContent === 'index.html',
) as HTMLElement;
expect(fileLabel).toBeInTheDocument();
fireEvent.contextMenu(fileLabel);
// Click the "Delete" menu item
const deleteItem = await screen.findByText('Delete');
await user.click(deleteItem);
expect(editorMosaic.files.get('index.html')).toBe(undefined);
});
it('can rename editors', async () => {
const user = userEvent.setup();
const EDITOR_NAME = 'index.html';
const EDITOR_NEW_NAME = 'new_index.html';
store.showInputDialog = vi.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
const { container } = render(<SidebarFileTree appState={store} />);
expect(editorMosaic.files.get(EDITOR_NAME)).toBe(EditorPresence.Pending);
// Right-click on index.html to open context menu
const fileLabel = Array.from(container.querySelectorAll('.pointer')).find(
(el) => el.textContent === EDITOR_NAME,
) as HTMLElement;
fireEvent.contextMenu(fileLabel);
// Click the "Rename" menu item
const renameItem = await screen.findByText('Rename');
await user.click(renameItem);
// Wait for the rename to complete
await waitFor(() => {
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 user = userEvent.setup();
const EDITOR_NAME = MAIN_JS;
const EDITOR_NEW_NAME = MAIN_CJS;
store.showInputDialog = vi.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
const { container } = render(<SidebarFileTree appState={store} />);
// Right-click on the main entry point file to open context menu
const fileLabel = Array.from(container.querySelectorAll('.pointer')).find(
(el) => el.textContent === EDITOR_NAME,
) as HTMLElement;
await user.pointer({ keys: '[MouseRight>]', target: fileLabel });
// Click the "Rename" menu item
const renameItem = await screen.findByText('Rename');
await user.click(renameItem);
// Wait for the rename to complete
await waitFor(() => {
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 user = userEvent.setup();
const EDITOR_NAME = 'index.html';
const EDITOR_NEW_NAME = PACKAGE_NAME;
store.showInputDialog = vi.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
store.showErrorDialog = vi.fn().mockResolvedValueOnce(true);
const { container } = render(<SidebarFileTree appState={store} />);
// Right-click on index.html to open context menu
const fileLabel = Array.from(container.querySelectorAll('.pointer')).find(
(el) => el.textContent === EDITOR_NAME,
) as HTMLElement;
fireEvent.contextMenu(fileLabel);
// Click the "Rename" menu item
const renameItem = await screen.findByText('Rename');
await user.click(renameItem);
// Wait for error dialog to be called
await waitFor(() => {
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 user = userEvent.setup();
const EDITOR_NAME = 'index.html';
const EDITOR_NEW_NAME = 'data.txt';
store.showInputDialog = vi.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
store.showErrorDialog = vi.fn().mockResolvedValueOnce(true);
const { container } = render(<SidebarFileTree appState={store} />);
// Right-click on index.html to open context menu
const fileLabel = Array.from(container.querySelectorAll('.pointer')).find(
(el) => el.textContent === EDITOR_NAME,
) as HTMLElement;
fireEvent.contextMenu(fileLabel);
// Click the "Rename" menu item
const renameItem = await screen.findByText('Rename');
await user.click(renameItem);
// Wait for error dialog to be called
await waitFor(() => {
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 unsupported name (file inside subdir)', async () => {
const user = userEvent.setup();
const EDITOR_NAME = 'index.html';
const EDITOR_NEW_NAME = 'msg/data.json';
store.showInputDialog = vi.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
store.showErrorDialog = vi.fn().mockResolvedValueOnce(true);
const { container } = render(<SidebarFileTree appState={store} />);
// Right-click on index.html to open context menu
const fileLabel = Array.from(container.querySelectorAll('.pointer')).find(
(el) => el.textContent === EDITOR_NAME,
) as HTMLElement;
fireEvent.contextMenu(fileLabel);
// Click the "Rename" menu item
const renameItem = await screen.findByText('Rename');
await user.click(renameItem);
// Wait for error dialog to be called
await waitFor(() => {
expect(store.showErrorDialog).toHaveBeenCalledWith(
`Invalid filename "${EDITOR_NEW_NAME}": inside subdir not supported yet`,
);
});
expect(editorMosaic.files.get(EDITOR_NAME)).toBe(EditorPresence.Pending);
});
it('fails if trying to rename an editor to an existing name', async () => {
const user = userEvent.setup();
const EXISTED_NAME = 'styles.css';
const TO_BE_NAMED = 'index.html';
const EDITOR_NEW_NAME = EXISTED_NAME;
store.showInputDialog = vi.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
store.showErrorDialog = vi.fn().mockResolvedValueOnce(true);
const { container } = render(<SidebarFileTree appState={store} />);
// Right-click on index.html to open context menu
const fileLabel = Array.from(container.querySelectorAll('.pointer')).find(
(el) => el.textContent === TO_BE_NAMED,
) as HTMLElement;
fireEvent.contextMenu(fileLabel);
// Click the "Rename" menu item
const renameItem = await screen.findByText('Rename');
await user.click(renameItem);
// Wait for error dialog to be called
await waitFor(() => {
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 user = userEvent.setup();
const TO_BE_NAMED = 'index.html';
const EDITOR_NEW_NAME = MAIN_CJS;
store.showInputDialog = vi.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
store.showErrorDialog = vi.fn().mockResolvedValueOnce(true);
const { container } = render(<SidebarFileTree appState={store} />);
// Right-click on index.html to open context menu
const fileLabel = Array.from(container.querySelectorAll('.pointer')).find(
(el) => el.textContent === TO_BE_NAMED,
) as HTMLElement;
fireEvent.contextMenu(fileLabel);
// Click the "Rename" menu item
const renameItem = await screen.findByText('Rename');
await user.click(renameItem);
// Wait for error dialog to be called
await waitFor(() => {
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 reset the editor layout', async () => {
const user = userEvent.setup();
editorMosaic.resetLayout = vi.fn();
const { container } = render(<SidebarFileTree appState={store} />);
// Click the "Reset Layout" button (by icon)
const resetButton = container.querySelector(
'button .bp3-icon-grid-view',
)?.parentElement;
await user.click(resetButton!);
expect(editorMosaic.resetLayout).toHaveBeenCalledTimes(1);
});
it('file is visible, click files tree, focus file content', async () => {
const user = userEvent.setup();
const { container } = render(<SidebarFileTree appState={store} />);
render(<Editors appState={stateMock as unknown as AppState} />);
// Click on index.html to focus it
const fileLabel = Array.from(container.querySelectorAll('.pointer')).find(
(el) => el.textContent === 'index.html',
) as HTMLElement;
await user.click(fileLabel);
// Wait for the file to become visible and focused
await waitFor(() => {
expect(editorMosaic.files.get('index.html')).toBe(EditorPresence.Visible);
expect(editorMosaic.focusedFile).toBe('index.html');
});
});
it('file is hidden, click files tree, make file visible and focus file content', async () => {
const user = userEvent.setup();
const { container } = render(<SidebarFileTree appState={store} />);
render(<Editors appState={stateMock as unknown as AppState} />);
// Hide the file first
const visibilityButtons = container.querySelectorAll(
'button .bp3-icon-eye-open, button .bp3-icon-eye-off',
);
const firstButton = visibilityButtons[0]?.parentElement;
await user.click(firstButton!);
expect(editorMosaic.files.get('index.html')).toBe(EditorPresence.Hidden);
// Click on index.html to focus it (should also make it visible)
const fileLabel = Array.from(container.querySelectorAll('.pointer')).find(
(el) => el.textContent === 'index.html',
) as HTMLElement;
await user.click(fileLabel);
// Wait for the file to become visible and focused
await waitFor(() => {
expect(editorMosaic.files.get('index.html')).toBe(EditorPresence.Visible);
expect(editorMosaic.focusedFile).toBe('index.html');
});
});
});