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

1. ManageAPIs

  • Tested behaviors:

    • Renders API list
    • Opens and closes the Add API modal
    • Adds, edits, and deletes APIs

2. AuthContext

  • Tested behaviors:

    • Initializes with default users
    • Handles login/logout correctly
    • Supports signup with localStorage persistence

3. App

  • 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