forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.test.ts
More file actions
140 lines (116 loc) · 4.1 KB
/
Copy pathstellar.test.ts
File metadata and controls
140 lines (116 loc) · 4.1 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
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('@stellar/freighter-api', () => ({
getAddress: vi.fn(),
isConnected: vi.fn(),
requestAccess: vi.fn()
}));
vi.mock('@lobstrco/signer-extension-api', () => ({
getPublicKey: vi.fn(),
isConnected: vi.fn()
}));
import { requestAccess } from '@stellar/freighter-api';
import { connectStellarWallet } from './stellar';
describe('connectStellarWallet', () => {
beforeEach(() => {
vi.clearAllMocks();
// @ts-expect-error test double
delete window.albedo;
// @ts-expect-error test double
delete window.xBullSDK;
// @ts-expect-error test double
delete window.rabet;
// @ts-expect-error test double
delete window.hana;
});
afterEach(() => {
vi.useRealTimers();
});
it('connects xBull via xBullSDK instead of Freighter', async () => {
const connect = vi.fn().mockResolvedValue(undefined);
const getPublicKeys = vi
.fn()
.mockResolvedValue([{ publicKey: 'GXBULLPUBLICKEY' }]);
// @ts-expect-error test double
window.xBullSDK = { connect, getPublicKeys };
const wallet = await connectStellarWallet('xbull');
expect(connect).toHaveBeenCalled();
expect(getPublicKeys).toHaveBeenCalled();
expect(requestAccess).not.toHaveBeenCalled();
expect(wallet).toMatchObject({
id: 'xbull',
name: 'xBull',
address: 'GXBULLPUBLICKEY',
family: 'stellar'
});
});
it('connects Albedo via window.albedo.publicKey', async () => {
// @ts-expect-error test double
window.albedo = {
publicKey: vi
.fn()
.mockResolvedValue({ pubkey: 'GALBEDOPUBLICKEY' })
};
const wallet = await connectStellarWallet('albedo');
expect(requestAccess).not.toHaveBeenCalled();
expect(wallet).toMatchObject({
id: 'albedo',
address: 'GALBEDOPUBLICKEY'
});
});
it('connects Rabet via window.rabet.connect', async () => {
// @ts-expect-error test double
window.rabet = {
connect: vi
.fn()
.mockResolvedValue({ publicKey: 'GRABETPUBLICKEY' })
};
const wallet = await connectStellarWallet('rabet');
expect(requestAccess).not.toHaveBeenCalled();
expect(wallet).toMatchObject({
id: 'rabet',
address: 'GRABETPUBLICKEY'
});
});
it('connects Hana via window.hana.getPublicKey', async () => {
// @ts-expect-error test double
window.hana = {
getPublicKey: vi
.fn()
.mockResolvedValue('GHANAPUBLICKEY')
};
const wallet = await connectStellarWallet('hana-wallet');
expect(requestAccess).not.toHaveBeenCalled();
expect(wallet).toMatchObject({
id: 'hana-wallet',
name: 'Hana',
address: 'GHANAPUBLICKEY'
});
});
it('fails fast for missing xBull instead of hanging on Freighter', async () => {
await expect(
connectStellarWallet('xbull')
).rejects.toThrow(/xBull is not installed/i);
expect(requestAccess).not.toHaveBeenCalled();
});
it('rejects unknown stellar wallet ids', async () => {
await expect(
// @ts-expect-error intentional bad id
connectStellarWallet('not-a-wallet')
).rejects.toThrow(/not supported/i);
expect(requestAccess).not.toHaveBeenCalled();
});
it('times out hanging wallet SDK calls', async () => {
vi.useFakeTimers();
// @ts-expect-error test double
window.xBullSDK = {
connect: () => new Promise(() => undefined)
};
const pending = connectStellarWallet('xbull');
const expectation = expect(pending).rejects.toThrow(
/timed out/i
);
await vi.advanceTimersByTimeAsync(30_000);
await expectation;
expect(requestAccess).not.toHaveBeenCalled();
});
});