diff --git a/src/hooks/__tests__/useAuthContextValue.test.ts b/src/hooks/__tests__/useAuthContextValue.test.ts new file mode 100644 index 0000000..f1699b0 --- /dev/null +++ b/src/hooks/__tests__/useAuthContextValue.test.ts @@ -0,0 +1,31 @@ +import { describe, test, expect } from "vitest" +import { renderHook } from "@testing-library/react" + +import useAuthContextValue from "@/hooks/useAuthContextValue" + +describe("useAuthContextValue", () => { + test("returns auth context", () => { + const { result } = renderHook(() => useAuthContextValue({})) + expect(result.current).toEqual({ + accessToken: null, + refreshToken: null, + setAccessToken: expect.any(Function), + setRefreshToken: expect.any(Function) + }) + }) + + test("updates access token and refresh token", () => { + const { result } = renderHook(() => useAuthContextValue({ + accessToken: "accessToken", + refreshToken: "refreshToken" + })) + + expect(result.current.accessToken).toBe("accessToken") + expect(result.current.refreshToken).toBe("refreshToken") + }) +}) + + + + + diff --git a/src/hooks/__tests__/useGetAccessTokenEffect.test.ts b/src/hooks/__tests__/useGetAccessTokenEffect.test.ts new file mode 100644 index 0000000..8b197d2 --- /dev/null +++ b/src/hooks/__tests__/useGetAccessTokenEffect.test.ts @@ -0,0 +1,51 @@ +import { describe, test, expect, vi } from "vitest" +import { renderHook } from "@testing-library/react" +import useGetAccessTokenEffect from "@/hooks/useGetAccessTokenEffect" + +describe("useGetAccessTokenEffect", () => { + test("returns null state", () => { + const { result } = renderHook(() => useGetAccessTokenEffect(null, null, undefined)) + expect(result.current).toEqual({ + isLoading: false, + error: null, + tokens: null + }) + }) + + test("returns access tokens", async () => { + vi.mock("@/apis/token", async () => ({ + postTokens: vi.fn(() => Promise.resolve({ + refresh_token: "refresh_token_jwt", access_token: "access_token_jwt" + })) + })) + + const { result } = renderHook( + () => useGetAccessTokenEffect("state", "code", "codeVerifier") + ) + + expect(result.current.isLoading).toBeTruthy() + expect(result.current.error).toBeFalsy() + + 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() + }) + }) + + test("captures error", async () => { + vi.mock("@/apis/token", async () => ({ + postTokens: vi.fn(() => Promise.reject(new Error("error"))) + })) + + const { result } = renderHook(() => + useGetAccessTokenEffect("state", "code", "codeVerifier")) + + expect(result.current.isLoading).toBeTruthy() + expect(result.current.error).toBeNull() + await vi.waitFor(() => expect(result.current.error).toBe("error")) + }) +}) diff --git a/src/utils/__tests__/stateCookie.test.ts b/src/utils/__tests__/stateCookie.test.ts index cf009da..81a1bf6 100644 --- a/src/utils/__tests__/stateCookie.test.ts +++ b/src/utils/__tests__/stateCookie.test.ts @@ -1,8 +1,10 @@ -import { describe, test, expect, beforeAll, afterAll, vi } from "vitest" +import { + describe, test, expect, beforeAll, afterAll, afterEach, vi +} from "vitest" import { getStateCookie, createStateCookie } from "../stateCookie" describe("createStateCookie", () => { - let mockCookie = [] + let mockCookie: string[] = [] beforeAll(() => { vi.stubGlobal('document', { get cookie() {