Skip to content

Commit

Permalink
Add unit test for zustand stores
Browse files Browse the repository at this point in the history
- Implemented tests for `userStore` to verify user state
  and persistence in session storage.
- Created tests for `authStore` to ensure authentication
  logic and state management function correctly.
  • Loading branch information
Fingertips18 committed Sep 28, 2024
1 parent 046a032 commit e0b05f3
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 0 deletions.
111 changes: 111 additions & 0 deletions client/src/lib/stores/auth-store.test.ts
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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();
});
});
85 changes: 85 additions & 0 deletions client/src/lib/stores/user-store.test.ts
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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();
});
});

0 comments on commit e0b05f3

Please sign in to comment.