Skip to content

Commit 54440af

Browse files

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

site/src/api/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,9 +1187,9 @@ class ApiMethods {
11871187
};
11881188

11891189
getWorkspaces = async (
1190-
options: TypesGen.WorkspacesRequest,
1190+
req: TypesGen.WorkspacesRequest,
11911191
): Promise<TypesGen.WorkspacesResponse> => {
1192-
const url = getURLWithSearchParams("/api/v2/workspaces", options);
1192+
const url = getURLWithSearchParams("/api/v2/workspaces", req);
11931193
const response = await this.axios.get<TypesGen.WorkspacesResponse>(url);
11941194
return response.data;
11951195
};

site/src/api/queries/workspaces.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,14 @@ async function findMatchWorkspace(q: string): Promise<Workspace | undefined> {
139139
}
140140
}
141141

142-
function workspacesKey(config: WorkspacesRequest = {}) {
143-
const { q, limit } = config;
144-
return ["workspaces", { q, limit }] as const;
142+
function workspacesKey(req: WorkspacesRequest = {}) {
143+
return ["workspaces", req] as const;
145144
}
146145

147-
export function workspaces(config: WorkspacesRequest = {}) {
146+
export function workspaces(req: WorkspacesRequest = {}) {
148147
return {
149-
queryKey: workspacesKey(config),
150-
queryFn: () => API.getWorkspaces(config),
148+
queryKey: workspacesKey(req),
149+
queryFn: () => API.getWorkspaces(req),
151150
} as const satisfies QueryOptions<WorkspacesResponse>;
152151
}
153152

site/src/pages/WorkspacesPage/WorkspacesPage.test.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,67 @@ describe("WorkspacesPage", () => {
305305
MockStoppedWorkspace.latest_build.template_version_id,
306306
);
307307
});
308+
309+
it("correctly handles pagination by including pagination parameters in query key", async () => {
310+
const totalWorkspaces = 50;
311+
const workspacesPage1 = Array.from({ length: 25 }, (_, i) => ({
312+
...MockWorkspace,
313+
id: `page1-workspace-${i}`,
314+
name: `page1-workspace-${i}`,
315+
}));
316+
const workspacesPage2 = Array.from({ length: 25 }, (_, i) => ({
317+
...MockWorkspace,
318+
id: `page2-workspace-${i}`,
319+
name: `page2-workspace-${i}`,
320+
}));
321+
322+
const getWorkspacesSpy = jest.spyOn(API, "getWorkspaces");
323+
324+
getWorkspacesSpy.mockImplementation(({ offset }) => {
325+
switch (offset) {
326+
case 0:
327+
return Promise.resolve({
328+
workspaces: workspacesPage1,
329+
count: totalWorkspaces,
330+
});
331+
case 25:
332+
return Promise.resolve({
333+
workspaces: workspacesPage2,
334+
count: totalWorkspaces,
335+
});
336+
default:
337+
return Promise.reject(new Error("Unexpected offset"));
338+
}
339+
});
340+
341+
const user = userEvent.setup();
342+
renderWithAuth(<WorkspacesPage />);
343+
344+
await waitFor(() => {
345+
expect(screen.getByText("page1-workspace-0")).toBeInTheDocument();
346+
});
347+
348+
expect(getWorkspacesSpy).toHaveBeenLastCalledWith({
349+
q: "owner:me",
350+
offset: 0,
351+
limit: 25,
352+
});
353+
354+
const nextPageButton = screen.getByRole("button", { name: /next page/i });
355+
await user.click(nextPageButton);
356+
357+
await waitFor(() => {
358+
expect(screen.getByText("page2-workspace-0")).toBeInTheDocument();
359+
});
360+
361+
expect(getWorkspacesSpy).toHaveBeenLastCalledWith({
362+
q: "owner:me",
363+
offset: 25,
364+
limit: 25,
365+
});
366+
367+
expect(screen.queryByText("page1-workspace-0")).not.toBeInTheDocument();
368+
});
308369
});
309370

310371
const getWorkspaceCheckbox = (workspace: Workspace) => {

site/src/pages/WorkspacesPage/WorkspacesPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ const WorkspacesPage: FC = () => {
116116
});
117117

118118
const workspacesQueryOptions = workspaces({
119-
...pagination,
119+
limit: pagination.limit,
120+
offset: pagination.offset,
120121
q: filterState.filter.query,
121122
});
122123
const { data, error, refetch } = useQuery({

0 commit comments

Comments
 (0)