forked from ant-design/ant-design-web3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile-modal.test.tsx
269 lines (244 loc) · 8.2 KB
/
profile-modal.test.tsx
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { Polygon } from '@ant-design/web3-assets';
import { fireEvent, render } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { ConnectButton } from '..';
import useIntl from '../../hooks/useIntl';
import { readCopyText } from '../../utils/test-utils';
import { ProfileModal } from '../profile-modal';
describe('ProfileModal', () => {
it('match snapshot', () => {
const App = () => {
const intl = useIntl('ConnectButton');
return (
<ProfileModal
intl={intl}
open
__hashId__="hashId"
address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B"
name="wanderingearth.eth"
avatar={{
src: 'https://mdn.alipayobjects.com/huamei_mutawc/afts/img/A*9jfLS41kn00AAAAAAAAAAAAADlrGAQ/original',
}}
/>
);
};
const { baseElement } = render(<App />);
expect(baseElement).toMatchSnapshot();
});
it('match snapshot without name', () => {
const App = () => {
const intl = useIntl('ConnectButton');
return (
<ProfileModal
intl={intl}
open
__hashId__="hashId"
address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B"
avatar={{
src: 'https://mdn.alipayobjects.com/huamei_mutawc/afts/img/A*9jfLS41kn00AAAAAAAAAAAAADlrGAQ/original',
}}
/>
);
};
const { baseElement } = render(<App />);
expect(baseElement).toMatchSnapshot();
});
it('open profile modal in ConnectButton', async () => {
const App = () => (
<ConnectButton
account={{
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
}}
/>
);
const { baseElement } = render(<App />);
expect(baseElement.querySelector('.ant-web3-connect-button')).not.toBeNull();
fireEvent.click(baseElement.querySelector('.ant-web3-connect-button')!);
await vi.waitFor(() => {
expect(
baseElement.querySelector('.ant-web3-connect-button-profile-modal .ant-web3-address')
?.textContent,
).toBe('0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B');
expect(baseElement.querySelector('.anticon-user')).not.toBeNull();
});
});
it('show balance', async () => {
const App = () => (
<ConnectButton
account={{
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
}}
balance={{
symbol: 'ETH',
decimals: 18,
value: 1230000000000000000n,
}}
/>
);
const { baseElement } = render(<App />);
expect(baseElement.querySelector('.ant-web3-connect-button')).not.toBeNull();
fireEvent.click(baseElement.querySelector('.ant-web3-connect-button')!);
await vi.waitFor(() => {
expect(
baseElement.querySelector('.ant-web3-connect-button-profile-modal .ant-web3-address')
?.textContent,
).toBe('0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B');
expect(
baseElement.querySelector(
'.ant-web3-connect-button-profile-modal .ant-web3-crypto-price-balance',
)?.textContent,
).toBe('1.23 ETH');
});
});
it('show default avatar', async () => {
const App = () => (
<ConnectButton
account={{
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
}}
/>
);
const { baseElement } = render(<App />);
expect(baseElement.querySelector('.ant-web3-connect-button')).not.toBeNull();
fireEvent.click(baseElement.querySelector('.ant-web3-connect-button')!);
await vi.waitFor(() => {
expect(
baseElement.querySelector(
'.ant-web3-connect-button-chain-icon.ant-web3-connect-button-default-icon',
),
).not.toBeNull();
});
});
it('show chain icon as the default avatar', async () => {
const App = () => (
<ConnectButton
account={{
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
}}
chain={Polygon}
/>
);
const { baseElement } = render(<App />);
expect(baseElement.querySelector('.ant-web3-connect-button')).not.toBeNull();
fireEvent.click(baseElement.querySelector('.ant-web3-connect-button')!);
await vi.waitFor(() => {
expect(
baseElement.querySelector(
'.ant-web3-connect-button-chain-icon .ant-web3-icon-polygon-circle-colorful',
),
).not.toBeNull();
});
});
it('Disconnect & Copy Address Button', () => {
const disconnectTestFn = vi.fn();
const App = () => {
const intl = useIntl('ConnectButton');
return (
<ProfileModal
open
intl={intl}
__hashId__="hashId"
address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B"
onDisconnect={disconnectTestFn}
/>
);
};
const { baseElement } = render(<App />);
const btns = baseElement.querySelectorAll('.ant-web3-connect-button-profile-modal .ant-btn');
expect(btns.length).toBe(2);
fireEvent.click(btns[1]);
expect(disconnectTestFn).toBeCalled();
fireEvent.click(btns[0]);
expect(readCopyText()).resolves.toBe('0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B');
});
it('should not display modal when pass false into profileModal', async () => {
const App = () => (
<ConnectButton
account={{
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
}}
profileModal={false}
/>
);
const { baseElement } = render(<App />);
fireEvent.click(baseElement.querySelector('.ant-web3-connect-button')!);
await vi.waitFor(() => {
expect(baseElement.querySelector('.ant-web3-connect-button-profile-modal')).toBeNull();
});
});
it('profile modal should customize by profileModal', async () => {
const App = () => (
<ConnectButton
account={{
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
}}
profileModal={{
title: 'Custom Title',
footer: 'Custom Description',
width: 500,
}}
/>
);
const { baseElement } = render(<App />);
fireEvent.click(baseElement.querySelector('.ant-web3-connect-button')!);
await vi.waitFor(() => {
expect(baseElement.querySelector('.ant-web3-connect-button-profile-modal')).not.toBeNull();
expect(
baseElement.querySelector('.ant-web3-connect-button-profile-modal .ant-modal-title')
?.textContent,
).toBe('Custom Title');
expect(
baseElement.querySelector('.ant-web3-connect-button-profile-modal .ant-modal-footer')
?.textContent,
).toBe('Custom Description');
expect(
baseElement
.querySelector('.ant-web3-connect-button-profile-modal.ant-modal')
?.getAttribute('style'),
).toContain('width: 500px;');
});
});
it('profile modal should can customize reset styles by `profileModal.styles`', async () => {
const App = () => (
<ConnectButton
account={{
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
}}
profileModal={{
title: 'Custom Title',
footer: 'Custom Description',
width: 500,
styles: {
header: {
fontSize: 32,
},
body: {
textAlign: 'right',
fontSize: 32,
},
footer: {
fontSize: 32,
},
},
}}
/>
);
const { baseElement } = render(<App />);
fireEvent.click(baseElement.querySelector('.ant-web3-connect-button')!);
await vi.waitFor(() => {
const headerStyleAttr = baseElement
.querySelector('.ant-web3-connect-button-profile-modal .ant-modal-header')
?.getAttribute('style');
const bodyStyleAttr = baseElement
.querySelector('.ant-web3-connect-button-profile-modal .ant-modal-body')
?.getAttribute('style');
const footerStyleAttr = baseElement
.querySelector('.ant-web3-connect-button-profile-modal .ant-modal-footer')
?.getAttribute('style');
expect(headerStyleAttr).toContain('font-size: 32px;');
expect(bodyStyleAttr).toContain('text-align: right;');
expect(bodyStyleAttr).toContain('font-size: 32px;');
expect(footerStyleAttr).toContain('font-size: 32px;');
});
});
});