1
+ import { type Workspace } from "api/typesGenerated" ;
1
2
import { screen , waitFor , within } from "@testing-library/react" ;
2
3
import userEvent from "@testing-library/user-event" ;
3
4
import EventSourceMock from "eventsourcemock" ;
4
5
import { rest } from "msw" ;
5
6
import {
6
7
MockTemplate ,
7
8
MockWorkspace ,
9
+ MockFailedWorkspace ,
8
10
MockWorkspaceBuild ,
9
11
MockStoppedWorkspace ,
10
12
MockStartingWorkspace ,
@@ -22,7 +24,7 @@ import { server } from "testHelpers/server";
22
24
import { WorkspacePage } from "./WorkspacePage" ;
23
25
24
26
// It renders the workspace page and waits for it be loaded
25
- const renderWorkspacePage = async ( mockWorkspace = MockWorkspace ) => {
27
+ const renderWorkspacePage = async ( workspace : Workspace ) => {
26
28
jest . spyOn ( api , "getTemplate" ) . mockResolvedValueOnce ( MockTemplate ) ;
27
29
jest . spyOn ( api , "getTemplateVersionRichParameters" ) . mockResolvedValueOnce ( [ ] ) ;
28
30
jest
@@ -36,11 +38,11 @@ const renderWorkspacePage = async (mockWorkspace = MockWorkspace) => {
36
38
} ) ;
37
39
38
40
renderWithAuth ( < WorkspacePage /> , {
39
- route : `/@${ mockWorkspace . owner_name } /${ mockWorkspace . name } ` ,
41
+ route : `/@${ workspace . owner_name } /${ workspace . name } ` ,
40
42
path : "/:username/:workspace" ,
41
43
} ) ;
42
44
43
- await screen . findByText ( mockWorkspace . name ) ;
45
+ await screen . findByText ( workspace . name ) ;
44
46
} ;
45
47
46
48
/**
@@ -51,11 +53,17 @@ const renderWorkspacePage = async (mockWorkspace = MockWorkspace) => {
51
53
* We don't need to test the UI exhaustively because Storybook does that; just
52
54
* enough to prove that the workspaceStatus was calculated correctly.
53
55
*/
54
- const testButton = async ( label : string , actionMock : jest . SpyInstance ) => {
56
+ const testButton = async (
57
+ workspace : Workspace ,
58
+ label : string ,
59
+ actionMock : jest . SpyInstance ,
60
+ ) => {
55
61
const user = userEvent . setup ( ) ;
56
- await renderWorkspacePage ( ) ;
62
+ await renderWorkspacePage ( workspace ) ;
63
+
57
64
const workspaceActions = screen . getByTestId ( "workspace-actions" ) ;
58
65
const button = within ( workspaceActions ) . getByRole ( "button" , { name : label } ) ;
66
+
59
67
await user . click ( button ) ;
60
68
expect ( actionMock ) . toBeCalled ( ) ;
61
69
} ;
@@ -82,7 +90,7 @@ describe("WorkspacePage", () => {
82
90
const deleteWorkspaceMock = jest
83
91
. spyOn ( api , "deleteWorkspace" )
84
92
. mockResolvedValueOnce ( MockWorkspaceBuild ) ;
85
- await renderWorkspacePage ( ) ;
93
+ await renderWorkspacePage ( MockWorkspace ) ;
86
94
87
95
// open the workspace action popover so we have access to all available ctas
88
96
const trigger = screen . getByTestId ( "workspace-options-button" ) ;
@@ -125,7 +133,7 @@ describe("WorkspacePage", () => {
125
133
const deleteWorkspaceMock = jest
126
134
. spyOn ( api , "deleteWorkspace" )
127
135
. mockResolvedValueOnce ( MockWorkspaceBuildDelete ) ;
128
- await renderWorkspacePage ( ) ;
136
+ await renderWorkspacePage ( MockWorkspace ) ;
129
137
130
138
// open the workspace action popover so we have access to all available ctas
131
139
const trigger = screen . getByTestId ( "workspace-options-button" ) ;
@@ -173,15 +181,15 @@ describe("WorkspacePage", () => {
173
181
const startWorkspaceMock = jest
174
182
. spyOn ( api , "startWorkspace" )
175
183
. mockImplementation ( ( ) => Promise . resolve ( MockWorkspaceBuild ) ) ;
176
- await testButton ( "Start" , startWorkspaceMock ) ;
184
+ await testButton ( MockWorkspace , "Start" , startWorkspaceMock ) ;
177
185
} ) ;
178
186
179
187
it ( "requests a stop job when the user presses Stop" , async ( ) => {
180
188
const stopWorkspaceMock = jest
181
189
. spyOn ( api , "stopWorkspace" )
182
190
. mockResolvedValueOnce ( MockWorkspaceBuild ) ;
183
191
184
- await testButton ( "Stop" , stopWorkspaceMock ) ;
192
+ await testButton ( MockWorkspace , "Stop" , stopWorkspaceMock ) ;
185
193
} ) ;
186
194
187
195
it ( "requests a stop when the user presses Restart" , async ( ) => {
@@ -190,7 +198,7 @@ describe("WorkspacePage", () => {
190
198
. mockResolvedValueOnce ( MockWorkspaceBuild ) ;
191
199
192
200
// Render
193
- await renderWorkspacePage ( ) ;
201
+ await renderWorkspacePage ( MockWorkspace ) ;
194
202
195
203
// Actions
196
204
const user = userEvent . setup ( ) ;
@@ -217,7 +225,7 @@ describe("WorkspacePage", () => {
217
225
. spyOn ( api , "cancelWorkspaceBuild" )
218
226
. mockImplementation ( ( ) => Promise . resolve ( { message : "job canceled" } ) ) ;
219
227
220
- await renderWorkspacePage ( ) ;
228
+ await renderWorkspacePage ( MockWorkspace ) ;
221
229
222
230
const workspaceActions = screen . getByTestId ( "workspace-actions" ) ;
223
231
const cancelButton = within ( workspaceActions ) . getByRole ( "button" , {
@@ -240,7 +248,7 @@ describe("WorkspacePage", () => {
240
248
. mockResolvedValueOnce ( MockWorkspaceBuild ) ;
241
249
242
250
// Render
243
- await renderWorkspacePage ( ) ;
251
+ await renderWorkspacePage ( MockWorkspace ) ;
244
252
245
253
// Actions
246
254
const user = userEvent . setup ( ) ;
@@ -269,7 +277,7 @@ describe("WorkspacePage", () => {
269
277
) ;
270
278
271
279
// Render
272
- await renderWorkspacePage ( ) ;
280
+ await renderWorkspacePage ( MockWorkspace ) ;
273
281
274
282
// Actions
275
283
const user = userEvent . setup ( ) ;
@@ -316,7 +324,7 @@ describe("WorkspacePage", () => {
316
324
} ) ;
317
325
318
326
it ( "shows the timeline build" , async ( ) => {
319
- await renderWorkspacePage ( ) ;
327
+ await renderWorkspacePage ( MockWorkspace ) ;
320
328
const table = await screen . findByTestId ( "builds-table" ) ;
321
329
322
330
// Wait for the results to be loaded
@@ -343,7 +351,7 @@ describe("WorkspacePage", () => {
343
351
} ) ;
344
352
const restartWorkspaceSpy = jest . spyOn ( api , "restartWorkspace" ) ;
345
353
const user = userEvent . setup ( ) ;
346
- await renderWorkspacePage ( ) ;
354
+ await renderWorkspacePage ( MockWorkspace ) ;
347
355
await user . click ( screen . getByTestId ( "build-parameters-button" ) ) ;
348
356
const buildParametersForm = await screen . findByTestId (
349
357
"build-parameters-form" ,
0 commit comments