46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
import { describe, expect, it, vi } from 'vitest';
|
|
import axios from 'axios';
|
|
import { getExchange } from 'src/composables/getExchange';
|
|
|
|
vi.mock('axios');
|
|
|
|
describe('getExchange()', () => {
|
|
it('should return the correct exchange rate', async () => {
|
|
axios.get.mockResolvedValue({
|
|
data: { value: 1.2 },
|
|
});
|
|
|
|
const amount = 100;
|
|
const currencyFk = 1;
|
|
const dated = '2023-01-01';
|
|
const result = await getExchange(amount, currencyFk, dated);
|
|
|
|
expect(result).toBe('83.33');
|
|
});
|
|
|
|
it('should return the correct exchange rate with custom decimal places', async () => {
|
|
axios.get.mockResolvedValue({
|
|
data: { value: 1.2 },
|
|
});
|
|
|
|
const amount = 100;
|
|
const currencyFk = 1;
|
|
const dated = '2023-01-01';
|
|
const decimalPlaces = 3;
|
|
const result = await getExchange(amount, currencyFk, dated, decimalPlaces);
|
|
|
|
expect(result).toBe('83.333');
|
|
});
|
|
|
|
it('should return null if the API call fails', async () => {
|
|
axios.get.mockRejectedValue(new Error('Network error'));
|
|
|
|
const amount = 100;
|
|
const currencyFk = 1;
|
|
const dated = '2023-01-01';
|
|
const result = await getExchange(amount, currencyFk, dated);
|
|
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|