Skip to content

Commit

Permalink
add extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Oct 16, 2024
1 parent 481b012 commit 75d27c1
Showing 1 changed file with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ describe("TriggerFileHashRequest", () => {
}
} as IDetailView;

it("returns immediately when the file hash changes on the first attempt", () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

it("returns immediately when the file hash changes on the first attempt", () => {
const requestSpy = jest
.spyOn(RequestNewFileHash, "RequestNewFileHash")
.mockResolvedValueOnce(true);
Expand All @@ -31,6 +37,42 @@ describe("TriggerFileHashRequest", () => {
jest.advanceTimersByTime(1);
jest.runAllTimers();
expect(requestSpy).toHaveBeenCalledTimes(1);
jest.useRealTimers();
});

it("retry when failed", () => {
const requestSpy = jest
.spyOn(RequestNewFileHash, "RequestNewFileHash")
.mockResolvedValueOnce(false);

const setIsLoading = jest.fn();
TriggerFileHashRequest(state, setIsLoading, jest.fn(), 1, 1);

act(() => {
jest.advanceTimersByTime(100);
});

jest.advanceTimersByTime(1);
jest.runAllTimers();
expect(requestSpy).toHaveBeenCalledTimes(2);
});

it("retry when failed (max 3 times)", () => {
const requestSpy = jest
.spyOn(RequestNewFileHash, "RequestNewFileHash")
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(false);

const setIsLoading = jest.fn();
TriggerFileHashRequest(state, setIsLoading, jest.fn(), 1, 1);

act(() => {
jest.advanceTimersByTime(100);
});

jest.advanceTimersByTime(1);
jest.runAllTimers();
expect(requestSpy).toHaveBeenCalledTimes(3);
});
});

0 comments on commit 75d27c1

Please sign in to comment.