-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathOwnedTokensCard.spec.tsx
More file actions
123 lines (107 loc) · 4.29 KB
/
OwnedTokensCard.spec.tsx
File metadata and controls
123 lines (107 loc) · 4.29 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
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { PublicKey } from '@solana/web3.js';
import React from 'react';
import { vi } from 'vitest';
import { OwnedTokensCard } from '@/app/components/account/OwnedTokensCard';
import { FetchStatus } from '@/app/providers/cache';
import type { TokenInfoWithPubkey } from '@/app/providers/accounts/tokens';
vi.mock('next/navigation', () => ({
usePathname: vi.fn(() => '/address/mock/tokens'),
useSearchParams: vi.fn(() => new URLSearchParams()),
}));
vi.mock('next/link', () => ({
__esModule: true,
default: ({ children, ...props }: React.ComponentProps<'a'>) => <a {...props}>{children}</a>,
}));
vi.mock('next/image', () => ({
__esModule: true,
default: ({ alt }: { alt: string }) => <img alt={alt} />,
}));
vi.mock('@components/common/Address', () => ({
Address: ({ pubkey }: { pubkey: { toBase58: () => string } }) => <span>{pubkey.toBase58()}</span>,
}));
vi.mock('@components/account/token-extensions/ScaledUiAmountMultiplierTooltip', () => ({
__esModule: true,
default: () => null,
}));
vi.mock('@providers/accounts/tokens', async () => {
const actual = await vi.importActual<typeof import('@providers/accounts/tokens')>(
'@providers/accounts/tokens'
);
return {
...actual,
useAccountOwnedTokens: vi.fn(),
useFetchAccountOwnedTokens: vi.fn(),
useScaledUiAmountForMint: vi.fn(() => ['0', '1']),
};
});
import {
useAccountOwnedTokens,
useFetchAccountOwnedTokens,
} from '@providers/accounts/tokens';
describe('OwnedTokensCard', () => {
const mockAddress = '7gN7aPfYZ7R6pujYnXghDUFxuDrGRq3NbS1hVeXeXzKZ';
const solMint = new PublicKey('So11111111111111111111111111111111111111112');
const bonkMint = new PublicKey('DezXAZ8z7PnrnRJgsSummSgGDn8uQmSez2w6zxDd4kPx');
const mockTokens: TokenInfoWithPubkey[] = [
{
info: {
isNative: false,
mint: solMint,
owner: new PublicKey('11111111111111111111111111111111'),
state: 'initialized',
tokenAmount: {
amount: '1000000000',
decimals: 9,
uiAmountString: '1',
},
},
name: 'Solana',
pubkey: new PublicKey('H3Y2Yk4EYBLETymFcpnSUsctNkYheAQ7EzxKQEiC5c6a'),
symbol: 'SOL',
},
{
info: {
isNative: false,
mint: bonkMint,
owner: new PublicKey('11111111111111111111111111111111'),
state: 'initialized',
tokenAmount: {
amount: '500000',
decimals: 5,
uiAmountString: '5',
},
},
name: 'Bonk',
pubkey: new PublicKey('3h6sEBQZG9hV91bLbX6FVUFYttGjoHNJpX15HwNhEx27'),
symbol: 'BONK',
},
];
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(useAccountOwnedTokens).mockReturnValue({
data: { tokens: mockTokens },
status: FetchStatus.Fetched,
});
vi.mocked(useFetchAccountOwnedTokens).mockReturnValue(vi.fn());
});
it('filters tokens using the find-as-you-type input', async () => {
const user = userEvent.setup();
render(<OwnedTokensCard address={mockAddress} />);
const input = screen.getByPlaceholderText('Find token');
await user.type(input, 'bonk');
expect(screen.getByText(bonkMint.toBase58())).toBeInTheDocument();
expect(screen.queryByText(solMint.toBase58())).not.toBeInTheDocument();
expect(screen.queryByText('No tokens match this search')).not.toBeInTheDocument();
});
it('shows an empty state message when no tokens match the filter', async () => {
const user = userEvent.setup();
render(<OwnedTokensCard address={mockAddress} />);
const input = screen.getByPlaceholderText('Find token');
await user.type(input, 'missing-token');
expect(screen.getByText('No tokens match this search')).toBeInTheDocument();
expect(screen.queryByText(solMint.toBase58())).not.toBeInTheDocument();
expect(screen.queryByText(bonkMint.toBase58())).not.toBeInTheDocument();
});
});