1
- import * as React from 'react' ;
2
-
3
- import { mount , shallow } from 'enzyme' ;
4
- import { MosaicWindowProps } from 'react-mosaic-component' ;
1
+ import { MosaicNode } from 'react-mosaic-component' ;
5
2
6
3
import { EditorId , EditorValues , MAIN_JS } from '../../src/interfaces' ;
7
4
import { App } from '../../src/renderer/app' ;
@@ -10,26 +7,24 @@ import { Editor, EditorMosaic } from '../../src/renderer/editor-mosaic';
10
7
import { AppState } from '../../src/renderer/state' ;
11
8
import {
12
9
MonacoEditorMock ,
13
- MonacoMock ,
14
10
StateMock ,
15
11
createEditorValues ,
16
12
} from '../../tests/mocks/mocks' ;
17
13
import { emitEvent } from '../../tests/utils' ;
14
+ import { renderClassComponentWithInstanceRef } from '../test-utils/renderClassComponentWithInstanceRef' ;
18
15
19
16
jest . mock ( '../../src/renderer/components/editor' , ( ) => ( {
20
17
Editor : ( ) => 'Editor' ,
21
18
} ) ) ;
22
19
23
20
describe ( 'Editors component' , ( ) => {
24
21
let app : App ;
25
- let monaco : MonacoMock ;
26
22
let store : AppState ;
27
23
let editorMosaic : EditorMosaic ;
28
24
let editorValues : EditorValues ;
29
25
30
26
beforeEach ( ( ) => {
31
27
( { app } = window ) ;
32
- monaco = window . monaco as unknown as MonacoMock ;
33
28
( { state : store } = window . app ) ;
34
29
editorValues = createEditorValues ( ) ;
35
30
editorMosaic = new EditorMosaic ( ) ;
@@ -38,15 +33,20 @@ describe('Editors component', () => {
38
33
( store as unknown as StateMock ) . editorMosaic = editorMosaic ;
39
34
} ) ;
40
35
36
+ function renderEditors ( ) {
37
+ return renderClassComponentWithInstanceRef ( Editors , {
38
+ appState : store ,
39
+ } ) ;
40
+ }
41
+
41
42
it ( 'renders' , ( ) => {
42
- const wrapper = mount ( < Editors appState = { store } /> ) ;
43
- wrapper . setState ( { monaco } ) ;
44
- expect ( wrapper ) . toMatchSnapshot ( ) ;
43
+ const { renderResult } = renderEditors ( ) ;
44
+
45
+ expect ( renderResult . getByTestId ( 'editors' ) ) . toBeInTheDocument ( ) ;
45
46
} ) ;
46
47
47
48
it ( 'does not execute command if not supported' , ( ) => {
48
- const wrapper = shallow ( < Editors appState = { store } /> ) ;
49
- const instance : any = wrapper . instance ( ) ;
49
+ const { instance } = renderEditors ( ) ;
50
50
51
51
const editor = new MonacoEditorMock ( ) ;
52
52
const action = editor . getAction ( ) ;
@@ -70,15 +70,13 @@ describe('Editors component', () => {
70
70
throw new Error ( 'Bwap bwap' ) ;
71
71
} ) ;
72
72
73
- const wrapper = shallow ( < Editors appState = { store } /> ) ;
74
- const instance : any = wrapper . instance ( ) ;
73
+ const { instance } = renderEditors ( ) ;
75
74
76
75
expect ( instance . toggleEditorOption ( 'wordWrap' ) ) . toBe ( false ) ;
77
76
} ) ;
78
77
79
78
it ( 'updates a setting' , ( ) => {
80
- const wrapper = shallow ( < Editors appState = { store } /> ) ;
81
- const instance : any = wrapper . instance ( ) ;
79
+ const { instance } = renderEditors ( ) ;
82
80
83
81
const editor = new MonacoEditorMock ( ) ;
84
82
editorMosaic . addEditor ( filename , editor as unknown as Editor ) ;
@@ -90,29 +88,35 @@ describe('Editors component', () => {
90
88
} ) ;
91
89
} ) ;
92
90
93
- it ( 'renders a toolbar' , ( ) => {
94
- const wrapper = shallow ( < Editors appState = { store } /> ) ;
95
- const instance : any = wrapper . instance ( ) ;
96
- const toolbar = instance . renderToolbar (
97
- { title : MAIN_JS } as MosaicWindowProps < EditorId > ,
98
- MAIN_JS ,
99
- ) ;
100
-
101
- expect ( toolbar ) . toMatchSnapshot ( ) ;
91
+ it ( 'renders toolbars' , ( ) => {
92
+ const { renderResult } = renderEditors ( ) ;
93
+
94
+ const [
95
+ mainToolbar ,
96
+ rendererToolbar ,
97
+ htmlToolbar ,
98
+ preloadToolbar ,
99
+ stylesheetToolbar ,
100
+ ] = renderResult . getAllByTestId ( 'editors-toolbar' ) ;
101
+
102
+ expect ( mainToolbar ) . toHaveTextContent ( 'Main Process (main.js)' ) ;
103
+ expect ( rendererToolbar ) . toHaveTextContent ( 'Renderer Process (renderer.js)' ) ;
104
+ expect ( htmlToolbar ) . toHaveTextContent ( 'HTML (index.html)' ) ;
105
+ expect ( preloadToolbar ) . toHaveTextContent ( 'Preload (preload.js)' ) ;
106
+ expect ( stylesheetToolbar ) . toHaveTextContent ( 'Stylesheet (styles.css)' ) ;
102
107
} ) ;
103
108
104
109
it ( 'onChange() updates the mosaic arrangement in the appState' , ( ) => {
105
- const wrapper = shallow ( < Editors appState = { store } /> ) ;
106
- const instance : any = wrapper . instance ( ) ;
110
+ const { instance } = renderEditors ( ) ;
107
111
108
- const arrangement = { testArrangement : true } ;
109
- instance . onChange ( arrangement as any ) ;
112
+ const arrangement : MosaicNode < EditorId > = ' testArrangement.js' ;
113
+ instance . onChange ( arrangement ) ;
110
114
expect ( editorMosaic . mosaic ) . toStrictEqual ( arrangement ) ;
111
115
} ) ;
112
116
113
117
describe ( 'events' , ( ) => {
114
118
it ( 'handles a "execute-monaco-command" event' , ( ) => {
115
- shallow ( < Editors appState = { store } /> ) ;
119
+ renderEditors ( ) ;
116
120
117
121
const editor = new MonacoEditorMock ( ) ;
118
122
const action = editor . getAction ( ) ;
@@ -129,7 +133,7 @@ describe('Editors component', () => {
129
133
const fakeValues = { [ MAIN_JS ] : 'hi' } as const ;
130
134
131
135
it ( 'handles a "new-fiddle" event' , async ( ) => {
132
- shallow ( < Editors appState = { store } /> ) ;
136
+ renderEditors ( ) ;
133
137
134
138
let resolve : ( value ?: unknown ) => void ;
135
139
const replacePromise = new Promise ( ( r ) => {
@@ -162,7 +166,7 @@ describe('Editors component', () => {
162
166
163
167
describe ( '"select-all-in-editor" handler' , ( ) => {
164
168
it ( 'selects all in the focused editor' , async ( ) => {
165
- shallow ( < Editors appState = { store } /> ) ;
169
+ renderEditors ( ) ;
166
170
167
171
const range = 'range' ;
168
172
const editor = new MonacoEditorMock ( ) ;
@@ -177,7 +181,7 @@ describe('Editors component', () => {
177
181
} ) ;
178
182
179
183
it ( 'does not change selection if the selected editor has no model' , async ( ) => {
180
- shallow ( < Editors appState = { store } /> ) ;
184
+ renderEditors ( ) ;
181
185
182
186
const editor = new MonacoEditorMock ( ) ;
183
187
delete ( editor as any ) . model ;
@@ -191,14 +195,14 @@ describe('Editors component', () => {
191
195
} ) ;
192
196
193
197
it ( 'does not crash if there is no selected editor' , ( ) => {
194
- shallow ( < Editors appState = { store } /> ) ;
198
+ renderEditors ( ) ;
195
199
editorMosaic . focusedEditor = jest . fn ( ) . mockReturnValue ( null ) ;
196
200
emitEvent ( 'select-all-in-editor' ) ;
197
201
} ) ;
198
202
} ) ;
199
203
200
204
it ( 'handles a "new-test" event' , async ( ) => {
201
- shallow ( < Editors appState = { store } /> ) ;
205
+ renderEditors ( ) ;
202
206
203
207
// setup
204
208
const getTestTemplateSpy = jest
@@ -229,7 +233,7 @@ describe('Editors component', () => {
229
233
} ) ;
230
234
231
235
it ( 'handles a "select-all-in-editor" event' , async ( ) => {
232
- shallow ( < Editors appState = { store } /> ) ;
236
+ renderEditors ( ) ;
233
237
234
238
const range = 'range' ;
235
239
const editor = new MonacoEditorMock ( ) ;
@@ -247,16 +251,16 @@ describe('Editors component', () => {
247
251
const editor = new MonacoEditorMock ( ) ;
248
252
editorMosaic . addEditor ( id , editor as unknown as Editor ) ;
249
253
250
- shallow ( < Editors appState = { store } /> ) ;
254
+ renderEditors ( ) ;
251
255
emitEvent ( 'toggle-monaco-option' , 'wordWrap' ) ;
252
256
expect ( editor . updateOptions ) . toHaveBeenCalled ( ) ;
253
257
} ) ;
254
258
} ) ;
255
259
256
260
describe ( 'setFocused()' , ( ) => {
257
261
it ( 'sets the "focused" property' , ( ) => {
258
- const wrapper = shallow ( < Editors appState = { store } /> ) ;
259
- const instance : any = wrapper . instance ( ) ;
262
+ const { instance } = renderEditors ( ) ;
263
+
260
264
const spy = jest . spyOn ( instance , 'setState' ) ;
261
265
262
266
const id = MAIN_JS ;
@@ -265,8 +269,8 @@ describe('Editors component', () => {
265
269
} ) ;
266
270
267
271
it ( 'focus sidebar file' , ( ) => {
268
- const wrapper = shallow ( < Editors appState = { store } /> ) ;
269
- const instance : any = wrapper . instance ( ) ;
272
+ const { instance } = renderEditors ( ) ;
273
+
270
274
const spy = jest . spyOn ( instance , 'setState' ) ;
271
275
272
276
const id = MAIN_JS ;
0 commit comments