Skip to content

class search slice testing#377

Open
Castro19 wants to merge 1 commit intomainfrom
class-search-slice-test
Open

class search slice testing#377
Castro19 wants to merge 1 commit intomainfrom
class-search-slice-test

Conversation

@Castro19
Copy link
Copy Markdown
Owner

📌 Summary

Added comprehensive test suite for the class search functionality using MSW (Mock Service Worker) for API mocking and Vitest for testing. This PR introduces unit tests for the Redux class search slice, ensuring proper state management and error handling.

�� Related Issues

Closes #N/A

🛠 Changes Made

  • Added MSW dependency for API mocking in tests
  • Created new test file classSearchSlice.test.tsx with comprehensive test coverage
  • Added mock data for sections in mockData.ts
  • Implemented tests for:
    • Filter and page state management
    • Successful section fetching
    • Error handling during section fetching
  • Removed unused model field from mock messages
  • Added proper TypeScript types for mock data

✅ Checklist

  • My code follows the PolyLink Contribution Guidelines
  • I have tested my changes to ensure they work as expected
  • I have documented my changes (if applicable)
  • My PR has a clear title and description

📸 Screenshots (if applicable)

N/A - Test implementation only

❓ Additional Notes

The test suite uses MSW to mock the API responses, which allows for testing both successful and error scenarios without making actual network requests. The tests cover the main functionality of the class search feature including:

  • State management for filters and pagination
  • API integration with proper error handling
  • Data transformation and storage in Redux state

The implementation follows Redux best practices and ensures proper type safety with TypeScript.

@Castro19 Castro19 self-assigned this May 28, 2025
@Castro19 Castro19 requested a review from Copilot May 28, 2025 19:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a full test suite for the Redux class search slice using MSW for API mocking and Vitest for testing.

  • Added mockSections data and removed the unused model field in mock messages.
  • Created classSearchSlice.test.tsx with tests for filter/page reducers, successful fetch, and error handling.
  • Installed MSW in package.json to support request interception in tests.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
Client/src/utils/mockData.ts Defined mockSections: Section[] and updated type imports
Client/src/tests/redux/classSearchSlice.test.tsx Added MSW server handlers and tests for fetchSectionsAsync
Client/package.json Added msw dependency for API mocking in tests
Comments suppressed due to low confidence (2)

Client/src/tests/redux/classSearchSlice.test.tsx:1

  • [nitpick] The test file has no JSX; consider renaming it from .test.tsx to .test.ts to better reflect its content.
import { describe, it, expect, beforeAll, afterEach, afterAll } from "vitest";

Client/src/tests/redux/classSearchSlice.test.tsx:41

  • Add an assertion for the loading state being true immediately after dispatching fetchSectionsAsync() to cover the pending state behavior.
it("fetches sections and updates state via fetchSectionsAsync", async () => {

classPair: null,
isCreditNoCredit: false,
},
] as Section[];
Copy link

Copilot AI May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Remove the redundant as Section[] cast—since you already declared mockSections with type Section[], the explicit cast is unnecessary.

Suggested change
] as Section[];
];

Copilot uses AI. Check for mistakes.
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"jsdom": "^26.1.0",
"msw": "^2.8.5",
Copy link

Copilot AI May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move msw into devDependencies instead of dependencies to prevent including test utilities in production bundles.

Suggested change
"msw": "^2.8.5",

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants