Skip to content

Commit

Permalink
test: thunks
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperhodge committed Dec 18, 2024
1 parent 67fc392 commit 780d05b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
101 changes: 101 additions & 0 deletions src/optimizer-page/data/thunks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { startLinkCheck, fetchLinkCheckStatus } from "./thunks";
import * as api from "./api";

Check failure on line 2 in src/optimizer-page/data/thunks.test.js

View workflow job for this annotation

GitHub Actions / tests

Strings must use singlequote
import { LINK_CHECK_STATUSES } from "./constants";

Check failure on line 3 in src/optimizer-page/data/thunks.test.js

View workflow job for this annotation

GitHub Actions / tests

Strings must use singlequote
import { RequestStatus } from "../../data/constants";

Check failure on line 4 in src/optimizer-page/data/thunks.test.js

View workflow job for this annotation

GitHub Actions / tests

Strings must use singlequote
import mockApiResponse from "../mocks/mockApiResponse";

Check failure on line 5 in src/optimizer-page/data/thunks.test.js

View workflow job for this annotation

GitHub Actions / tests

Strings must use singlequote

describe("startLinkCheck thunk", () => {

Check failure on line 7 in src/optimizer-page/data/thunks.test.js

View workflow job for this annotation

GitHub Actions / tests

Strings must use singlequote
const dispatch = jest.fn();
Expand Down Expand Up @@ -69,3 +70,103 @@ describe("startLinkCheck thunk", () => {
});
});
});

describe("fetchLinkCheckStatus thunk", () => {
describe("successful request", () => {
it("should return scan result", async () => {
const dispatch = jest.fn();
const getState = jest.fn();
const courseId = "course-123";
const mockGetLinkCheckStatus = jest
.spyOn(api, "getLinkCheckStatus")
.mockResolvedValue({
linkCheckStatus: mockApiResponse.LinkCheckStatus,
linkCheckOutput: mockApiResponse.LinkCheckOutput,
linkCheckCreatedAt: mockApiResponse.LinkCheckCreatedAt,
});

await fetchLinkCheckStatus(courseId)(dispatch, getState);

expect(dispatch).toHaveBeenCalledWith({
payload: false,
type: "courseOptimizer/updateLinkCheckInProgress",
});

expect(dispatch).toHaveBeenCalledWith({
payload: 2,
type: "courseOptimizer/updateCurrentStage",
});

expect(dispatch).toHaveBeenCalledWith({
payload: mockApiResponse.LinkCheckOutput,
type: "courseOptimizer/updateLinkCheckResult",
});

expect(dispatch).toHaveBeenCalledWith({
payload: { status: RequestStatus.SUCCESSFUL },
type: "courseOptimizer/updateLoadingStatus",
});
});
});

describe("failed request", () => {
it("should set request status to failed", async () => {
const dispatch = jest.fn();
const getState = jest.fn();
const courseId = "course-123";
const mockGetLinkCheckStatus = jest
.spyOn(api, "getLinkCheckStatus")
.mockRejectedValue(new Error("error"));

await fetchLinkCheckStatus(courseId)(dispatch, getState);

expect(dispatch).toHaveBeenCalledWith({
payload: { status: RequestStatus.FAILED },
type: "courseOptimizer/updateLoadingStatus",
});
});
});

describe("failed scan", () => {
it("should set error message", async () => {
const mockGetLinkCheckStatus = jest
.spyOn(api, "getLinkCheckStatus")
.mockResolvedValue({
linkCheckStatus: LINK_CHECK_STATUSES.FAILED,
linkCheckOutput: mockApiResponse.LinkCheckOutput,
linkCheckCreatedAt: mockApiResponse.LinkCheckCreatedAt,
});

const dispatch = jest.fn();
const getState = jest.fn();
const courseId = "course-123";

await fetchLinkCheckStatus(courseId)(dispatch, getState);

expect(dispatch).toHaveBeenCalledWith({
payload: true,
type: "courseOptimizer/updateIsErrorModalOpen",
});

expect(dispatch).toHaveBeenCalledWith({
payload: { msg: "Link Check Failed" },
type: "courseOptimizer/updateError",
});

expect(dispatch).toHaveBeenCalledWith({
payload: { status: RequestStatus.SUCCESSFUL },
type: "courseOptimizer/updateLoadingStatus",
});

expect(dispatch).toHaveBeenCalledWith({
payload: -1,
type: "courseOptimizer/updateCurrentStage",
});

expect(dispatch).not.toHaveBeenCalledWith({
payload: expect.anything(),
type: "courseOptimizer/updateLinkCheckResult",
});
});
});
});
7 changes: 3 additions & 4 deletions src/optimizer-page/data/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function fetchLinkCheckStatus(courseId) {
} else {
dispatch(updateLinkCheckInProgress(false));
}
console.log('linkCheckStatus:', linkCheckStatus);

Check warning on line 58 in src/optimizer-page/data/thunks.ts

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement

dispatch(updateCurrentStage(SCAN_STAGES[linkCheckStatus]));

Expand All @@ -65,9 +66,7 @@ export function fetchLinkCheckStatus(courseId) {
) {
dispatch(updateError({ msg: 'Link Check Failed' }));
dispatch(updateIsErrorModalOpen(true));
}

if (linkCheckOutput) {
} else if (linkCheckOutput) {
dispatch(updateLinkCheckResult(linkCheckOutput));
}

Expand All @@ -78,7 +77,7 @@ export function fetchLinkCheckStatus(courseId) {
dispatch(updateLoadingStatus({ status: RequestStatus.DENIED }));
} else {
dispatch(
updateLoadingStatus({ courseId, status: RequestStatus.FAILED }),
updateLoadingStatus({ status: RequestStatus.FAILED }),
);
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/optimizer-page/mocks/mockApiResponse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const mockApiResponse = {
LinkCheckStatus: 'In-Progress',
LinkCheckStatus: 'Succeeded',
LinkCheckCreatedAt: '2024-12-14T00:26:50.838350Z',
LinkCheckOutput: {
sections: [
Expand Down

0 comments on commit 780d05b

Please sign in to comment.