Skip to content

Commit b1f6660

Browse files
committed
chore: make workspace helpers take explicit workspace parameter
1 parent edecd14 commit b1f6660

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

site/src/pages/WorkspacePage/WorkspacePage.test.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { type Workspace } from "api/typesGenerated";
12
import { screen, waitFor, within } from "@testing-library/react";
23
import userEvent from "@testing-library/user-event";
34
import EventSourceMock from "eventsourcemock";
45
import { rest } from "msw";
56
import {
67
MockTemplate,
78
MockWorkspace,
9+
MockFailedWorkspace,
810
MockWorkspaceBuild,
911
MockStoppedWorkspace,
1012
MockStartingWorkspace,
@@ -22,7 +24,7 @@ import { server } from "testHelpers/server";
2224
import { WorkspacePage } from "./WorkspacePage";
2325

2426
// It renders the workspace page and waits for it be loaded
25-
const renderWorkspacePage = async (mockWorkspace = MockWorkspace) => {
27+
const renderWorkspacePage = async (workspace: Workspace) => {
2628
jest.spyOn(api, "getTemplate").mockResolvedValueOnce(MockTemplate);
2729
jest.spyOn(api, "getTemplateVersionRichParameters").mockResolvedValueOnce([]);
2830
jest
@@ -36,11 +38,11 @@ const renderWorkspacePage = async (mockWorkspace = MockWorkspace) => {
3638
});
3739

3840
renderWithAuth(<WorkspacePage />, {
39-
route: `/@${mockWorkspace.owner_name}/${mockWorkspace.name}`,
41+
route: `/@${workspace.owner_name}/${workspace.name}`,
4042
path: "/:username/:workspace",
4143
});
4244

43-
await screen.findByText(mockWorkspace.name);
45+
await screen.findByText(workspace.name);
4446
};
4547

4648
/**
@@ -51,11 +53,17 @@ const renderWorkspacePage = async (mockWorkspace = MockWorkspace) => {
5153
* We don't need to test the UI exhaustively because Storybook does that; just
5254
* enough to prove that the workspaceStatus was calculated correctly.
5355
*/
54-
const testButton = async (label: string, actionMock: jest.SpyInstance) => {
56+
const testButton = async (
57+
workspace: Workspace,
58+
label: string,
59+
actionMock: jest.SpyInstance,
60+
) => {
5561
const user = userEvent.setup();
56-
await renderWorkspacePage();
62+
await renderWorkspacePage(workspace);
63+
5764
const workspaceActions = screen.getByTestId("workspace-actions");
5865
const button = within(workspaceActions).getByRole("button", { name: label });
66+
5967
await user.click(button);
6068
expect(actionMock).toBeCalled();
6169
};
@@ -82,7 +90,7 @@ describe("WorkspacePage", () => {
8290
const deleteWorkspaceMock = jest
8391
.spyOn(api, "deleteWorkspace")
8492
.mockResolvedValueOnce(MockWorkspaceBuild);
85-
await renderWorkspacePage();
93+
await renderWorkspacePage(MockWorkspace);
8694

8795
// open the workspace action popover so we have access to all available ctas
8896
const trigger = screen.getByTestId("workspace-options-button");
@@ -125,7 +133,7 @@ describe("WorkspacePage", () => {
125133
const deleteWorkspaceMock = jest
126134
.spyOn(api, "deleteWorkspace")
127135
.mockResolvedValueOnce(MockWorkspaceBuildDelete);
128-
await renderWorkspacePage();
136+
await renderWorkspacePage(MockWorkspace);
129137

130138
// open the workspace action popover so we have access to all available ctas
131139
const trigger = screen.getByTestId("workspace-options-button");
@@ -173,15 +181,15 @@ describe("WorkspacePage", () => {
173181
const startWorkspaceMock = jest
174182
.spyOn(api, "startWorkspace")
175183
.mockImplementation(() => Promise.resolve(MockWorkspaceBuild));
176-
await testButton("Start", startWorkspaceMock);
184+
await testButton(MockWorkspace, "Start", startWorkspaceMock);
177185
});
178186

179187
it("requests a stop job when the user presses Stop", async () => {
180188
const stopWorkspaceMock = jest
181189
.spyOn(api, "stopWorkspace")
182190
.mockResolvedValueOnce(MockWorkspaceBuild);
183191

184-
await testButton("Stop", stopWorkspaceMock);
192+
await testButton(MockWorkspace, "Stop", stopWorkspaceMock);
185193
});
186194

187195
it("requests a stop when the user presses Restart", async () => {
@@ -190,7 +198,7 @@ describe("WorkspacePage", () => {
190198
.mockResolvedValueOnce(MockWorkspaceBuild);
191199

192200
// Render
193-
await renderWorkspacePage();
201+
await renderWorkspacePage(MockWorkspace);
194202

195203
// Actions
196204
const user = userEvent.setup();
@@ -217,7 +225,7 @@ describe("WorkspacePage", () => {
217225
.spyOn(api, "cancelWorkspaceBuild")
218226
.mockImplementation(() => Promise.resolve({ message: "job canceled" }));
219227

220-
await renderWorkspacePage();
228+
await renderWorkspacePage(MockWorkspace);
221229

222230
const workspaceActions = screen.getByTestId("workspace-actions");
223231
const cancelButton = within(workspaceActions).getByRole("button", {
@@ -240,7 +248,7 @@ describe("WorkspacePage", () => {
240248
.mockResolvedValueOnce(MockWorkspaceBuild);
241249

242250
// Render
243-
await renderWorkspacePage();
251+
await renderWorkspacePage(MockWorkspace);
244252

245253
// Actions
246254
const user = userEvent.setup();
@@ -269,7 +277,7 @@ describe("WorkspacePage", () => {
269277
);
270278

271279
// Render
272-
await renderWorkspacePage();
280+
await renderWorkspacePage(MockWorkspace);
273281

274282
// Actions
275283
const user = userEvent.setup();
@@ -316,7 +324,7 @@ describe("WorkspacePage", () => {
316324
});
317325

318326
it("shows the timeline build", async () => {
319-
await renderWorkspacePage();
327+
await renderWorkspacePage(MockWorkspace);
320328
const table = await screen.findByTestId("builds-table");
321329

322330
// Wait for the results to be loaded
@@ -343,7 +351,7 @@ describe("WorkspacePage", () => {
343351
});
344352
const restartWorkspaceSpy = jest.spyOn(api, "restartWorkspace");
345353
const user = userEvent.setup();
346-
await renderWorkspacePage();
354+
await renderWorkspacePage(MockWorkspace);
347355
await user.click(screen.getByTestId("build-parameters-button"));
348356
const buildParametersForm = await screen.findByTestId(
349357
"build-parameters-form",

0 commit comments

Comments
 (0)