forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment-client.test.ts
More file actions
107 lines (93 loc) · 3.04 KB
/
Copy pathpayment-client.test.ts
File metadata and controls
107 lines (93 loc) · 3.04 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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { PaymentClient } from '../src/clients/payment-client';
import type { HttpClient, HttpResponse } from '../src/http/types';
import type { Payment, PaymentQuote, PaymentQuoteRequest, ExecutePaymentRequest } from '../src/models';
function createMockHttpClient(responseData: unknown = {}): HttpClient {
return {
request: vi.fn().mockResolvedValue({
status: 200,
headers: new Headers(),
data: responseData,
} as HttpResponse),
};
}
const mockPayment: Payment = {
id: 'pay-1',
fromWalletId: 'wallet-1',
toAddress: 'GABC...',
amount: { amount: '100', assetCode: 'XLM' },
status: 'settled',
createdAt: '2024-01-01T00:00:00Z',
updatedAt: '2024-01-01T00:00:00Z',
};
const mockQuote: PaymentQuote = {
amount: { amount: '100', assetCode: 'XLM' },
estimatedFee: { amount: '0.00001', assetCode: 'XLM' },
expiresAt: '2024-01-01T01:00:00Z',
};
describe('PaymentClient', () => {
let httpClient: HttpClient;
let client: PaymentClient;
beforeEach(() => {
httpClient = createMockHttpClient();
client = new PaymentClient(httpClient);
});
describe('quote', () => {
it('sends POST /v1/payments/quote with the input body and returns the quote', async () => {
const input: PaymentQuoteRequest = {
fromWalletId: 'wallet-1',
toAddress: 'GABC...',
amount: { amount: '100', assetCode: 'XLM' },
};
vi.mocked(httpClient.request).mockResolvedValueOnce({
status: 200,
headers: new Headers(),
data: mockQuote,
} as HttpResponse);
const result = await client.quote(input);
expect(result).toEqual(mockQuote);
expect(httpClient.request).toHaveBeenCalledWith({
method: 'POST',
path: '/v1/payments/quote',
body: input,
});
});
});
describe('execute', () => {
it('sends POST /v1/payments with the input body and returns the payment', async () => {
const input: ExecutePaymentRequest = {
fromWalletId: 'wallet-1',
toAddress: 'GABC...',
amount: { amount: '100', assetCode: 'XLM' },
memo: 'test-payment',
};
vi.mocked(httpClient.request).mockResolvedValueOnce({
status: 201,
headers: new Headers(),
data: mockPayment,
} as HttpResponse);
const result = await client.execute(input);
expect(result.id).toBe('pay-1');
expect(httpClient.request).toHaveBeenCalledWith({
method: 'POST',
path: '/v1/payments',
body: input,
});
});
});
describe('get', () => {
it('sends GET /v1/payments/:id and returns the payment', async () => {
vi.mocked(httpClient.request).mockResolvedValueOnce({
status: 200,
headers: new Headers(),
data: mockPayment,
} as HttpResponse);
const result = await client.get('pay-1');
expect(result).toEqual(mockPayment);
expect(httpClient.request).toHaveBeenCalledWith({
method: 'GET',
path: '/v1/payments/pay-1',
});
});
});
});