-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathTransactionHistory.test.tsx
More file actions
170 lines (141 loc) · 4.88 KB
/
Copy pathTransactionHistory.test.tsx
File metadata and controls
170 lines (141 loc) · 4.88 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import './__mocks__/rn-modules';
import React from 'react';
import renderer, { act } from 'react-test-renderer';
import { Text, TouchableOpacity } from 'react-native';
import TransactionHistory from '../components/TransactionHistory';
import { useWalletStore } from '../store/walletStore';
import { StellarPayment } from '../services/stellar';
jest.mock('../../services/stellar', () => ({
...jest.requireActual('../../services/stellar'),
getPayments: jest.fn(),
}));
const { getPayments } = require('../../services/stellar') as {
getPayments: jest.MockedFunction<(publicKey: string, limit?: number) => Promise<StellarPayment[]>>;
};
function payment(id: string, amount = '10'): StellarPayment {
return {
id,
type: 'payment',
amount,
asset_type: 'native',
from: 'GSOURCE',
to: 'GDEST',
created_at: new Date().toISOString(),
};
}
function textValues(tree: renderer.ReactTestRenderer): string[] {
return tree.root
.findAllByType(Text)
.flatMap(node =>
(Array.isArray(node.props.children)
? node.props.children
: [node.props.children]
).filter((child: unknown): child is string => typeof child === 'string'),
);
}
function buttonWithText(
tree: renderer.ReactTestRenderer,
label: string,
): renderer.ReactTestInstance {
const button = tree.root
.findAllByType(TouchableOpacity)
.find(node =>
node.findAllByType(Text).some(text => text.props.children === label),
);
if (!button) {
throw new Error(`Could not find a button labelled "${label}"`);
}
return button;
}
beforeEach(() => {
useWalletStore.setState({
publicKey: 'GABC',
payments: null,
paymentsLastFetchedAt: null,
isConnected: true,
status: 'connected',
connectError: null,
balance: '100',
ecoBalance: '10',
usdcBalance: '5',
walletType: 'inapp',
});
getPayments.mockReset();
});
describe('TransactionHistory cache behavior', () => {
it('renders skeleton when cache is empty and fetches once', async () => {
getPayments.mockResolvedValueOnce([payment('1')]);
let tree: renderer.ReactTestRenderer;
await act(async () => {
tree = renderer.create(<TransactionHistory publicKey="GABC" />);
});
expect(getPayments).toHaveBeenCalledTimes(1);
expect(textValues(tree!).toContain('Recent Transactions');
expect(textValues(tree!).toContain('1.00');
});
it('does not fetch again within the cache TTL on remount', async () => {
getPayments.mockResolvedValueOnce([payment('1')]);
let tree: renderer.ReactTestRenderer;
await act(async () => {
tree = renderer.create(<TransactionHistory publicKey="GABC" />);
});
expect(getPayments).toHaveBeenCalledTimes(1);
await act(async () => {
tree!.unmount();
});
await act(async () => {
tree = renderer.create(<TransactionHistory publicKey="GABC" />);
});
expect(getPayments).toHaveBeenCalledTimes(1);
expect(textValues(tree!).toContain('1.00');
});
it('fetches again after the cache TTL expires', async () => {
getPayments.mockResolvedValueOnce([payment('1')]);
getPayments.mockResolvedValueOnce([payment('2')]);
let tree: renderer.ReactTestRenderer;
await act(async () => {
tree = renderer.create(<TransactionHistory publicKey="GABC" />);
});
expect(getPayments).toHaveBeenCalledTimes(1);
await act(async () => {
tree!.unmount();
});
useWalletStore.setState({
paymentsLastFetchedAt: Date.now() - 60_001,
});
await act(async () => {
tree = renderer.create(<TransactionHistory publicKey="GABC" />);
});
expect(getPayments).toHaveBeenCalledTimes(2);
expect(textValues(tree!).toContain('2.00');
});
it('manual refresh triggers a fresh call even within TTL', async () => {
getPayments.mockResolvedValueOnce([payment('1')]);
getPayments.mockResolvedValueOnce([payment('2')]);
let tree: renderer.ReactTestRenderer;
await act(async () => {
tree = renderer.create(<TransactionHistory publicKey="GABC" />);
});
expect(getPayments).toHaveBeenCalledTimes(1);
await act(async () => {
buttonWithText(tree!, 'Refresh').props.onPress();
});
expect(getPayments).toHaveBeenCalledTimes(2);
expect(textValues(tree!).toContain('2.00');
});
it('shows error and retry when fetch fails', async () => {
getPayments.mockRejectedValueOnce(new Error('Network down'));
let tree: renderer.ReactTestRenderer;
await act(async () => {
tree = renderer.create(<TransactionHistory publicKey="GABC" />);
});
expect(textValues(tree!)).toContain('Failed to load transaction history');
expect(textValues(tree!)).toContain('Retry');
getPayments.mockResolvedValueOnce([payment('1')]);
await act(async () => {
buttonWithText(tree!, 'Retry').props.onPress();
});
expect(getPayments).toHaveBeenCalledTimes(2);
expect(textValues(tree!)).toContain('1.00');
});
});