diff --git a/starsky/starsky/clientapp/src/components/molecules/menu-option-rotate-image-90/internal/trigger-file-hash-request.spec.tsx b/starsky/starsky/clientapp/src/components/molecules/menu-option-rotate-image-90/internal/trigger-file-hash-request.spec.tsx index e29dffa0c..476240686 100644 --- a/starsky/starsky/clientapp/src/components/molecules/menu-option-rotate-image-90/internal/trigger-file-hash-request.spec.tsx +++ b/starsky/starsky/clientapp/src/components/molecules/menu-option-rotate-image-90/internal/trigger-file-hash-request.spec.tsx @@ -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); @@ -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); }); });