Skip to content

Add unit tests for currency exchange rate utilities #71

@aruneshwisdm

Description

@aruneshwisdm

Summary

The currency exchange rate module in `apps/web/lib/currency/exchange-rates.ts` contains utility functions for multi-currency conversion that have no unit tests. The synchronous `convertToBase` function and the caching logic are particularly well-suited for testing.

Functions to test

  • `convertToBase(amount, fromCurrency, baseCurrency, rates)` — synchronous currency conversion using a pre-fetched rates map
  • `convertCurrency(amount, fromCurrency, toCurrency)` — async conversion that fetches rates (mock the fetch)
  • `getExchangeRates()` — rate fetching with caching and fallback logic

Files

  • Source: `apps/web/lib/currency/exchange-rates.ts`
  • Test file to create: `apps/web/tests/lib/currency/exchange-rates.test.ts`

Acceptance criteria

  • `convertToBase` tested with: same currency (returns original amount), different currencies, unknown currency (returns original), zero amount
  • `convertCurrency` tested with mocked fetch: successful API response, failed API response (falls back to static rates)
  • `getExchangeRates` tested: returns cached rates when fresh, fetches new rates when cache is expired
  • Edge case: unknown currency codes return the original amount unchanged
  • All tests pass with `pnpm vitest run`

Getting started

You'll need to mock `fetch` for the async functions. Look at how Vitest's `vi.fn()` and `vi.spyOn(globalThis, 'fetch')` work. The `convertToBase` function is synchronous and needs no mocking — start there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions