From 4a4e836edc0a2601616650755d940c6c931a3adc Mon Sep 17 00:00:00 2001 From: Ghian Date: Sun, 29 Sep 2024 17:30:20 +0800 Subject: [PATCH] Configure test workflow for client (#12) * Configure test workflow for client Verified that the test file naming conventions are followed, allowing for proper detection of test cases across the project. * Resolve test issues by mocking useUserStore and removing updatedAt property - Added a mock for `useUserStore` to resolve test failures and ensure consistent behavior during testing. - Removed the `updatedAt` property from the `UserDTO` as it is currently not needed, simplifying the data model and improving test reliability. - Updated related test cases to reflect these changes and maintain coverage. --- .github/workflows/client-test.yaml | 33 +++++++++++++++++ client/src/guards/auth-guard.test.tsx | 2 +- client/src/guards/private-guard.test.tsx | 35 +++++++++++++++++++ client/src/lib/DTO/user-dto.ts | 1 - .../src/lib/providers/toast-provider.test.tsx | 2 +- client/src/lib/stores/user-store.test.ts | 2 -- 6 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/client-test.yaml diff --git a/.github/workflows/client-test.yaml b/.github/workflows/client-test.yaml new file mode 100644 index 0000000..fc62423 --- /dev/null +++ b/.github/workflows/client-test.yaml @@ -0,0 +1,33 @@ +name: Client Test Workflow + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + test: + name: Run Client Tests + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: 18 + + - name: Install dependencies + run: | + cd client + npm install + + - name: Run vitest + run: | + cd client + npm run test diff --git a/client/src/guards/auth-guard.test.tsx b/client/src/guards/auth-guard.test.tsx index 09c4f4a..a78ed01 100644 --- a/client/src/guards/auth-guard.test.tsx +++ b/client/src/guards/auth-guard.test.tsx @@ -11,7 +11,7 @@ import { AppRoutes } from "@/constants/routes"; import AuthGuard from "./auth-guard"; -vi.mock("../lib/stores/auth-store.ts", () => ({ +vi.mock("@/lib/stores/auth-store", () => ({ useAuthStore: vi.fn(), })); diff --git a/client/src/guards/private-guard.test.tsx b/client/src/guards/private-guard.test.tsx index d95e121..c128d59 100644 --- a/client/src/guards/private-guard.test.tsx +++ b/client/src/guards/private-guard.test.tsx @@ -9,6 +9,7 @@ import { render } from "@testing-library/react"; import "@testing-library/jest-dom"; import { useAuthStore } from "@/lib/stores/auth-store"; +import { useUserStore } from "@/lib/stores/user-store"; import { AppRoutes } from "@/constants/routes"; import PrivateGuard from "./private-guard"; @@ -20,11 +21,27 @@ vi.mock("@/lib/stores/auth-store", () => ({ }), })); +vi.mock("@/lib/stores/user-store", () => ({ + useUserStore: vi.fn().mockReturnValueOnce({ + setUser: vi.fn(), + }), +})); + vi.mock("@tanstack/react-query", () => ({ useQuery: vi.fn().mockReturnValueOnce({ isLoading: false, isError: false, isSuccess: false, + data: { + user: { + id: "abc123", + username: "Test", + email_address: "test@example.com", + last_signed_in: new Date(), + is_verified: false, + created_at: new Date(), + }, + }, }), })); @@ -60,6 +77,10 @@ describe("Private Guard", () => { initialEntries: [AppRoutes.Root], }); + (useUserStore as unknown as Mock).mockRejectedValueOnce({ + setUser: vi.fn(), + }); + (useAuthStore as unknown as Mock).mockReturnValueOnce({ authenticated: true, setAuthorized: vi.fn(), @@ -69,6 +90,9 @@ describe("Private Guard", () => { isError: true, isLoading: false, isSuccess: false, + data: { + user: undefined, + }, }); render(); @@ -81,6 +105,10 @@ describe("Private Guard", () => { initialEntries: [AppRoutes.Root], }); + (useUserStore as unknown as Mock).mockReturnValueOnce({ + setUser: vi.fn(), + }); + (useAuthStore as unknown as Mock).mockReturnValueOnce({ authorized: true, setAuthorized: vi.fn(), @@ -90,6 +118,9 @@ describe("Private Guard", () => { isError: false, isLoading: false, isSuccess: true, + data: { + user: undefined, + }, }); render(); @@ -102,6 +133,10 @@ describe("Private Guard", () => { initialEntries: [AppRoutes.Root], }); + (useUserStore as unknown as Mock).mockRejectedValueOnce({ + useUser: vi.fn(), + }); + (useAuthStore as unknown as Mock).mockReturnValueOnce({ authorized: true, setAuthorized: vi.fn(), diff --git a/client/src/lib/DTO/user-dto.ts b/client/src/lib/DTO/user-dto.ts index cca01dd..7e1fc22 100644 --- a/client/src/lib/DTO/user-dto.ts +++ b/client/src/lib/DTO/user-dto.ts @@ -5,5 +5,4 @@ export type UserDTO = { last_signed_in: Date; is_verified: boolean; created_at: Date; - updated_at: Date; }; diff --git a/client/src/lib/providers/toast-provider.test.tsx b/client/src/lib/providers/toast-provider.test.tsx index 3cf7a0b..b17f2cd 100644 --- a/client/src/lib/providers/toast-provider.test.tsx +++ b/client/src/lib/providers/toast-provider.test.tsx @@ -7,7 +7,7 @@ import { useTheme } from "@/lib/hooks/use-theme"; import ToastProvider from "./toast-provider"; -vi.mock("../hooks/use-theme.tsx", () => ({ +vi.mock("@/lib/hooks/use-theme", () => ({ useTheme: vi.fn(), })); diff --git a/client/src/lib/stores/user-store.test.ts b/client/src/lib/stores/user-store.test.ts index 223128d..fd022fd 100644 --- a/client/src/lib/stores/user-store.test.ts +++ b/client/src/lib/stores/user-store.test.ts @@ -12,7 +12,6 @@ const user: UserDTO = { last_signed_in: new Date(), is_verified: false, created_at: new Date(), - updated_at: new Date(), }; describe("userStore", () => { @@ -61,7 +60,6 @@ describe("userStore", () => { ...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);