Skip to content

Commit 1603526

Browse files
authored
TestRuns. Add pagination (#134)
1 parent 8869091 commit 1603526

File tree

4 files changed

+66
-23
lines changed

4 files changed

+66
-23
lines changed

src/_test/stub.helper.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ const buildsServiceStub = {
2121

2222
const testRunServiceStub = {
2323
getList: (testRuns: TestRun[]) =>
24-
cy.stub(testRunService, "getList").resolves(testRuns),
24+
cy
25+
.stub(testRunService, "getList")
26+
.resolves({ data: testRuns, total: 100, skip: 0, take: 10 }),
2527
};
2628

2729
export { projectStub, buildsServiceStub, testRunServiceStub };

src/contexts/testRun.context.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { TestRun } from "../types";
2+
import { PaginatedData, TestRun } from "../types";
33
import { testRunService } from "../services";
44

55
interface IRequestAction {
@@ -9,7 +9,7 @@ interface IRequestAction {
99

1010
interface IGetAction {
1111
type: "get";
12-
payload: TestRun[];
12+
payload: PaginatedData<TestRun>;
1313
}
1414

1515
interface ISelectAction {
@@ -63,6 +63,9 @@ type State = {
6363
selectedTestRunId: string | undefined;
6464
selectedTestRunIndex: number | undefined;
6565
testRuns: TestRun[];
66+
total: number;
67+
take: number;
68+
skip: number;
6669
loading: boolean;
6770
};
6871

@@ -77,6 +80,9 @@ const initialState: State = {
7780
selectedTestRunId: undefined,
7881
selectedTestRunIndex: undefined,
7982
testRuns: [],
83+
take: 10,
84+
skip: 0,
85+
total: 0,
8086
loading: false,
8187
};
8288

@@ -101,9 +107,13 @@ function testRunReducer(state: State, action: IAction): State {
101107
loading: true,
102108
};
103109
case "get":
110+
const { data, take, skip, total } = action.payload;
104111
return {
105112
...state,
106-
testRuns: action.payload,
113+
testRuns: data,
114+
take,
115+
skip,
116+
total,
107117
loading: false,
108118
};
109119
case "delete":
@@ -163,12 +173,18 @@ function useTestRunDispatch() {
163173
return context;
164174
}
165175

166-
async function getTestRunList(dispatch: Dispatch, buildId: string) {
176+
async function getTestRunList(
177+
dispatch: Dispatch,
178+
buildId: string,
179+
page: number
180+
): Promise<void> {
167181
dispatch({ type: "request" });
168182

169-
return testRunService.getList(buildId).then((items) => {
170-
dispatch({ type: "get", payload: items });
171-
});
183+
return testRunService
184+
.getList(buildId, initialState.take, initialState.take * (page - 1))
185+
.then((response) => {
186+
dispatch({ type: "get", payload: response });
187+
});
172188
}
173189

174190
async function deleteTestRun(dispatch: Dispatch, id: string) {

src/pages/ProjectPage.tsx

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ const ProjectPage = () => {
5858
const buildDispatch = useBuildDispatch();
5959
const { selectedProjectId } = useProjectState();
6060
const projectDispatch = useProjectDispatch();
61-
const { testRuns, selectedTestRunIndex } = useTestRunState();
61+
const {
62+
testRuns,
63+
selectedTestRunIndex,
64+
total: testRunTotal,
65+
take: testRunTake,
66+
} = useTestRunState();
6267
const testRunDispatch = useTestRunDispatch();
6368

6469
// filter
@@ -81,16 +86,6 @@ const ProjectPage = () => {
8186
}
8287
}, [projectId, projectDispatch]);
8388

84-
useEffect(() => {
85-
if (selectedBuildId) {
86-
getTestRunList(testRunDispatch, selectedBuildId).catch((err) =>
87-
enqueueSnackbar(err, {
88-
variant: "error",
89-
})
90-
);
91-
}
92-
}, [selectedBuildId, testRunDispatch, enqueueSnackbar]);
93-
9489
useEffect(() => {
9590
if (queryParams.buildId) {
9691
selectBuild(buildDispatch, queryParams.buildId);
@@ -117,6 +112,18 @@ const ProjectPage = () => {
117112
);
118113
}, [query, os, device, browser, viewport, testStatus, testRuns]);
119114

115+
const getTestRunListCalback: any = React.useCallback(
116+
(page: number) =>
117+
selectedBuildId &&
118+
getTestRunList(testRunDispatch, selectedBuildId, page).catch(
119+
(err: string) =>
120+
enqueueSnackbar(err, {
121+
variant: "error",
122+
})
123+
),
124+
[testRunDispatch, enqueueSnackbar, selectedBuildId]
125+
);
126+
120127
const getBuildListCalback: any = React.useCallback(
121128
(page: number) =>
122129
selectedProjectId &&
@@ -129,6 +136,10 @@ const ProjectPage = () => {
129136
[buildDispatch, enqueueSnackbar, selectedProjectId]
130137
);
131138

139+
React.useEffect(() => {
140+
getTestRunListCalback(1);
141+
}, [getTestRunListCalback]);
142+
132143
React.useEffect(() => {
133144
getBuildListCalback(1);
134145
}, [getBuildListCalback]);
@@ -162,9 +173,19 @@ const ProjectPage = () => {
162173
viewportState={[viewport, setViewport]}
163174
testStatusState={[testStatus, setTestStatus]}
164175
/>
165-
<Box height="75%">
176+
<Box height="70%" my={0.5}>
166177
<TestRunList items={filteredTestRuns} />
167178
</Box>
179+
<Grid container justify="center">
180+
<Grid item>
181+
<Pagination
182+
size="small"
183+
defaultPage={1}
184+
count={Math.ceil(testRunTotal / testRunTake)}
185+
onChange={(event, page) => getTestRunListCalback(page)}
186+
/>
187+
</Grid>
188+
</Grid>
168189

169190
{selectedTestRunIndex !== undefined && testRuns[selectedTestRunIndex] && (
170191
<Dialog open={true} fullScreen className={classes.modal}>

src/services/testRun.service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
import { TestRun } from "../types";
1+
import { PaginatedData, TestRun } from "../types";
22
import { handleResponse, authHeader } from "../_helpers/service.helpers";
33
import { API_URL } from "../_config/env.config";
44
import { IgnoreArea } from "../types/ignoreArea";
55

66
const ENDPOINT_URL = "/test-runs";
77

8-
async function getList(buildId: string): Promise<TestRun[]> {
8+
async function getList(
9+
buildId: string,
10+
take: number,
11+
skip: number
12+
): Promise<PaginatedData<TestRun>> {
913
const requestOptions = {
1014
method: "GET",
1115
headers: authHeader(),
1216
};
1317

1418
return fetch(
15-
`${API_URL}${ENDPOINT_URL}?buildId=${buildId}`,
19+
`${API_URL}${ENDPOINT_URL}?buildId=${buildId}&take=${take}&skip=${skip}`,
1620
requestOptions
1721
).then(handleResponse);
1822
}

0 commit comments

Comments
 (0)