@accounter/server
Version:
Accounter GraphQL server
47 lines • 1.73 kB
JavaScript
/**
* Creates a mock function that overrides ExchangeProvider.getExchangeRates
* to return a fixed rate for specific currency pairs.
*
* @example
* ```ts
* // Fix USD → ILS at 3.5
* const mockFn = createMockExchangeRates([
* { fromCurrency: Currency.Usd, toCurrency: Currency.Ils, rate: 3.5 }
* ]);
* provider.getExchangeRates = mockFn;
* ```
*/
export function createMockExchangeRates(mocks) {
return async function getExchangeRates(baseCurrency, quoteCurrency, _date) {
// Same currency always returns 1
if (baseCurrency === quoteCurrency) {
return 1;
}
// Find matching mock
const mock = mocks.find(m => m.fromCurrency === baseCurrency && m.toCurrency === quoteCurrency);
if (mock) {
return mock.rate;
}
// Try inverse rate (base/quote swapped)
const inverseMock = mocks.find(m => m.fromCurrency === quoteCurrency && m.toCurrency === baseCurrency);
if (inverseMock) {
return 1 / inverseMock.rate;
}
// No mock found - throw error to prevent fallthrough to real API
throw new Error(`No exchange rate mock configured for ${baseCurrency} → ${quoteCurrency}. ` +
`Available mocks: ${mocks.map(m => `${m.fromCurrency}→${m.toCurrency}`).join(', ')}`);
};
}
/**
* Convenience function to create a single exchange rate mock.
*
* @example
* ```ts
* const mockFn = mockExchangeRate(Currency.Usd, Currency.Ils, 3.5);
* provider.getExchangeRates = mockFn;
* ```
*/
export function mockExchangeRate(fromCurrency, toCurrency, rate) {
return createMockExchangeRates([{ fromCurrency, toCurrency, rate }]);
}
//# sourceMappingURL=exchange-mock.js.map