-
Notifications
You must be signed in to change notification settings - Fork 756
Expand file tree
/
Copy patheditor.spec.tsx
More file actions
115 lines (87 loc) · 3.03 KB
/
editor.spec.tsx
File metadata and controls
115 lines (87 loc) · 3.03 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
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { EditorId, MAIN_JS } from '../../src/interfaces';
import { Editor } from '../../src/renderer/components/editor';
import { AppState } from '../../src/renderer/state';
import { renderClassComponentWithInstanceRef } from '../test-utils/renderClassComponentWithInstanceRef';
type DidMount = () => void;
describe('Editor component', () => {
let store: AppState;
let monaco: any;
beforeEach(() => {
({ monaco } = window);
({ state: store } = window.app);
});
function createEditor(id: EditorId, didMount: DidMount = vi.fn()) {
return renderClassComponentWithInstanceRef(Editor, {
appState: store,
editorDidMount: didMount,
id,
monaco,
monacoOptions: {},
setFocused: () => undefined,
});
}
async function initializeEditorMosaic(id: EditorId) {
await store.editorMosaic.set({ [id]: '// content' });
}
it('renders the editor container', async () => {
const id = MAIN_JS;
await initializeEditorMosaic(id);
const { renderResult } = createEditor(id);
expect(renderResult.getByTestId('editorContainer')).toBeInTheDocument();
});
describe('correctly sets the language', () => {
it.each([
['for javascript', 'file.js' as EditorId, 'javascript'],
['for html', 'file.html' as EditorId, 'html'],
['for css', 'file.css' as EditorId, 'css'],
])('%s', (_: unknown, filename: EditorId, language: string) => {
initializeEditorMosaic(filename);
const { instance } = createEditor(filename);
expect(instance.language).toBe(language);
});
});
it('denies updates', () => {
const id = MAIN_JS;
initializeEditorMosaic(id);
const { instance } = createEditor(id);
expect(instance.shouldComponentUpdate()).toBe(false);
});
describe('initMonaco()', () => {
it('calls editorMosaic.addEditor', async () => {
const id = MAIN_JS;
const { editorMosaic } = store;
await editorMosaic.set({ [id]: '// content' });
const addEditorSpy = vi.spyOn(editorMosaic, 'addEditor');
const didMount = vi.fn();
createEditor(id, didMount);
await vi.waitFor(() => didMount.mock.calls.length > 0);
expect(addEditorSpy).toHaveBeenCalledWith(id, expect.anything());
});
it('sets up a listener on focused text editor', async () => {
const id = MAIN_JS;
initializeEditorMosaic(id);
createEditor(id);
expect(monaco.latestEditor.onDidFocusEditorText).toHaveBeenCalled();
});
});
it('componentWillUnmount() attempts to dispose the editor', async () => {
const id = MAIN_JS;
initializeEditorMosaic(id);
const {
renderResult: { unmount },
} = createEditor(id);
unmount();
expect(monaco.latestEditor.dispose).toHaveBeenCalled();
});
it('focus editor file', async () => {
const id = MAIN_JS;
initializeEditorMosaic(id);
const {
instance,
renderResult: { unmount },
} = createEditor(id);
unmount();
expect(instance.props.id).toBe(id);
});
});