Skip to content

test: add unit tests for Freighter error mapping - #1024

Open
Queen-T16 wants to merge 1 commit into
Iris-IV:mainfrom
Queen-T16:fix/800-freighter-errors-unit-tests
Open

test: add unit tests for Freighter error mapping#1024
Queen-T16 wants to merge 1 commit into
Iris-IV:mainfrom
Queen-T16:fix/800-freighter-errors-unit-tests

Conversation

@Queen-T16

@Queen-T16 Queen-T16 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #800 — [Testing] Add unit tests for freighterErrors mapping.

src/utils/freighterErrors.ts previously had no dedicated test coverage. This PR adds a full unit-test suite and extends the module (in the spirit of the already-tested contractErrors.ts) so the required cases are both testable and mapped to user-facing messages.

Changes

src/utils/freighterErrors.ts

  • New isExtensionNotInstalled(error) — detects the "Freighter extension not installed / unavailable" case (patterns like not installed, is not defined, window.freighter, no freighter).
  • New FreighterExtensionNotInstalledError — typed error thrown by wrapFreighterError() for the missing-extension case.
  • New parseFreighterError(error): string — maps any thrown value to a user-facing message:
    • user rejection → USER_CANCELLED_MESSAGE
    • missing extension → FREIGHTER_NOT_INSTALLED_MESSAGE
    • readable unrecognized errors → raw message
    • unrecognized error objects (e.g. {}, { code: 4001 }), null, undefinedGENERIC_FREIGHTER_ERROR_MESSAGE (no crash)
  • Message strings centralized as exported constants; a shared errorMessage() extractor handles Error, strings, flat objects, and nested { error: { message } } shapes.
  • wrapFreighterError() now re-throws typed errors for both known cases and re-throws unrecognized errors unchanged (existing callers in contractClient.ts / offchainApiClient.ts are unaffected).

src/__tests__/utils/freighterErrors.test.ts (new)

  • User-rejected signature case across formats (Error / string / object / nested object).
  • Extension-not-installed case across formats, plus a guard that unrelated errors are not misclassified.
  • Unrecognized error objects falling back to a generic message rather than crashing.

Note: placed at src/__tests__/utils/freighterErrors.test.ts (the repo's existing convention, e.g. exportCsv.test.ts) rather than the literal src/tests/ path from the issue, since the repo has no src/tests directory and Jest's config ignores only the root tests/ e2e dir.

Acceptance criteria

  • Add src/tests/utils/freighterErrors.test.ts (see note above — added under src/__tests__/utils/ per repo convention).
  • Cover the user-rejected-signature case.
  • Cover the extension-not-installed case.
  • Cover an unrecognized error object falling back to a generic message rather than crashing.

Verification

  • New suite: 11 tests pass (npx jest src/__tests__/utils/freighterErrors.test.ts).
  • eslint clean on both touched files.
  • No changes to how WalletContext surfaces errors (out of scope).

Pre-existing failures unrelated to this change (verified on main): src/__tests__/lib/contractClient.test.ts (1 test) and the typecheck errors in CauseDetailClient.tsx / WalletContext.tsx.

Out of scope

Changing how WalletContext surfaces these errors in the UI (explicitly out of scope in the issue).

Extend src/utils/freighterErrors.ts so it can be fully tested and matches
the spirit of contractErrors.ts:

- Add isExtensionNotInstalled() and FreighterExtensionNotInstalledError to
  detect the extension-not-installed case.
- Add parseFreighterError() to map any thrown value to a user-facing
  message, falling back to a generic message for unrecognized error objects
  instead of crashing.
- Error messages are now centralized as exported constants and reuse a
  shared errorMessage() extractor (handles Error, string, flat objects, and
  nested { error: { message } } shapes).
- wrapFreighterError() now re-throws typed errors for both known cases and
  re-throws unrecognized errors unchanged.

Add src/__tests__/utils/freighterErrors.test.ts covering user-rejected
signature errors, extension-not-installed errors, and unrecognized error
objects falling back to a generic message.

Closes Iris-IV#800
@sshdopey

sshdopey commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Auto-review failed (API error). Leaving PR for human review.

@drips-wave

drips-wave Bot commented Aug 1, 2026

Copy link
Copy Markdown

@Queen-T16 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@davidmaronio

Copy link
Copy Markdown
Contributor

solid coverage: the new errorMessage extractor handles nested { error: { message } } shapes, isExtensionNotInstalled fills a real gap, and the tests exercise rejection, missing-extension, unrecognized objects, and the wrap/rethrow contract. one small note, non-blocking:

  1. src/utils/freighterErrors.ts:11: the new user-facing strings (FREIGHTER_NOT_INSTALLED_MESSAGE, GENERIC_FREIGHTER_ERROR_MESSAGE) are hardcoded english, while contractErrors.ts maps to i18n keys resolved via messages/*.json. following that convention would keep these translatable; fine as a follow-up if the maintainers prefer.

gate: branch is behind main; rebase and rerun CI, then good to merge. (no stacking concerns with your #1023/#1025, the files are disjoint.)

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.

[Testing] Add unit tests for freighterErrors mapping

3 participants