Skip to content

chore(unit test): expand unit test coverage across HTTP layer, services, custom hooks and utilities - #212

Open
ginaxu1 wants to merge 3 commits into
mainfrom
chore/add-unit-tests
Open

chore(unit test): expand unit test coverage across HTTP layer, services, custom hooks and utilities#212
ginaxu1 wants to merge 3 commits into
mainfrom
chore/add-unit-tests

Conversation

@ginaxu1

@ginaxu1 ginaxu1 commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Following #155, expanded unit test coverage to frontend HTTP layer, services, custom hooks, and utilities

Changes Made

  1. HTTP Client src/http.test.ts

    • Bearer Token Injection: Verifies http.request attaches Authorization: Bearer <token> when attachToken: true.
    • URL Query Serialization: Verifies query string parameter formatting and filtering out undefined/null parameters.
    • Error Handling: Verifies HTTP error status handling (e.g. HTTP error! status: 404).
  2. Consignment Service src/features/consignment/service.test.ts

    • Verifies fetchConsignments formats q, page, and pageSize parameters correctly.
  3. Consignment Hook src/features/consignment/hooks/useConsignmentList.test.ts

    • Tests asynchronous consignment data fetching on mount.
    • Verifies loading flags, dataset assignment, and pagination calculation (total, totalPages).
  4. Sign-Out Handler src/features/user/hooks/useSignOutHandler.test.ts

    • Verifies signoutRedirect() invocation via OIDC authentication context.
  5. Date Formatter src/utils/date.test.ts`

    • Tests empty fallback ("-") and valid ISO date string formatting.
  6. Debounce Hook src/hooks/useDebounce.test.ts

    • Verifies timer-based debouncing behavior using Vitest fake timers.
  7. Application API Service src/features/application/service.test.ts

    • Tests fetchApplications, fetchApplicationDetail, submitReview, submitFeedback, and getDownloadUrl.

Verification

  • Unit Tests: pnpm test:run — PASS (17/17 passed)
  • TypeScript: pnpm type-check — PASS (0 errors)
  • ESLint: pnpm lint — PASS (0 errors, 0 warnings)
  • Production Build: pnpm build — PASS

Summary by CodeRabbit

  • Tests

    • Expanded automated coverage for application, consignment, authentication, date formatting, debouncing, and HTTP request behavior.
    • Verified loading states, pagination, request parameters, authentication handling, response mapping, and error handling.
  • Refactor

    • Clarified the HTTP request method’s return type without changing its runtime behavior.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 1c07b519-6990-4612-b6f2-8773139dbcba

📝 Walkthrough

Walkthrough

The PR adds Vitest coverage for frontend HTTP handling, application and consignment services, authentication and debounce hooks, and date formatting. It also adds an explicit return type to http.request.

Changes

Frontend test coverage

Layer / File(s) Summary
HTTP client contract and coverage
frontend/src/http.ts, frontend/src/http.test.ts
http.request now declares its promise response type. Tests cover bearer tokens, query serialization, successful JSON responses, and non-OK errors.
Application and consignment service flows
frontend/src/features/application/service.test.ts, frontend/src/features/consignment/service.test.ts, frontend/src/features/consignment/hooks/useConsignmentList.test.ts
Tests cover application listing, detail retrieval, review and feedback submission, download metadata mapping, consignment requests, loading states, returned items, and pagination.
Hook and utility behavior
frontend/src/features/user/hooks/useSignOutHandler.test.ts, frontend/src/hooks/useDebounce.test.ts, frontend/src/utils/date.test.ts
Tests cover sign-out redirects, 400 ms debounce updates, and date formatting for missing and valid values.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

  • OpenNSW/nsw-agency issue 154 — The PR adds the proposed Vitest coverage for the same frontend hooks, HTTP utility, and application and consignment services.

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: expanded frontend unit test coverage across the HTTP layer, services, hooks, and utilities.
Description check ✅ Passed The description clearly explains the test coverage and includes verification results, but it omits several template sections such as the change type and checklist.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/add-unit-tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
frontend/src/hooks/useDebounce.test.ts (1)

19-35: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Cover the debounce boundary and stale-timer cancellation.

The test advances directly to 400 milliseconds after one rerender. It would pass if the hook used a shorter delay. It would also pass if an old timer were not cleared, because the old and new callbacks can leave "world" as the final value. Advance to 399 milliseconds, perform a second rerender, and assert that the intermediate value is not published. This protects the cleanup contract in frontend/src/hooks/useDebounce.ts, Lines 3-17.

Suggested timing assertions
     rerender({ val: 'world' })
-    // Before delay, still initial value
-    expect(result.current).toBe('hello')
-
     act(() => {
-      vi.advanceTimersByTime(400)
+      vi.advanceTimersByTime(399)
     })
+    expect(result.current).toBe('hello')
 
-    expect(result.current).toBe('world')
+    rerender({ val: 'later' })
+    act(() => {
+      vi.advanceTimersByTime(1)
+    })
+    expect(result.current).toBe('hello')
+
+    act(() => {
+      vi.advanceTimersByTime(399)
+    })
+    expect(result.current).toBe('later')
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/src/hooks/useDebounce.test.ts` around lines 19 - 35, Update the
“updates debounced value after specified delay” test to cover the exact 400ms
boundary and stale-timer cancellation: advance 399ms after the first rerender,
rerender with a second value, assert the initial value remains, then advance the
remaining delay and assert only the second value is published. Preserve the
existing immediate-value assertion.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@frontend/src/http.test.ts`:
- Line 1: Run Prettier across the changed frontend test files, including
http.test.ts and service.test.ts, and commit the resulting formatting changes;
remove the file-level eslint suppression only if it becomes unnecessary after
formatting.

In `@frontend/src/utils/date.test.ts`:
- Around line 10-14: Update the valid-date test around formatDateForTable to set
a deterministic locale and time zone, then assert the complete expected
formatted result for August 10, 2026 rather than only checking the year.
Preserve the existing input and ensure the assertion verifies all date
components.

---

Nitpick comments:
In `@frontend/src/hooks/useDebounce.test.ts`:
- Around line 19-35: Update the “updates debounced value after specified delay”
test to cover the exact 400ms boundary and stale-timer cancellation: advance
399ms after the first rerender, rerender with a second value, assert the initial
value remains, then advance the remaining delay and assert only the second value
is published. Preserve the existing immediate-value assertion.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 92794950-81b4-45d2-93d7-264978d5e2be

📥 Commits

Reviewing files that changed from the base of the PR and between 063cc7b and 2b003e6.

📒 Files selected for processing (8)
  • frontend/src/features/application/service.test.ts
  • frontend/src/features/consignment/hooks/useConsignmentList.test.ts
  • frontend/src/features/consignment/service.test.ts
  • frontend/src/features/user/hooks/useSignOutHandler.test.ts
  • frontend/src/hooks/useDebounce.test.ts
  • frontend/src/http.test.ts
  • frontend/src/http.ts
  • frontend/src/utils/date.test.ts

Comment thread frontend/src/http.test.ts Outdated
@@ -0,0 +1,84 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

npx prettier --check \
  frontend/src/http.test.ts \
  frontend/src/features/application/service.test.ts \
  frontend/src/features/consignment/service.test.ts \
  frontend/src/features/consignment/hooks/useConsignmentList.test.ts

Repository: OpenNSW/nsw-agency

Length of output: 746


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf 'Changed frontend test files by git diff --stat:\n'
git diff --stat -- '*.test.ts' || true

printf '\nChanged frontend test files by git diff --name-only:\n'
git diff --name-only -- '*.test.ts' || true

printf '\nPrettier check for tracked frontend test files (no external deps):\n'
if [ -f frontend/package.json ]; then
  if [ -f frontend/node_modules/.bin/prettier ]; then
    ./frontend/node_modules/.bin/prettier --check frontend/src/http.test.ts frontend/src/features/application/service.test.ts frontend/src/features/consignment/service.test.ts frontend/src/features/consignment/hooks/useConsignmentList.test.ts || true
  else
    printf 'frontend/node_modules/.bin/prettier not present; using npx prettier --bin\n'
    npx --yes prettier --check frontend/src/http.test.ts frontend/src/features/application/service.test.ts frontend/src/features/consignment/service.test.ts frontend/src/features/consignment/hooks/useConsignmentList.test.ts || true
  fi
else
  printf 'frontend/package.json not present; using npx prettier --check\n'
  npx --yes prettier --check frontend/src/http.test.ts frontend/src/features/application/service.test.ts frontend/src/features/consignment/service.test.ts frontend/src/features/consignment/hooks/useConsignmentList.test.ts || true
fi

printf '\nPrettier diffs for changed frontend test files:\n'
rm -rf /tmp/prettier-diffs
pnpm list -g prettier 2>/dev/null | sed -n '1,20p' || true
npx --yes prettier --no-config --print-width=120 --write /tmp/prettier-diffs --stdin-filepath frontend/src/http.test.ts < frontend/src/http.test.ts > /tmp/prettier-diffs/http.test.ts
diff -u frontend/src/http.test.ts /tmp/prettier-diffs/http.test.ts | sed -n '1,160p' || true

npx --yes prettier --no-config --print-width=120 --write /tmp/prettier-diffs --stdin-filepath frontend/src/features/application/service.test.ts < frontend/src/features/application/service.test.ts > /tmp/prettier-diffs/application.service.test.ts
diff -u frontend/src/features/application/service.test.ts /tmp/prettier-diffs/application.service.test.ts | sed -n '1,160p' || true

Repository: OpenNSW/nsw-agency

Length of output: 1026


Restore Prettier compliance for the changed frontend tests.

frontend/src/http.test.ts and frontend/src/features/application/service.test.ts fail Prettier formatting; run Prettier in the frontend workspace and commit the result so CI formatting passes.

🧰 Tools
🪛 GitHub Actions: Frontend CI / 1_Quality Check & Build.txt

[error] 1-1: Prettier formatting check failed. Run 'prettier --write' to fix code style issues.

🪛 GitHub Actions: Frontend CI / Quality Check & Build

[error] 1-1: Prettier formatting check failed. Run 'prettier --write' to fix code style issues.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/src/http.test.ts` at line 1, Run Prettier across the changed
frontend test files, including http.test.ts and service.test.ts, and commit the
resulting formatting changes; remove the file-level eslint suppression only if
it becomes unnecessary after formatting.

Source: Pipeline failures

Comment thread frontend/src/utils/date.test.ts Outdated
@ginaxu1
ginaxu1 force-pushed the chore/add-unit-tests branch from 2b003e6 to b213fdc Compare August 11, 2026 05:00
@ginaxu1
ginaxu1 force-pushed the chore/add-unit-tests branch 2 times, most recently from afa6c3f to 622d80d Compare September 3, 2026 05:23
ginaxu1 and others added 3 commits September 5, 2026 09:27
Drop the sign-out pass-through case and redundant debounce/date assertions, and tighten the HTTP client tests around auth, query params, and error handling.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ginaxu1
ginaxu1 force-pushed the chore/add-unit-tests branch from 622d80d to 5b8dd01 Compare September 5, 2026 04:45
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.

1 participant