Skip to content

Commit c335148

Browse files
committed
Fix tests
1 parent 5e5ea5f commit c335148

File tree

3 files changed

+128
-120
lines changed

3 files changed

+128
-120
lines changed

site/src/api/api.test.ts

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import axios from "axios"
2-
import { getApiKey, getURLWithSearchParams, login, logout } from "./api"
2+
import {
3+
MockTemplate,
4+
MockTemplateVersionParameter1,
5+
MockWorkspace,
6+
MockWorkspaceBuild,
7+
} from "testHelpers/entities"
8+
import * as api from "./api"
39
import * as TypesGen from "./typesGenerated"
410

511
describe("api.ts", () => {
@@ -12,7 +18,7 @@ describe("api.ts", () => {
1218
jest.spyOn(axios, "post").mockResolvedValueOnce({ data: loginResponse })
1319

1420
// when
15-
const result = await login("test", "123")
21+
const result = await api.login("test", "123")
1622

1723
// then
1824
expect(axios.post).toHaveBeenCalled()
@@ -33,7 +39,7 @@ describe("api.ts", () => {
3339
axios.post = axiosMockPost
3440

3541
try {
36-
await login("test", "123")
42+
await api.login("test", "123")
3743
} catch (error) {
3844
expect(error).toStrictEqual(expectedError)
3945
}
@@ -49,7 +55,7 @@ describe("api.ts", () => {
4955
axios.post = axiosMockPost
5056

5157
// when
52-
await logout()
58+
await api.logout()
5359

5460
// then
5561
expect(axiosMockPost).toHaveBeenCalled()
@@ -68,7 +74,7 @@ describe("api.ts", () => {
6874
axios.post = axiosMockPost
6975

7076
try {
71-
await logout()
77+
await api.logout()
7278
} catch (error) {
7379
expect(error).toStrictEqual(expectedError)
7480
}
@@ -87,7 +93,7 @@ describe("api.ts", () => {
8793
axios.post = axiosMockPost
8894

8995
// when
90-
const result = await getApiKey()
96+
const result = await api.getApiKey()
9197

9298
// then
9399
expect(axiosMockPost).toHaveBeenCalled()
@@ -107,7 +113,7 @@ describe("api.ts", () => {
107113
axios.post = axiosMockPost
108114

109115
try {
110-
await getApiKey()
116+
await api.getApiKey()
111117
} catch (error) {
112118
expect(error).toStrictEqual(expectedError)
113119
}
@@ -133,7 +139,7 @@ describe("api.ts", () => {
133139
])(
134140
`Workspaces - getURLWithSearchParams(%p, %p) returns %p`,
135141
(basePath, filter, expected) => {
136-
expect(getURLWithSearchParams(basePath, filter)).toBe(expected)
142+
expect(api.getURLWithSearchParams(basePath, filter)).toBe(expected)
137143
},
138144
)
139145
})
@@ -150,8 +156,38 @@ describe("api.ts", () => {
150156
])(
151157
`Users - getURLWithSearchParams(%p, %p) returns %p`,
152158
(basePath, filter, expected) => {
153-
expect(getURLWithSearchParams(basePath, filter)).toBe(expected)
159+
expect(api.getURLWithSearchParams(basePath, filter)).toBe(expected)
154160
},
155161
)
156162
})
163+
164+
describe("update", () => {
165+
it("creates a build with start and the latest template", async () => {
166+
jest
167+
.spyOn(api, "postWorkspaceBuild")
168+
.mockResolvedValueOnce(MockWorkspaceBuild)
169+
jest.spyOn(api, "getTemplate").mockResolvedValueOnce(MockTemplate)
170+
await api.updateWorkspace(MockWorkspace)
171+
expect(api.postWorkspaceBuild).toHaveBeenCalledWith(MockWorkspace.id, {
172+
transition: "start",
173+
template_version_id: MockTemplate.active_version_id,
174+
rich_parameter_values: [],
175+
})
176+
})
177+
178+
it("fails when having missing parameters", async () => {
179+
jest
180+
.spyOn(api, "postWorkspaceBuild")
181+
.mockResolvedValueOnce(MockWorkspaceBuild)
182+
jest.spyOn(api, "getTemplate").mockResolvedValueOnce(MockTemplate)
183+
jest.spyOn(api, "getWorkspaceBuildParameters").mockResolvedValueOnce([])
184+
jest
185+
.spyOn(api, "getTemplateVersionRichParameters")
186+
.mockResolvedValueOnce([MockTemplateVersionParameter1])
187+
188+
await expect(api.updateWorkspace(MockWorkspace)).rejects.toThrow(
189+
api.MissingBuildParameters,
190+
)
191+
})
192+
})
157193
})

site/src/api/api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,6 @@ export const updateWorkspace = async (
932932
newBuildParameters,
933933
templateParameters,
934934
)
935-
936935
if (missingParameters.length > 0) {
937936
throw new MissingBuildParameters(missingParameters)
938937
}

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

Lines changed: 83 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -173,52 +173,79 @@ describe("WorkspacePage", () => {
173173

174174
expect(cancelWorkspaceMock).toBeCalled()
175175
})
176-
it("requests a template when the user presses Update", async () => {
177-
const getTemplateMock = jest
178-
.spyOn(api, "getTemplate")
179-
.mockResolvedValueOnce(MockTemplate)
180-
server.use(
181-
rest.get(
182-
`/api/v2/users/:userId/workspace/:workspaceName`,
183-
(req, res, ctx) => {
184-
return res(ctx.status(200), ctx.json(MockOutdatedWorkspace))
185-
},
186-
),
187-
)
188176

189-
await renderWorkspacePage()
190-
const buttonText = t("actionButton.update", { ns: "workspacePage" })
191-
const button = await screen.findByText(buttonText, { exact: true })
192-
await userEvent.setup().click(button)
177+
it("requests an update when the user presses Update", async () => {
178+
jest
179+
.spyOn(api, "getWorkspaceByOwnerAndName")
180+
.mockResolvedValueOnce(MockOutdatedWorkspace)
181+
const updateWorkspaceMock = jest
182+
.spyOn(api, "updateWorkspace")
183+
.mockResolvedValueOnce(MockWorkspaceBuild)
193184

194-
// getTemplate is called twice: once when the machine starts, and once after the user requests to update
195-
expect(getTemplateMock).toBeCalledTimes(2)
185+
await testButton(
186+
t("actionButton.update", { ns: "workspacePage" }),
187+
updateWorkspaceMock,
188+
)
196189
})
197-
it("after an update postWorkspaceBuild is called with the latest template active version id", async () => {
198-
jest.spyOn(api, "getTemplate").mockResolvedValueOnce(MockTemplate) // active_version_id = "test-template-version"
199-
jest.spyOn(api, "startWorkspace").mockResolvedValueOnce({
200-
...MockWorkspaceBuild,
201-
})
202190

203-
server.use(
204-
rest.get(
205-
`/api/v2/users/:userId/workspace/:workspaceName`,
206-
(req, res, ctx) => {
207-
return res(ctx.status(200), ctx.json(MockOutdatedWorkspace))
208-
},
209-
),
191+
it("updates the parameters when they are missing during update", async () => {
192+
// Setup mocks
193+
const user = userEvent.setup()
194+
jest
195+
.spyOn(api, "getWorkspaceByOwnerAndName")
196+
.mockResolvedValueOnce(MockOutdatedWorkspace)
197+
const updateWorkspaceSpy = jest
198+
.spyOn(api, "updateWorkspace")
199+
.mockRejectedValueOnce(
200+
new api.MissingBuildParameters([
201+
MockTemplateVersionParameter1,
202+
MockTemplateVersionParameter2,
203+
]),
204+
)
205+
// Render page and wait for it to be loaded
206+
renderWithAuth(<WorkspacePage />, {
207+
route: `/@${MockWorkspace.owner_name}/${MockWorkspace.name}`,
208+
path: "/@:username/:workspace",
209+
})
210+
await waitForLoaderToBeRemoved()
211+
// Click on the update button
212+
const workspaceActions = screen.getByTestId("workspace-actions")
213+
await user.click(
214+
within(workspaceActions).getByRole("button", { name: "Update" }),
210215
)
211-
await renderWorkspacePage()
212-
const buttonText = t("actionButton.update", { ns: "workspacePage" })
213-
const button = await screen.findByText(buttonText, { exact: true })
214-
await userEvent.setup().click(button)
215-
216-
await waitFor(() =>
217-
expect(api.startWorkspace).toBeCalledWith(
218-
"test-outdated-workspace",
219-
"test-template-version",
220-
),
216+
await waitFor(() => {
217+
expect(api.updateWorkspace).toBeCalled()
218+
// We want to clear this mock to use it later
219+
updateWorkspaceSpy.mockClear()
220+
})
221+
// Fill the parameters and send the form
222+
const dialog = await screen.findByTestId("dialog")
223+
const firstParameterInput = within(dialog).getByLabelText(
224+
MockTemplateVersionParameter1.name,
225+
{ exact: false },
221226
)
227+
await user.clear(firstParameterInput)
228+
await user.type(firstParameterInput, "some-value")
229+
const secondParameterInput = within(dialog).getByLabelText(
230+
MockTemplateVersionParameter2.name,
231+
{ exact: false },
232+
)
233+
await user.clear(secondParameterInput)
234+
await user.type(secondParameterInput, "2")
235+
await user.click(within(dialog).getByRole("button", { name: "Update" }))
236+
// Check if the update was called using the values from the form
237+
await waitFor(() => {
238+
expect(api.updateWorkspace).toBeCalledWith(MockOutdatedWorkspace, [
239+
{
240+
name: MockTemplateVersionParameter1.name,
241+
value: "some-value",
242+
},
243+
{
244+
name: MockTemplateVersionParameter2.name,
245+
value: "2",
246+
},
247+
])
248+
})
222249
})
223250

224251
it("shows the Stopping status when the workspace is stopping", async () => {
@@ -227,126 +254,72 @@ describe("WorkspacePage", () => {
227254
t("workspaceStatus.stopping", { ns: "common" }),
228255
)
229256
})
257+
230258
it("shows the Stopped status when the workspace is stopped", async () => {
231259
await testStatus(
232260
MockStoppedWorkspace,
233261
t("workspaceStatus.stopped", { ns: "common" }),
234262
)
235263
})
264+
236265
it("shows the Building status when the workspace is starting", async () => {
237266
await testStatus(
238267
MockStartingWorkspace,
239268
t("workspaceStatus.starting", { ns: "common" }),
240269
)
241270
})
271+
242272
it("shows the Running status when the workspace is running", async () => {
243273
await testStatus(
244274
MockWorkspace,
245275
t("workspaceStatus.running", { ns: "common" }),
246276
)
247277
})
278+
248279
it("shows the Failed status when the workspace is failed or canceled", async () => {
249280
await testStatus(
250281
MockFailedWorkspace,
251282
t("workspaceStatus.failed", { ns: "common" }),
252283
)
253284
})
285+
254286
it("shows the Canceling status when the workspace is canceling", async () => {
255287
await testStatus(
256288
MockCancelingWorkspace,
257289
t("workspaceStatus.canceling", { ns: "common" }),
258290
)
259291
})
292+
260293
it("shows the Canceled status when the workspace is canceling", async () => {
261294
await testStatus(
262295
MockCanceledWorkspace,
263296
t("workspaceStatus.canceled", { ns: "common" }),
264297
)
265298
})
299+
266300
it("shows the Deleting status when the workspace is deleting", async () => {
267301
await testStatus(
268302
MockDeletingWorkspace,
269303
t("workspaceStatus.deleting", { ns: "common" }),
270304
)
271305
})
306+
272307
it("shows the Deleted status when the workspace is deleted", async () => {
273308
await testStatus(
274309
MockDeletedWorkspace,
275310
t("workspaceStatus.deleted", { ns: "common" }),
276311
)
277312
})
278313

279-
describe("Timeline", () => {
280-
it("shows the timeline build", async () => {
281-
await renderWorkspacePage()
282-
const table = await screen.findByTestId("builds-table")
283-
284-
// Wait for the results to be loaded
285-
await waitFor(async () => {
286-
const rows = table.querySelectorAll("tbody > tr")
287-
// Added +1 because of the date row
288-
expect(rows).toHaveLength(MockBuilds.length + 1)
289-
})
290-
})
291-
})
314+
it("shows the timeline build", async () => {
315+
await renderWorkspacePage()
316+
const table = await screen.findByTestId("builds-table")
292317

293-
it("Workspace update when having new parameters on the template version", async () => {
294-
// Setup mocks
295-
const user = userEvent.setup()
296-
jest
297-
.spyOn(api, "getWorkspaceByOwnerAndName")
298-
.mockResolvedValueOnce(MockOutdatedWorkspace)
299-
const updateWorkspaceSpy = jest
300-
.spyOn(api, "updateWorkspace")
301-
.mockRejectedValueOnce(
302-
new api.MissingBuildParameters([
303-
MockTemplateVersionParameter1,
304-
MockTemplateVersionParameter2,
305-
]),
306-
)
307-
// Render page and wait for it to be loaded
308-
renderWithAuth(<WorkspacePage />, {
309-
route: `/@${MockWorkspace.owner_name}/${MockWorkspace.name}`,
310-
path: "/@:username/:workspace",
311-
})
312-
await waitForLoaderToBeRemoved()
313-
// Click on the update button
314-
const workspaceActions = screen.getByTestId("workspace-actions")
315-
await user.click(
316-
within(workspaceActions).getByRole("button", { name: "Update" }),
317-
)
318-
await waitFor(() => {
319-
expect(api.updateWorkspace).toBeCalled()
320-
// We want to clear this mock to use it later
321-
updateWorkspaceSpy.mockClear()
322-
})
323-
// Fill the parameters and send the form
324-
const dialog = await screen.findByTestId("dialog")
325-
const firstParameterInput = within(dialog).getByLabelText(
326-
MockTemplateVersionParameter1.name,
327-
{ exact: false },
328-
)
329-
await user.clear(firstParameterInput)
330-
await user.type(firstParameterInput, "some-value")
331-
const secondParameterInput = within(dialog).getByLabelText(
332-
MockTemplateVersionParameter2.name,
333-
{ exact: false },
334-
)
335-
await user.clear(secondParameterInput)
336-
await user.type(secondParameterInput, "2")
337-
await user.click(within(dialog).getByRole("button", { name: "Update" }))
338-
// Check if the update was called using the values from the form
339-
await waitFor(() => {
340-
expect(api.updateWorkspace).toBeCalledWith(MockOutdatedWorkspace, [
341-
{
342-
name: MockTemplateVersionParameter1.name,
343-
value: "some-value",
344-
},
345-
{
346-
name: MockTemplateVersionParameter2.name,
347-
value: "2",
348-
},
349-
])
318+
// Wait for the results to be loaded
319+
await waitFor(async () => {
320+
const rows = table.querySelectorAll("tbody > tr")
321+
// Added +1 because of the date row
322+
expect(rows).toHaveLength(MockBuilds.length + 1)
350323
})
351324
})
352325
})

0 commit comments

Comments
 (0)