Skip to content

Commit

Permalink
Configure test workflow for client (#12)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Fingertips18 authored Sep 29, 2024
1 parent 0b42037 commit 4a4e836
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/client-test.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion client/src/guards/auth-guard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}));

Expand Down
35 changes: 35 additions & 0 deletions client/src/guards/private-guard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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: "[email protected]",
last_signed_in: new Date(),
is_verified: false,
created_at: new Date(),
},
},
}),
}));

Expand Down Expand Up @@ -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(),
Expand All @@ -69,6 +90,9 @@ describe("Private Guard", () => {
isError: true,
isLoading: false,
isSuccess: false,
data: {
user: undefined,
},
});

render(<RouterProvider router={router} />);
Expand All @@ -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(),
Expand All @@ -90,6 +118,9 @@ describe("Private Guard", () => {
isError: false,
isLoading: false,
isSuccess: true,
data: {
user: undefined,
},
});

render(<RouterProvider router={router} />);
Expand All @@ -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(),
Expand Down
1 change: 0 additions & 1 deletion client/src/lib/DTO/user-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ export type UserDTO = {
last_signed_in: Date;
is_verified: boolean;
created_at: Date;
updated_at: Date;
};
2 changes: 1 addition & 1 deletion client/src/lib/providers/toast-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}));

Expand Down
2 changes: 0 additions & 2 deletions client/src/lib/stores/user-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4a4e836

Please sign in to comment.