-
Notifications
You must be signed in to change notification settings - Fork 670
Expand file tree
/
Copy pathuse-das-image.spec.ts
More file actions
66 lines (51 loc) · 2.41 KB
/
Copy pathuse-das-image.spec.ts
File metadata and controls
66 lines (51 loc) · 2.41 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
import { renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Cluster, clusterSlug } from '@/app/utils/cluster';
vi.mock('@/app/providers/cluster', () => ({ useCluster: vi.fn() }));
vi.mock('swr', () => ({ default: vi.fn() }));
import useSWR from 'swr';
import { useCluster } from '@/app/providers/cluster';
import { useDasImage } from '../use-das-image';
describe('useDasImage', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(useCluster).mockReturnValue({ cluster: Cluster.MainnetBeta, customUrl: '' } as ReturnType<
typeof useCluster
>);
vi.mocked(useSWR).mockReturnValue({ data: undefined } as ReturnType<typeof useSWR>);
});
it('should return undefined when no mintAddress', () => {
const { result } = renderHook(() => useDasImage(undefined));
expect(result.current).toBeUndefined();
expect(useSWR).toHaveBeenCalledWith(undefined, expect.any(Function), expect.any(Object));
});
it('should return the image URL from SWR data', () => {
vi.mocked(useSWR).mockReturnValue({ data: 'https://example.com/image.png' } as ReturnType<typeof useSWR>);
const { result } = renderHook(() => useDasImage('SomeMintAddress'));
expect(result.current).toBe('https://example.com/image.png');
});
it('should return undefined when SWR has no data', () => {
const { result } = renderHook(() => useDasImage('SomeMintAddress'));
expect(result.current).toBeUndefined();
});
it('should pass correct SWR key including cluster slug and customUrl', () => {
vi.mocked(useCluster).mockReturnValue({
cluster: Cluster.Devnet,
customUrl: 'https://custom.rpc',
} as ReturnType<typeof useCluster>);
renderHook(() => useDasImage('SomeMintAddress'));
expect(useSWR).toHaveBeenCalledWith(
['das-image', 'SomeMintAddress', clusterSlug(Cluster.Devnet), 'https://custom.rpc'],
expect.any(Function),
expect.any(Object),
);
});
it('should pass correct SWR config', () => {
renderHook(() => useDasImage('SomeMintAddress'));
expect(useSWR).toHaveBeenCalledWith(expect.any(Array), expect.any(Function), {
dedupingInterval: 5 * 60 * 1000,
revalidateOnFocus: false,
revalidateOnReconnect: false,
});
});
});