Skip to content

Add unit tests for the apiClient request helpers and error unwrapping #14

@mikewheeleer

Description

@mikewheeleer

Test the apiClient request helpers and ApiError unwrapping

Description

src/lib/apiClient.ts is the single point through which most pages talk to the backend, yet it has no tests — its 204 handling, JSON parsing, res.ok failure path that throws an enriched ApiError, and the apiGet/apiPost/apiPatch/apiDelete wrappers are all unverified. This issue adds a focused test suite around the fetch wrapper.

Requirements and context

  • Repository scope: Agentpay-Org/Agentpay-frontend only.
  • Mock globalThis.fetch (as done in src/app/usage/page.test.tsx) and assert: base-URL prefixing, default Content-Type header, 204 returning undefined, and a non-OK response throwing an error carrying error/message/requestId.
  • Verify each wrapper sends the correct method and JSON-stringified body.
  • Cover NEXT_PUBLIC_AGENTPAY_API_BASE override vs the localhost default.

Suggested execution

  • Fork the repo and create a branch
  • git checkout -b test/testing-15-apiclient-unit
  • Implement changes
    • Write code in: no source change expected (file a follow-up if a bug is found).
    • Write comprehensive tests in: create src/lib/__tests__/apiClient.test.ts.
    • Add documentation: none beyond test descriptions.
    • Validate the thrown object is an Error instance with merged ApiError fields.
  • Test and commit

Test and commit

  • Run npm run lint, npm run typecheck, and npm test.
  • Cover edge cases: empty body on error, header override merge, and DELETE returning 204.
  • Include the npm test output and the coverage summary for apiClient.ts.

Example commit message

test(api-client): cover request helpers and ApiError unwrapping

Guidelines

  • Minimum 95 percent test coverage for apiClient.ts.
  • Clear, reviewer-focused documentation.
  • Timeframe: 96 hours.

Community & contribution rewards

  • 💬 Join the AgentPay community on Discord for questions, reviews, and faster merges: https://discord.gg/eXvRKkgcv
  • ⭐ This is a GrantFox OSS / Official Campaign task and may be rewarded. When your PR is merged you'll be prompted to rate the project — if this issue and the maintainers helped you ship, we'd be grateful for a 5-star rating. Clear questions in Discord and tidy, well-tested PRs are the fastest path to a merge and a reward.

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions