forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuiService.test.ts
More file actions
99 lines (83 loc) · 2.28 KB
/
Copy pathsuiService.test.ts
File metadata and controls
99 lines (83 loc) · 2.28 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
import {
afterEach,
beforeEach,
describe,
expect,
it,
vi
} from 'vitest';
import {
resolveSuiAddress
} from './suiService';
const fetchMock = vi.fn<typeof fetch>();
const jsonResponse = (
body: unknown,
status = 200
) =>
new Response(JSON.stringify(body), {
status,
headers: {
'Content-Type': 'application/json'
}
});
describe('resolveSuiAddress', () => {
beforeEach(() => {
fetchMock.mockReset();
vi.stubGlobal('fetch', fetchMock);
});
afterEach(() => {
vi.unstubAllGlobals();
vi.useRealTimers();
});
it('caches a resolved SuiNS address within the TTL', async () => {
fetchMock.mockResolvedValue(
jsonResponse({
jsonrpc: '2.0',
id: 1,
result: '0x111111'
})
);
const firstResult = await resolveSuiAddress(
'mainnet',
'cache-test-unique.sui'
);
const secondResult = await resolveSuiAddress(
'mainnet',
'cache-test-unique.sui'
);
expect(firstResult).toBe('0x111111');
expect(secondResult).toBe('0x111111');
expect(fetchMock).toHaveBeenCalledOnce();
});
it('re-resolves a SuiNS name after the cache expires', async () => {
vi.useFakeTimers();
fetchMock
.mockResolvedValueOnce(
jsonResponse({
jsonrpc: '2.0',
id: 1,
result: '0x111111'
})
)
.mockResolvedValueOnce(
jsonResponse({
jsonrpc: '2.0',
id: 1,
result: '0x222222'
})
);
const firstResult = await resolveSuiAddress(
'mainnet',
'expiration-test-unique.sui'
);
expect(firstResult).toBe('0x111111');
expect(fetchMock).toHaveBeenCalledOnce();
vi.advanceTimersByTime(5 * 60 * 1000 + 1);
const secondResult = await resolveSuiAddress(
'mainnet',
'expiration-test-unique.sui'
);
expect(secondResult).toBe('0x222222');
expect(fetchMock).toHaveBeenCalledTimes(2);
});
});