-
Notifications
You must be signed in to change notification settings - Fork 28
/
App.test.tsx
390 lines (316 loc) · 11.4 KB
/
App.test.tsx
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
/**
* @jest-environment jsdom
*/
import React from 'react'
import { render, cleanup, screen, fireEvent } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'
import comparisonTableFixture from 'dvc/src/test/fixtures/plotsDiff/comparison'
import livePlotsFixture from 'dvc/src/test/fixtures/expShow/livePlots'
import staticPlotsFixture from 'dvc/src/test/fixtures/plotsDiff/static/webview'
import {
DEFAULT_SECTION_COLLAPSED,
LivePlotsColors,
PlotsData,
PlotSize,
Section
} from 'dvc/src/plots/webview/contract'
import {
MessageFromWebviewType,
MessageToWebviewType
} from 'dvc/src/webview/contract'
import { App } from './App'
import { Plots } from './Plots'
import { vsCodeApi } from '../../shared/api'
jest.mock('../../shared/api')
jest.mock('./constants', () => ({
...jest.requireActual('./constants'),
createSpec: (title: string, scale?: LivePlotsColors) => ({
...jest.requireActual('./constants').createSpec(title, scale),
height: 100,
width: 100
})
}))
const { postMessage, setState } = vsCodeApi
const mockPostMessage = jest.mocked(postMessage)
const mockSetState = jest.mocked(setState)
beforeEach(() => {
jest.clearAllMocks()
})
afterEach(() => {
cleanup()
})
describe('App', () => {
const sendSetDataMessage = (data: PlotsData) => {
const message = new MessageEvent('message', {
data: {
data,
type: MessageToWebviewType.SET_DATA
}
})
fireEvent(window, message)
}
const renderAppWithData = (data: PlotsData) => {
render(<App />)
sendSetDataMessage(data)
}
it('should send the initialized message on first render', () => {
render(<App />)
expect(mockPostMessage).toHaveBeenCalledWith({
type: MessageFromWebviewType.INITIALIZED
})
expect(mockPostMessage).toHaveBeenCalledTimes(1)
})
it('should set dvcRoot when the setDvcRoot message comes in', () => {
render(<App />)
fireEvent(
window,
new MessageEvent('message', {
data: {
dvcRoot: 'root',
type: MessageToWebviewType.SET_DVC_ROOT
}
})
)
expect(mockSetState).toBeCalledWith({
dvcRoot: 'root'
})
expect(mockSetState).toBeCalledTimes(1)
})
it('should render the loading state when given no data', async () => {
render(<App />)
const loadingState = await screen.findByText('Loading Plots...')
expect(loadingState).toBeInTheDocument()
})
it('should render the loading state when not initially provided with collapsed sections', async () => {
renderAppWithData({
live: null
})
const loadingState = await screen.findByText('Loading Plots...')
expect(loadingState).toBeInTheDocument()
})
it('should render the empty state when given data with no plots', async () => {
renderAppWithData({
live: null,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
const emptyState = await screen.findByText('No Plots to Display')
expect(emptyState).toBeInTheDocument()
})
it('should render only live plots when given a message with only live plots data', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {})
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
expect(screen.queryByText('Loading Plots...')).not.toBeInTheDocument()
expect(screen.getByText('Live Experiments Plots')).toBeInTheDocument()
expect(screen.queryByText('Static Plots')).not.toBeInTheDocument()
expect(screen.queryByText('Comparison')).not.toBeInTheDocument()
})
it('should render live and static plots when given messages with both types of plots data', () => {
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
const heightToSuppressVegaError = 1000
jest
.spyOn(HTMLElement.prototype, 'clientHeight', 'get')
.mockImplementation(() => heightToSuppressVegaError)
sendSetDataMessage({
static: staticPlotsFixture
})
expect(screen.queryByText('Loading Plots...')).not.toBeInTheDocument()
expect(screen.getByText('Live Experiments Plots')).toBeInTheDocument()
expect(screen.getByText('Static Plots')).toBeInTheDocument()
})
it('should render the comparison table when given a message with comparison plots data', () => {
const expectedSectionName = 'Comparison'
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
sendSetDataMessage({
comparison: comparisonTableFixture
})
expect(screen.getByText(expectedSectionName)).toBeInTheDocument()
})
it('should remove live plots given a message showing live plots as null', () => {
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
expect(screen.getByText('Live Experiments Plots')).toBeInTheDocument()
sendSetDataMessage({
live: null
})
expect(screen.queryByText('Live Experiments Plots')).not.toBeInTheDocument()
})
it('should toggle the live plots section in state when its header is clicked', async () => {
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
const summaryElement = await screen.findByText('Live Experiments Plots')
const [plot] = await screen.findAllByLabelText('Vega visualization')
expect(plot).toBeInTheDocument()
fireEvent.click(summaryElement, {
bubbles: true,
cancelable: true
})
expect(
screen.queryByLabelText('Vega visualization')
).not.toBeInTheDocument()
expect(mockPostMessage).toBeCalledWith({
payload: { [Section.LIVE_PLOTS]: true },
type: MessageFromWebviewType.PLOTS_SECTION_TOGGLED
})
})
it('should toggle the visibility of plots when clicking the metrics in the metrics picker', async () => {
render(
<Plots
state={{
data: {
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED,
static: null
}
}}
dispatch={jest.fn}
/>
)
const summaryElement = await screen.findByText('Live Experiments Plots')
fireEvent.click(summaryElement, {
bubbles: true,
cancelable: true
})
expect(() => screen.getByTestId('plot-summary.json:loss')).not.toThrow()
const [, pickerButton] = screen.queryAllByTestId('icon-menu-item')
fireEvent.mouseEnter(pickerButton)
fireEvent.click(pickerButton)
const lossItem = await screen.findByText('loss')
fireEvent.click(lossItem, {
bubbles: true,
cancelable: true
})
expect(() => screen.getByTestId('plot-summary.json:loss')).toThrow()
fireEvent.mouseEnter(pickerButton)
fireEvent.click(pickerButton)
fireEvent.click(lossItem, {
bubbles: true,
cancelable: true
})
expect(() => screen.getByTestId('plot-summary.json:loss')).not.toThrow()
})
it('should send a message to the extension with the selected metrics when toggling the visibility of a plot', async () => {
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
const [, pickerButton] = screen.getAllByTestId('icon-menu-item')
fireEvent.mouseEnter(pickerButton)
fireEvent.click(pickerButton)
const lossItem = await screen.findByText('loss')
fireEvent.click(lossItem, {
bubbles: true,
cancelable: true
})
expect(mockPostMessage).toBeCalledWith({
payload: ['accuracy', 'val_loss', 'val_accuracy'],
type: MessageFromWebviewType.METRIC_TOGGLED
})
fireEvent.click(lossItem, {
bubbles: true,
cancelable: true
})
expect(mockPostMessage).toBeCalledWith({
payload: ['loss', 'accuracy', 'val_loss', 'val_accuracy'],
type: MessageFromWebviewType.METRIC_TOGGLED
})
})
it('should change the size of the plots according to the size picker', async () => {
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
const [, , sizePickerButton] = screen.getAllByTestId('icon-menu-item')
fireEvent.mouseEnter(sizePickerButton)
fireEvent.click(sizePickerButton)
const smallButton = screen.getByText('Small')
const regularButton = screen.getByText('Regular')
const largeButton = screen.getByText('Large')
fireEvent.click(smallButton)
let wrapper = await screen.findByTestId('plots-wrapper')
expect(wrapper).toHaveClass('smallPlots')
fireEvent.click(regularButton)
wrapper = await screen.findByTestId('plots-wrapper')
expect(wrapper).toHaveClass('regularPlots')
fireEvent.click(largeButton)
wrapper = await screen.findByTestId('plots-wrapper')
expect(wrapper).toHaveClass('largePlots')
})
it('should send a message to the extension with the selected size when changing the size of plots', () => {
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
const [, , sizeButton] = screen.getAllByTestId('icon-menu-item')
fireEvent.mouseEnter(sizeButton)
fireEvent.click(sizeButton)
const largeButton = screen.getByText('Large')
fireEvent.click(largeButton)
expect(mockPostMessage).toBeCalledWith({
payload: { section: Section.LIVE_PLOTS, size: PlotSize.LARGE },
type: MessageFromWebviewType.PLOTS_RESIZED
})
const smallButton = screen.getByText('Small')
fireEvent.click(smallButton)
expect(mockPostMessage).toBeCalledWith({
payload: { section: Section.LIVE_PLOTS, size: PlotSize.SMALL },
type: MessageFromWebviewType.PLOTS_RESIZED
})
})
it('should show an input to rename the section when clicking the rename icon button', () => {
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
expect(screen.queryByRole('textbox')).toBeNull()
const [renameButton] = screen.getAllByTestId('icon-menu-item')
fireEvent.mouseEnter(renameButton)
fireEvent.click(renameButton)
expect(screen.getByRole('textbox')).toBeInTheDocument()
})
it('should change the title of the section when hitting enter on the title input', () => {
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
const originalText = 'Live Experiments Plots'
expect(screen.getByText(originalText)).toBeInTheDocument()
const [renameButton] = screen.getAllByTestId('icon-menu-item')
fireEvent.mouseEnter(renameButton)
fireEvent.click(renameButton)
const titleInput = screen.getByRole('textbox')
const newTitle = 'Brand new section'
fireEvent.change(titleInput, { target: { value: newTitle } })
fireEvent.keyDown(titleInput, { key: 'Enter' })
expect(screen.getByText(newTitle)).toBeInTheDocument()
})
it('should send a message to the extension with the new section name after a section rename', () => {
renderAppWithData({
live: livePlotsFixture,
sectionCollapsed: DEFAULT_SECTION_COLLAPSED
})
const [renameButton] = screen.getAllByTestId('icon-menu-item')
fireEvent.mouseEnter(renameButton)
fireEvent.click(renameButton)
const titleInput = screen.getByRole('textbox')
const newTitle = 'Brand new section'
fireEvent.change(titleInput, { target: { value: newTitle } })
fireEvent.keyDown(titleInput, { key: 'Enter' })
expect(mockPostMessage).toBeCalledWith({
payload: { name: newTitle, section: Section.LIVE_PLOTS },
type: MessageFromWebviewType.SECTION_RENAMED
})
})
})