Unit Test - COS301-SE-2025/API-Threat-Assessment-Tool GitHub Wiki
Testing Guide: React Application
Overview
This repository uses Jest and React Testing Library to unit test UI components, hooks, and context logic. It ensures reliability and maintainability by validating both rendering and behavior of components.
Testing Tools
@testing-library/react
@testing-library/jest-dom
@testing-library/react-hooks
jest
File Structure
/tests
- ManageAPIs.test.js
- AuthContext.test.js
- App.test.js
Components Tested
ManageAPIs
1. -
Tested behaviors:
- Renders API list
- Opens and closes the Add API modal
- Adds, edits, and deletes APIs
AuthContext
2. -
Tested behaviors:
- Initializes with default users
- Handles login/logout correctly
- Supports signup with localStorage persistence
App
3. -
Tested behaviors:
- Mounts without crashing
- Theme context toggling
- Provides ScanSimulation context
Mocking
AuthContext
Mocked to inject currentUser
, logout
, and getUserFullName
for UI components relying on user data.
Running Tests
npm test
or
yarn test
Use --watch
to auto-run on file changes.
Suggestions for Expansion
- Add snapshot tests for UI consistency
- Use
jest-axe
to ensure accessibility - Validate edge cases (empty form submits, invalid inputs)
- Test dark mode toggling logic directly
Resources
- [Jest Docs](https://jestjs.io/docs/getting-started)
- [React Testing Library Docs](https://testing-library.com/docs/react-testing-library/intro/)
- [Testing Hooks](https://react-hooks-testing-library.com/)