forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency.controller.spec.ts
More file actions
137 lines (111 loc) · 3.89 KB
/
Copy pathcurrency.controller.spec.ts
File metadata and controls
137 lines (111 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Test, TestingModule } from '@nestjs/testing';
import { CurrencyController } from './currency.controller';
import { CurrencyService } from './currency.service';
describe('CurrencyController', () => {
let controller: CurrencyController;
let service: CurrencyService;
const mockCurrencyService = {
getRates: jest.fn(),
convertCurrency: jest.fn(),
getSupportedCurrencies: jest.fn(),
formatCurrency: jest.fn(),
clearCache: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CurrencyController],
providers: [
{
provide: CurrencyService,
useValue: mockCurrencyService,
},
],
}).compile();
controller = module.get<CurrencyController>(CurrencyController);
service = module.get<CurrencyService>(CurrencyService);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('getRates', () => {
it('should return exchange rates', async () => {
const expectedRates = {
base: 'USD',
rates: {
EUR: 0.85,
},
metadata: {
EUR: {
source: 'ExchangeRateAPI',
stale: false,
cached: false,
fetchedAt: '2026-03-29T10:00:00.000Z',
expiresAt: '2026-03-29T10:05:00.000Z',
fallbackReason: null,
},
},
};
mockCurrencyService.getRates.mockResolvedValue(expectedRates);
const result = await controller.getRates();
expect(result).toEqual(expectedRates);
expect(service.getRates).toHaveBeenCalledWith('USD', undefined);
});
});
describe('convertCurrency', () => {
it('should convert currency successfully', async () => {
const convertDto = {
amount: 100,
from: 'USD',
to: 'EUR',
};
const expectedResult = {
amount: 100,
convertedAmount: 85,
rate: 0.85,
from: 'USD',
to: 'EUR',
metadata: {
source: 'ExchangeRateAPI',
stale: false,
cached: false,
fetchedAt: '2026-03-29T10:00:00.000Z',
expiresAt: '2026-03-29T10:05:00.000Z',
fallbackReason: null,
},
};
mockCurrencyService.convertCurrency.mockResolvedValue(expectedResult);
const result = await controller.convertCurrency(convertDto);
expect(result).toEqual(expectedResult);
expect(service.convertCurrency).toHaveBeenCalledWith(convertDto);
});
});
describe('getSupportedCurrencies', () => {
it('should return supported currencies', async () => {
const expectedCurrencies = ['USD', 'EUR', 'GBP', 'XLM', 'USDC'];
mockCurrencyService.getSupportedCurrencies.mockReturnValue(expectedCurrencies);
const result = await controller.getSupportedCurrencies();
expect(result).toEqual(expectedCurrencies);
expect(service.getSupportedCurrencies).toHaveBeenCalled();
});
});
describe('formatCurrency', () => {
it('should format currency successfully', async () => {
const expectedResult = { formatted: '$100.50' };
mockCurrencyService.formatCurrency.mockReturnValue('$100.50');
const result = await controller.formatCurrency('100.50', 'USD');
expect(result).toEqual(expectedResult);
expect(service.formatCurrency).toHaveBeenCalledWith(100.5, 'USD');
});
it('should throw error for invalid amount', async () => {
await expect(controller.formatCurrency('invalid', 'USD')).rejects.toThrow('Invalid amount parameter');
});
});
describe('clearCache', () => {
it('should clear cache successfully', async () => {
mockCurrencyService.clearCache.mockResolvedValue(undefined);
const result = await controller.clearCache();
expect(result).toEqual({ message: 'Exchange rate cache cleared successfully' });
expect(service.clearCache).toHaveBeenCalled();
});
});
});