-
Notifications
You must be signed in to change notification settings - Fork 2
class search slice testing #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { describe, it, expect, beforeAll, afterEach, afterAll } from "vitest"; | ||
| import { renderWithProviders } from "@/utils/test-utils"; | ||
| import { setupServer } from "msw/node"; | ||
| import { http, HttpResponse } from "msw"; | ||
| import { | ||
| fetchSectionsAsync, | ||
| setFilters, | ||
| setPage, | ||
| } from "@/redux/classSearch/classSearchSlice"; | ||
| import { getInitialFilterValues } from "@/components/classSearch/courseFilters/helpers/constants"; | ||
| import { mockSections } from "@/utils/mockData"; | ||
| import { AppDispatch } from "@/redux/store"; | ||
|
|
||
| // Setup MSW server | ||
| const handlers = [ | ||
| http.get("http://localhost:4000/classSearch", () => { | ||
| return HttpResponse.json({ data: mockSections, page: 1, totalPages: 1 }); | ||
| }), | ||
| ]; | ||
|
|
||
| const server = setupServer(...handlers); | ||
|
|
||
| // Setup and teardown | ||
| beforeAll(() => server.listen({ onUnhandledRequest: "warn" })); | ||
| afterEach(() => server.resetHandlers()); | ||
| afterAll(() => server.close()); | ||
|
|
||
| describe("classSearchSlice", () => { | ||
| it("sets filters and page via reducers", () => { | ||
| const { store } = renderWithProviders(<div />); | ||
| const newFilters = { ...getInitialFilterValues(), subject: "TEST" }; | ||
|
|
||
| store.dispatch(setFilters(newFilters)); | ||
| store.dispatch(setPage(3)); | ||
|
|
||
| const state = store.getState().classSearch; | ||
| expect(state.filters.subject).toBe("TEST"); | ||
| expect(state.page).toBe(3); | ||
| }); | ||
|
|
||
| it("fetches sections and updates state via fetchSectionsAsync", async () => { | ||
| const { store } = renderWithProviders(<div />); | ||
| await (store.dispatch as AppDispatch)(fetchSectionsAsync()); | ||
|
|
||
| const state = store.getState().classSearch; | ||
| expect(state.loading).toBe(false); | ||
| expect(state.sections.length).toBe(1); | ||
|
Check failure on line 47 in Client/src/__tests__/redux/classSearchSlice.test.tsx
|
||
| expect(state.sections[0].classNumber).toBe(1234); | ||
| expect(state.page).toBe(1); | ||
| expect(state.totalPages).toBe(1); | ||
| }); | ||
|
|
||
| it("handles fetchSectionsAsync errors", async () => { | ||
| server.use( | ||
| http.get("http://localhost:4000/classSearch", () => { | ||
| return new HttpResponse(null, { status: 500 }); | ||
| }) | ||
| ); | ||
|
|
||
| const { store } = renderWithProviders(<div />); | ||
| await (store.dispatch as AppDispatch)(fetchSectionsAsync()); | ||
|
|
||
| const state = store.getState().classSearch; | ||
| expect(state.loading).toBe(false); | ||
| expect(state.error).toBeTruthy(); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,11 +1,10 @@ | ||||||
| import { MessageObjType } from "@polylink/shared/types"; | ||||||
| import { CourseTerm, MessageObjType, Section } from "@polylink/shared/types"; | ||||||
|
|
||||||
| export const mockMessages: MessageObjType[] = [ | ||||||
| { | ||||||
| id: "msg1", | ||||||
| sender: "user", | ||||||
| text: "Hey! Can you help me understand how machine learning works?", | ||||||
| model: "GPT-4", | ||||||
| userReaction: null, | ||||||
| }, | ||||||
| { | ||||||
|
|
@@ -19,7 +18,6 @@ export const mockMessages: MessageObjType[] = [ | |||||
| id: "msg3", | ||||||
| sender: "user", | ||||||
| text: "Yes, please! Can you give me a simple example?", | ||||||
| model: "GPT-4", | ||||||
| userReaction: null, | ||||||
| }, | ||||||
| { | ||||||
|
|
@@ -33,7 +31,6 @@ export const mockMessages: MessageObjType[] = [ | |||||
| id: "msg5", | ||||||
| sender: "user", | ||||||
| text: "That makes sense! What about neural networks?", | ||||||
| model: "GPT-4", | ||||||
| userReaction: null, | ||||||
| }, | ||||||
| { | ||||||
|
|
@@ -45,3 +42,35 @@ export const mockMessages: MessageObjType[] = [ | |||||
| thinkingState: false, | ||||||
| }, | ||||||
| ]; | ||||||
|
|
||||||
| export const mockSections: Section[] = [ | ||||||
| { | ||||||
| classNumber: 1234, | ||||||
| courseName: "Test Class", | ||||||
| term: "spring2025" as CourseTerm, | ||||||
| courseId: "COURSE1", | ||||||
| subject: "TEST", | ||||||
| catalogNumber: "101", | ||||||
| component: "LEC", | ||||||
| description: "Test Description", | ||||||
| prerequisites: null, | ||||||
| units: "3", | ||||||
| enrollmentStatus: "O", | ||||||
| enrollment: { | ||||||
| waitTotal: 0, | ||||||
| waitCap: 0, | ||||||
| classCapacity: 100, | ||||||
| enrollmentTotal: 50, | ||||||
| enrollmentAvailable: 50, | ||||||
| enrollmentStatusDescription: "Open", | ||||||
| }, | ||||||
| instructionMode: "PA", | ||||||
| courseAttributes: [], | ||||||
| meetings: [], | ||||||
| instructors: [], | ||||||
| instructorsWithRatings: null, | ||||||
| techElectives: [], | ||||||
| classPair: null, | ||||||
| isCreditNoCredit: false, | ||||||
| }, | ||||||
| ] as Section[]; | ||||||
|
||||||
| ] as Section[]; | |
| ]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move
mswintodevDependenciesinstead ofdependenciesto prevent including test utilities in production bundles.