diff --git a/client/src/lib/stores/auth-store.test.ts b/client/src/lib/stores/auth-store.test.ts new file mode 100644 index 0000000..c81b442 --- /dev/null +++ b/client/src/lib/stores/auth-store.test.ts @@ -0,0 +1,111 @@ +import { beforeEach, describe, expect, it } from "vitest"; +import { act, renderHook } from "@testing-library/react"; + +import { useAuthStore } from "./auth-store"; + +const auth = { + email: "test@example.com", + authorized: true, + loading: true, +}; + +describe("authStore", () => { + beforeEach(() => { + sessionStorage.clear(); + }); + + it("should have initial state", () => { + const { result } = renderHook(() => useAuthStore()); + + expect(result.current.email).toBe(""); + expect(result.current.authorized).toBeFalsy(); + expect(result.current.loading).toBeFalsy(); + }); + + it("should set email correctly", () => { + const { result } = renderHook(() => useAuthStore()); + + act(() => { + result.current.setEmail(auth.email); + }); + + expect(result.current.email).toEqual(auth.email); + }); + + it("should set authorized correctly", () => { + const { result } = renderHook(() => useAuthStore()); + + act(() => { + result.current.setAuthorized(true); + }); + + expect(result.current.authorized).toEqual(auth.authorized); + }); + + it("should set loading correctly", () => { + const { result } = renderHook(() => useAuthStore()); + + act(() => { + result.current.setLoading(true); + }); + + expect(result.current.loading).toEqual(auth.loading); + }); + + it("should persist data in session storage", () => { + const { result } = renderHook(() => useAuthStore()); + + act(() => { + result.current.setEmail(auth.email); + result.current.setAuthorized(true); + result.current.setLoading(true); + }); + + const storedData = sessionStorage.getItem("go-react-auth"); + expect(storedData).toBeTruthy(); + }); + + it("should persist email and authorized only and it should be the same", () => { + const { result } = renderHook(() => useAuthStore()); + + act(() => { + result.current.setEmail(auth.email); + result.current.setAuthorized(true); + result.current.setLoading(true); + }); + + const storedData = sessionStorage.getItem("go-react-auth"); + const parsedData = storedData ? JSON.parse(storedData) : ""; + const email = parsedData.state.email; + const authorized = parsedData.state.authorized; + const loading = parsedData.state.loading; + + expect(email).toEqual(auth.email); + expect(authorized).toEqual(auth.authorized); + expect(loading).toBeUndefined(); + }); + + it("should reset", () => { + const { result } = renderHook(() => useAuthStore()); + + act(() => { + result.current.setEmail(auth.email); + result.current.setAuthorized(true); + result.current.setLoading(true); + }); + + expect(result.current.email).toEqual(auth.email); + expect(result.current.authorized).toBeTruthy(); + expect(result.current.loading).toBeTruthy(); + + act(() => { + result.current.setEmail(""); + result.current.setAuthorized(false); + result.current.setLoading(false); + }); + + expect(result.current.email).toEqual(""); + expect(result.current.authorized).toBeFalsy(); + expect(result.current.loading).toBeFalsy(); + }); +}); diff --git a/client/src/lib/stores/user-store.test.ts b/client/src/lib/stores/user-store.test.ts new file mode 100644 index 0000000..223128d --- /dev/null +++ b/client/src/lib/stores/user-store.test.ts @@ -0,0 +1,85 @@ +import { beforeEach, describe, expect, it, test } from "vitest"; +import { act, renderHook } from "@testing-library/react"; + +import { UserDTO } from "@/lib/DTO/user-dto"; + +import { useUserStore } from "./user-store"; + +const user: UserDTO = { + id: "abc123", + username: "Test", + email_address: "test@example.com", + last_signed_in: new Date(), + is_verified: false, + created_at: new Date(), + updated_at: new Date(), +}; + +describe("userStore", () => { + beforeEach(() => { + sessionStorage.clear(); + }); + + it("should have initial state", () => { + const { result } = renderHook(() => useUserStore()); + expect(result.current.user).toBeUndefined(); + }); + + it("should set user correctly", () => { + const { result } = renderHook(() => useUserStore()); + + act(() => { + result.current.setUser(user); + }); + + expect(result.current.user).toEqual(user); + }); + + it("should persist user in session storage", () => { + const { result } = renderHook(() => useUserStore()); + + act(() => { + result.current.setUser(user); + }); + + const storedData = sessionStorage.getItem("go-react-user"); + expect(storedData).toBeTruthy(); + }); + + test("stored user should be the same", () => { + const { result } = renderHook(() => useUserStore()); + + act(() => { + result.current.setUser(user); + }); + + const storedData = sessionStorage.getItem("go-react-user"); + const parseData = storedData ? JSON.parse(storedData) : ""; + const storedUser = parseData.state.user; + + const parsedUser = { + ...storedUser, + last_signed_in: new Date(storedUser.last_signed_in), + created_at: new Date(storedUser.created_at), + updated_at: new Date(storedUser.updated_at), + }; + + expect(parsedUser).toEqual(user); + }); + + it("should reset", () => { + const { result } = renderHook(() => useUserStore()); + + act(() => { + result.current.setUser(user); + }); + + expect(result.current.user).toEqual(user); + + act(() => { + result.current.setUser(undefined); + }); + + expect(result.current.user).toBeUndefined(); + }); +});