Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinsj committed Jun 9, 2024
1 parent be7ceb7 commit 023c119
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/hooks/__tests__/useAuthContextValue.test.ts
Original file line number Diff line number Diff line change
@@ -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")
})
})





51 changes: 51 additions & 0 deletions src/hooks/__tests__/useGetAccessTokenEffect.test.ts
Original file line number Diff line number Diff line change
@@ -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"))
})
})
6 changes: 4 additions & 2 deletions src/utils/__tests__/stateCookie.test.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down

0 comments on commit 023c119

Please sign in to comment.