import { renderHook, act } from "@testing-library/react"; import { sleep } from "@reins/utils"; import { useCountdown } from "hooks"; describe("useCountdown [ Base ]", () => { beforeEach(() => { jest.resetModules(); jest.restoreAllMocks(); }); describe("when end time is in future", () => { it("should generate correct countdown", async () => { const { result } = renderHook(() => useCountdown(+new Date() + 61000)); expect(result.current).toStrictEqual({ days: 0, hours: 0, minutes: 1, seconds: 1, total: 61000, }); await act(async () => { await sleep(1000); }); expect(result.current).toStrictEqual({ days: 0, hours: 0, minutes: 1, seconds: 0, total: 60000, }); await act(async () => { await sleep(1000); }); expect(result.current).toStrictEqual({ days: 0, hours: 0, minutes: 0, seconds: 59, total: 59000, }); }); }); describe("when end time is in the past", () => { it("should generate negative countdown values", async () => { const { result } = renderHook(() => useCountdown(+new Date() - 1000, { countInPast: true })); expect(result.current).toStrictEqual({ days: -0, hours: -0, minutes: -0, seconds: -1, total: -1000, }); await act(async () => { await sleep(1000); }); expect(result.current).toStrictEqual({ days: -0, hours: -0, minutes: -0, seconds: -2, total: -2000, }); await act(async () => { await sleep(1000); }); expect(result.current).toStrictEqual({ days: -0, hours: -0, minutes: -0, seconds: -3, total: -3000, }); }); it("should stop countdown when date is in past", async () => { const { result } = renderHook(() => useCountdown(+new Date() - 1000, { countInPast: false })); expect(result.current).toStrictEqual({ days: 0, hours: 0, minutes: 0, seconds: 0, total: 0, }); await act(async () => { await sleep(1000); }); expect(result.current).toStrictEqual({ days: 0, hours: 0, minutes: 0, seconds: 0, total: 0, }); await act(async () => { await sleep(1000); }); expect(result.current).toStrictEqual({ days: 0, hours: 0, minutes: 0, seconds: 0, total: 0, }); }); }); });