diff --git a/src/hooks/__tests__/useGetAccessToken.test.ts b/src/hooks/__tests__/useGetAccessToken.test.ts index 87932f6..ab95cce 100644 --- a/src/hooks/__tests__/useGetAccessToken.test.ts +++ b/src/hooks/__tests__/useGetAccessToken.test.ts @@ -1,15 +1,17 @@ import { describe, test, expect, vi, afterEach } from "vitest" import { act, renderHook } from "@testing-library/react" -import useGetAccessToken from "@/hooks/useGetAccessToken" -import { deleteRefreshToken } from "@/utils/token" +import useGetAccessToken from "@/hooks/useGetAccessToken" +import { deleteRefreshToken } from "@/utils/refreshToken" +import { deleteStateCookie } from "@/utils/stateCookie" describe("useGetAccessToken", () => { afterEach(() => { + vi.unstubAllGlobals() vi.resetAllMocks() }) - test("returns null state", () => { + test("returns initial state", () => { const { result } = renderHook(() => useGetAccessToken()) expect(result.current).toEqual({ getATWithAuthCode: expect.any(Function), @@ -23,64 +25,129 @@ describe("useGetAccessToken", () => { }) }) - test("returns access tokens", async () => { - vi.mock("@/apis/token", async () => ({ - postToken: vi.fn(() => Promise.resolve({ - refresh_token: "refresh_token_jwt", access_token: "access_token_jwt" + + describe("auth token", () => { + test("returns access tokens", async () => { + const mockResponse = { + access_token: 'access_token_jwt', + refresh_token: 'refresh_token_jwt', + expires_at: 1234567890 + } + // FIXME unstable module mocking was replaced by fetch mock + vi.stubGlobal('fetch', () => Promise.resolve({ + ok: true, + json: () => Promise.resolve(mockResponse) })) - })) - const { result } = renderHook( - () => useGetAccessToken() - ) + const { result } = renderHook(() => useGetAccessToken()) + + act(() => { + result.current.getATWithAuthCode("state", "code", "codeVerifier") + }) + expect(result.current.isLoading).toBeTruthy() + expect(result.current.error).toBeFalsy() - act(() => { - result.current.getATWithAuthCode("state", "code", "codeVerifier") + await vi.waitFor(() => { + expect(result.current.tokens).toEqual({ + accessToken: "access_token_jwt", + refreshToken: "refresh_token_jwt" + }) + expect(result.current.isLoading).toBeFalsy() + expect(result.current.error).toBeFalsy() + }) }) - expect(result.current.isLoading).toBeTruthy() - expect(result.current.error).toBeFalsy() + test("captures error and delete state cookie", async () => { + // FIXME unstable module mocking was replaced by fetch mock + vi.stubGlobal('fetch', () => Promise.resolve({ + ok: false, + status: 400, + statusText: 'Bad Request' + })) + vi.mock('@/utils/stateCookie', () => ({ + deleteStateCookie: vi.fn() + })) + + const { result } = renderHook(() => useGetAccessToken()) - await vi.waitFor(() => { - expect(result.current.tokens).toEqual({ - accessToken: "access_token_jwt", - refreshToken: "refresh_token_jwt" + act(() => { + result.current.getATWithAuthCode("state", "code", "codeVerifier") }) - expect(result.current.isLoading).toBeFalsy() - expect(result.current.error).toBeFalsy() - }) - vi.unmock("@/apis/token") + expect(result.current.isLoading).toBeTruthy() + expect(result.current.error).toBeNull() + + await vi.waitFor(() => { + expect(result.current.tokens).toEqual({ + accessToken: null, + refreshToken: null + }) + expect(result.current.isLoading).toBeFalsy() + expect(result.current.error).toBe("Bad Request") + expect(deleteStateCookie).toHaveBeenCalled() + }) + }) }) + describe("refresh token", () => { + test("returns access tokens", async () => { + const mockResponse = { + access_token: 'access_token_jwt', + refresh_token: 'refresh_token_jwt', + expires_at: 1234567890 + } + // FIXME unstable module mocking was replaced by fetch mock + vi.stubGlobal('fetch', () => Promise.resolve({ + ok: true, + json: () => Promise.resolve(mockResponse) + })) - test("captures error", async () => { - vi.mock("@/apis/token", async () => ({ - postToken: vi.fn(() => Promise.reject(new Error("error"))) - })) - vi.mock("@/utils/token", async () => ({ - deleteRefreshToken: vi.fn() - })) + const { result } = renderHook(() => useGetAccessToken()) - const { result } = renderHook(() => - useGetAccessToken() - ) + act(() => { + result.current.getATWithAuthCode("state", "code", "codeVerifier") + }) + expect(result.current.isLoading).toBeTruthy() + expect(result.current.error).toBeFalsy() - act(() => { - result.current.getATWithAuthCode("state", "code", "codeVerifier") + await vi.waitFor(() => { + expect(result.current.tokens).toEqual({ + accessToken: "access_token_jwt", + refreshToken: "refresh_token_jwt" + }) + expect(result.current.isLoading).toBeFalsy() + expect(result.current.error).toBeFalsy() + }) }) - expect(result.current.isLoading).toBeTruthy() - expect(result.current.error).toBeNull() + test("captures error and delete refresh token", async () => { + // FIXME unstable module mocking was replaced by fetch mock + vi.stubGlobal('fetch', () => Promise.resolve({ + ok: false, + status: 400, + statusText: 'Bad Request' + })) + vi.mock('@/utils/refreshToken', () => ({ + deleteRefreshToken: vi.fn() + })) - await vi.waitFor(() => { - expect(result.current.tokens).toEqual({ - accessToken: null, - refreshToken: null + const { result } = renderHook(() => useGetAccessToken()) + + act(() => { + result.current.getATWithRefreshToken("mock_refresh_token") + }) + + expect(result.current.isLoading).toBeTruthy() + expect(result.current.error).toBeNull() + + await vi.waitFor(() => { + expect(result.current.tokens).toEqual({ + accessToken: null, + refreshToken: null + }) + expect(result.current.isLoading).toBeFalsy() + expect(result.current.error).toBe("Bad Request") + expect(deleteRefreshToken).toHaveBeenCalled() }) - expect(result.current.isLoading).toBeFalsy() - expect(result.current.error).toBe("error") - expect(deleteRefreshToken).toHaveBeenCalledWith('sd') }) - vi.unmock("@/apis/token") }) })