Skip to content

Commit 7040bee

Browse files
committed
refactor copy handlers
1 parent cc78b4b commit 7040bee

File tree

2 files changed

+51
-47
lines changed

2 files changed

+51
-47
lines changed

src/wallet/components/WalletAdvancedQrReceive.test.tsx

+24-18
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ describe('WalletAdvancedQrReceive', () => {
165165

166166
render(<WalletAdvancedQrReceive />);
167167

168-
const copyIcon = screen.getByTestId('ockWalletAdvancedQrReceive_CopyIcon');
168+
const copyIcon = screen.getByRole('button', {
169+
name: 'Copy your address by clicking the icon',
170+
});
169171

170172
await act(async () => {
171173
fireEvent.click(copyIcon);
@@ -176,9 +178,9 @@ describe('WalletAdvancedQrReceive', () => {
176178
expect(mockClipboard.writeText).toHaveBeenCalledWith('0x1234567890');
177179
expect(mockSetCopyText).toHaveBeenCalledWith('Copied');
178180

179-
const tooltip = screen.getByTestId(
180-
'ockWalletAdvancedQrReceive_CopyTooltip',
181-
);
181+
const tooltip = screen.getByRole('button', {
182+
name: 'Copy your address by clicking the tooltip',
183+
});
182184
expect(tooltip).toBeInTheDocument();
183185

184186
vi.advanceTimersByTime(2000);
@@ -205,9 +207,9 @@ describe('WalletAdvancedQrReceive', () => {
205207

206208
render(<WalletAdvancedQrReceive />);
207209

208-
const copyTooltip = screen.getByTestId(
209-
'ockWalletAdvancedQrReceive_CopyTooltip',
210-
);
210+
const copyTooltip = screen.getByRole('button', {
211+
name: 'Copy your address by clicking the tooltip',
212+
});
211213

212214
await act(async () => {
213215
fireEvent.click(copyTooltip);
@@ -218,9 +220,9 @@ describe('WalletAdvancedQrReceive', () => {
218220
expect(mockClipboard.writeText).toHaveBeenCalledWith('0x1234567890');
219221
expect(mockSetCopyText).toHaveBeenCalledWith('Copied');
220222

221-
const tooltip = screen.getByTestId(
222-
'ockWalletAdvancedQrReceive_CopyTooltip',
223-
);
223+
const tooltip = screen.getByRole('button', {
224+
name: 'Copy your address by clicking the tooltip',
225+
});
224226
expect(tooltip).toBeInTheDocument();
225227

226228
vi.advanceTimersByTime(2000);
@@ -247,9 +249,9 @@ describe('WalletAdvancedQrReceive', () => {
247249

248250
render(<WalletAdvancedQrReceive />);
249251

250-
const copyButton = screen.getByTestId(
251-
'ockCopyButton',
252-
);
252+
const copyButton = screen.getByRole('button', {
253+
name: 'Copy your address by clicking the button',
254+
});
253255

254256
await act(async () => {
255257
fireEvent.click(copyButton);
@@ -280,7 +282,9 @@ describe('WalletAdvancedQrReceive', () => {
280282
render(<WalletAdvancedQrReceive />);
281283

282284
mockSetCopyText.mockClear();
283-
const copyIcon = screen.getByTestId('ockWalletAdvancedQrReceive_CopyIcon');
285+
const copyIcon = screen.getByRole('button', {
286+
name: 'Copy your address by clicking the icon',
287+
});
284288
await act(async () => {
285289
fireEvent.click(copyIcon);
286290
await Promise.resolve();
@@ -292,9 +296,9 @@ describe('WalletAdvancedQrReceive', () => {
292296
vi.advanceTimersByTime(2000);
293297

294298
mockSetCopyButtonText.mockClear();
295-
const copyButton = screen.getByTestId(
296-
'ockCopyButton',
297-
);
299+
const copyButton = screen.getByRole('button', {
300+
name: 'Copy your address by clicking the button',
301+
});
298302
await act(async () => {
299303
fireEvent.click(copyButton);
300304
await Promise.resolve();
@@ -315,7 +319,9 @@ describe('WalletAdvancedQrReceive', () => {
315319

316320
render(<WalletAdvancedQrReceive />);
317321

318-
const copyIcon = screen.getByTestId('ockWalletAdvancedQrReceive_CopyIcon');
322+
const copyIcon = screen.getByRole('button', {
323+
name: 'Copy your address by clicking the icon',
324+
});
319325
fireEvent.click(copyIcon);
320326

321327
expect(mockClipboard.writeText).toHaveBeenCalledWith('');

src/wallet/components/WalletAdvancedQrReceive.tsx

+27-29
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,25 @@ export function WalletAdvancedQrReceive() {
3535
}, 2000);
3636
}, []);
3737

38-
const handleCopySuccess = useCallback(
39-
(element: 'button' | 'icon') => {
40-
if (element === 'button') {
41-
setCopyButtonText('Address copied');
42-
} else {
43-
setCopyText('Copied');
44-
}
45-
resetAffordanceText();
46-
},
47-
[resetAffordanceText],
48-
);
38+
const handleCopyButtonSuccess = useCallback(() => {
39+
setCopyButtonText('Address copied');
40+
resetAffordanceText();
41+
}, [resetAffordanceText]);
4942

50-
const handleCopyError = useCallback(
51-
(element: 'button' | 'icon') => {
52-
if (element === 'button') {
53-
setCopyButtonText('Failed to copy address');
54-
} else {
55-
setCopyText('Failed to copy');
56-
}
57-
resetAffordanceText();
58-
},
59-
[resetAffordanceText],
60-
);
43+
const handleCopyButtonError = useCallback(() => {
44+
setCopyButtonText('Failed to copy address');
45+
resetAffordanceText();
46+
}, [resetAffordanceText]);
47+
48+
const handleCopyIconSuccess = useCallback(() => {
49+
setCopyText('Copied');
50+
resetAffordanceText();
51+
}, [resetAffordanceText]);
52+
53+
const handleCopyIconError = useCallback(() => {
54+
setCopyText('Failed to copy');
55+
resetAffordanceText();
56+
}, [resetAffordanceText]);
6157

6258
if (isSubComponentClosing) {
6359
return null;
@@ -87,20 +83,21 @@ export function WalletAdvancedQrReceive() {
8783
<CopyButton
8884
label={copySvg}
8985
copyValue={address ?? ''}
90-
onSuccess={() => handleCopySuccess('icon')}
91-
onError={() => handleCopyError('icon')}
86+
onSuccess={handleCopyIconSuccess}
87+
onError={handleCopyIconError}
9288
className={cn(
9389
pressable.default,
9490
border.radiusInner,
9591
border.default,
9692
'flex items-center justify-center p-2',
9793
)}
94+
aria-label="Copy your address by clicking the icon"
9895
/>
9996
<CopyButton
10097
label={copyText}
10198
copyValue={address ?? ''}
102-
onSuccess={() => handleCopySuccess('icon')}
103-
onError={() => handleCopyError('icon')}
99+
onSuccess={handleCopyIconSuccess}
100+
onError={handleCopyIconError}
104101
className={cn(
105102
pressable.alternate,
106103
text.legal,
@@ -110,7 +107,7 @@ export function WalletAdvancedQrReceive() {
110107
zIndex.dropdown,
111108
'absolute top-full right-0 mt-0.5 px-1.5 py-0.5 opacity-0 transition-opacity group-hover:opacity-100',
112109
)}
113-
aria-label="Copy your address"
110+
aria-label="Copy your address by clicking the tooltip"
114111
/>
115112
</div>
116113
</div>
@@ -119,8 +116,9 @@ export function WalletAdvancedQrReceive() {
119116
copyValue={address ?? ''}
120117
label={copyButtonText}
121118
className={cn(border.radius, pressable.alternate, 'w-full p-3')}
122-
onSuccess={() => handleCopySuccess('button')}
123-
onError={() => handleCopyError('button')}
119+
onSuccess={handleCopyButtonSuccess}
120+
onError={handleCopyButtonError}
121+
aria-label="Copy your address by clicking the button"
124122
/>
125123
</div>
126124
);

0 commit comments

Comments
 (0)