Skip to content

Test: Implement E2E: Complete Escrow and Cargo Insurance Flow - #539

Merged
johdanike merged 1 commit into
SwiftChainn:mainfrom
BigManly4:test-e2e-escrow-insurance
Sep 1, 2026
Merged

Test: Implement E2E: Complete Escrow and Cargo Insurance Flow#539
johdanike merged 1 commit into
SwiftChainn:mainfrom
BigManly4:test-e2e-escrow-insurance

Conversation

@BigManly4

Copy link
Copy Markdown
Contributor

Summary

Automates the end-to-end flow of a shipper selecting cargo insurance and completing the XLM escrow payment at checkout.

The repository had escrow lock and payout pieces but no cargo insurance step and no checkout flow to drive, so this PR adds the flow under test alongside the tests. Every external boundary is mocked, so the suite makes no network calls and does not depend on a wallet extension.

Changes

types/insurance.ts (new)

  • InsurancePlan — a coverage product: coverage, premium, optional deductible and a recommended flag. All amounts in XLM.
  • EscrowCheckoutQuote — the priced breakdown: shipment cost, premium, total and the selected plan.
  • EscrowCheckoutParams / EscrowCheckoutReceipt — the payment payload and the on-chain confirmation.
  • Discriminated success result unions matching the convention used by the other services here.

services/insuranceService.ts (new)

  • getPlans(shipmentId) — fetches the coverage offered for a shipment.
  • payWithEscrow(params) — posts the checkout and returns the escrow receipt. Validates shipment id, wallet address and amount before touching the network.
  • buildCheckoutQuote(shipmentXlm, plan) — pure pricing, so the total displayed to the shipper and the amount submitted on chain come from one source.
  • toStroopPrecision / formatXlm — XLM is divisible to seven decimals, so amounts are rounded to one stroop. This is what stops 0.1 + 0.2 reaching either the UI or the contract as 0.30000000000000004.

hooks/useEscrowCheckout.ts (new) — owns step and request state across the three checkout steps, delegating every network call to the service.

components/escrow/EscrowInsuranceCheckout.tsx (new) — the flow itself:

  1. Cargo insurance — a radio group of available plans plus an explicit "continue without coverage" option.
  2. Review and pay — shipment cost, premium and total locked, with back and pay actions.
  3. Complete — escrow id, transaction hash and policy reference.

Headings, radiogroup, role="alert" and role="status" are used throughout so the tests query the way a user perceives the page rather than by implementation detail.

components/escrow/__tests__/EscrowInsuranceCheckout.test.tsx (new) — 21 flow tests.
services/__tests__/insuranceService.test.ts (new) — 23 service tests.

Mocking strategy

  • axios is mocked in the service suite. axios.isAxiosError is a type guard rather than a request method, so auto-mocking leaves it returning undefined; the suite restores real behaviour explicitly, which is what lets the error-branch assertions actually exercise the axios path.
  • The flow suite mocks only getPlans and payWithEscrow, keeping buildCheckoutQuote and formatXlm real via jest.requireActual. The totals asserted in the flow tests are therefore the ones production pricing produces, not values restated in the test.
  • The wallet is injected as a prop, so no browser extension or Web3 global is touched.
  • No timers, no polling and no real promises against the network, so there is nothing for the tests to race against.

Test coverage

Happy path

  • The full flow end to end: select Premium Cover, review the breakdown (250 + 40.25 = 290.25 XLM), pay, and assert the payload sent to the service plus the escrow id, transaction hash and policy id on the receipt.
  • Checkout with coverage declined: zero premium, insurancePlanId: null, and no policy reference on the receipt.
  • Changing plan after reaching review recalculates the total and pays the corrected amount.
  • Plans are requested for the shipment under checkout, once.

Empty states

  • No plans offered: the shipper is told, and can still fund the escrow without coverage.
  • Coverage defaults to declined rather than silently pre-selecting a paid plan.

Edge cases

  • Stroop rounding: a 0.1 XLM shipment with a 0.2 XLM premium renders 0.3 XLM.
  • Disconnected wallet: warned on review, pay button disabled, service never called.
  • Invalid shipment amount disables the continue action.
  • Double-clicking pay submits exactly one payment.
  • Both actions are disabled and progress is shown while the payment is in flight.

Error handling

  • Plan lookup failure shows the message, hides the plan list, and recovers on retry.
  • Payment failure keeps the shipper on the review step with the error shown, and a retry succeeds.
  • Going back to change coverage clears a stale payment error.
  • Service layer: missing shipment id, missing wallet, non-positive amount, unsuccessful API responses, axios error bodies, and fallbacks for unrecognised failures.

Verification

npx jest components/escrow/__tests__/EscrowInsuranceCheckout.test.tsx services/__tests__/insuranceService.test.ts
Test Suites: 2 passed, 2 total
Tests:       44 passed, 44 total

eslint and tsc --noEmit are clean for the added files.

The full suite was run against main and again with this branch applied. Both runs report 24 failing suites and 71 failing tests; this branch adds 2 passing suites and 44 passing tests and changes no existing failure. Those pre-existing failures are unrelated to this work and are left untouched, including the placeholder at __tests__/features/checkout/checkout-flow.test.tsx, whose component import is still commented out upstream.

closes #510

@drips-wave

drips-wave Bot commented Aug 31, 2026

Copy link
Copy Markdown

@BigManly4 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

@BigManly4

Copy link
Copy Markdown
Contributor Author

Note on the failing CI run: it fails at the Lint step before reaching Test, and the failures are pre-existing on main rather than introduced by this branch. pnpm run lint reports four errors, all in files this PR does not touch:

  • components/mobile/MobileFooter.tsx:1 — the directive is written with a backtick, `use client, instead of 'use client'. The unterminated template literal that results also fails tsc --noEmit with TS1160, so it blocks Type Check and Build as well.
  • app/page.tsx:48react/jsx-no-undef: KineticExplorer is used but never imported.
  • hooks/useTheme.ts:22Cannot access variable before it is declared.
  • hooks/useDriverReputation.ts:27react-hooks/set-state-in-effect.

I have deliberately left these alone rather than bundling unrelated source fixes into a test-scoped PR, and the KineticExplorer import in particular needs a maintainer decision about which component was intended. Every open PR against main will be red until they are resolved. Happy to send a separate PR for the MobileFooter directive typo if that is useful.

For what it is worth, the test suite itself is unaffected. Locally, main and this branch both report the same 24 failing suites and 71 failing tests; this branch only adds passing ones.

@johdanike
johdanike merged commit a4fbf3c into SwiftChainn:main Sep 1, 2026
0 of 2 checks passed
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.

[Test] E2E: Complete Escrow and Cargo Insurance Flow

2 participants