-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNftApiClient.test.ts
More file actions
154 lines (140 loc) · 4.8 KB
/
Copy pathNftApiClient.test.ts
File metadata and controls
154 lines (140 loc) · 4.8 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* eslint-disable @typescript-eslint/naming-convention */
import type { ICache } from '../../caching/ICache';
import { InMemoryCache } from '../../caching/InMemoryCache';
import type { Serializable } from '../../serialization/types';
import type { ConfigProvider } from '../../services/config';
import type { ILogger } from '../../utils/logger';
import { MOCK_NFT_METADATA_RESPONSE_MAPPED } from './mocks/mockNftMetadataResponseMapped';
import { MOCK_NFT_METADATA_RESPONSE_RAW } from './mocks/mockNftMetadataResponseRaw';
import { MOCK_NFTS_LIST_RESPONSE_MAPPED } from './mocks/mockNftsListResponseMapped';
import { MOCK_NFTS_LIST_RESPONSE_RAW } from './mocks/mockNftsListResponseRaw';
import { NftApiClient } from './NftApiClient';
const mockFetch = jest.fn();
const mockLogger = {
info: jest.fn(console.info),
error: jest.fn(console.error),
warn: jest.fn(console.error),
} as unknown as ILogger;
let mockCache: ICache<Serializable>;
describe('NftApiClient', () => {
let client: NftApiClient;
let mockConfigProvider: ConfigProvider;
beforeEach(() => {
jest.clearAllMocks();
mockCache = new InMemoryCache(mockLogger);
mockConfigProvider = {
get: jest.fn().mockReturnValue({
nftApi: {
baseUrl: 'https://some-mock-url.com',
cacheTtlsMilliseconds: {
listAddressSolanaNfts: 0,
getNftMetadata: 0,
},
},
}),
} as unknown as ConfigProvider;
client = new NftApiClient(
mockConfigProvider,
mockCache,
mockFetch,
mockLogger,
);
});
describe('constructor', () => {
it('rejects invalid baseUrl', async () => {
const invalidConfigProvider = {
get: jest.fn().mockReturnValue({
nftApi: {
baseUrl: 'invalid-url',
cacheTtlsMilliseconds: {
listAddressSolanaNfts: 0,
getNftMetadata: 0,
},
},
}),
} as unknown as ConfigProvider;
expect(
() =>
new NftApiClient(
invalidConfigProvider,
mockCache,
mockFetch,
mockLogger,
),
).toThrow('Invalid URL format');
});
});
describe('listAddressSolanaNfts', () => {
it('should correctly map the API response, covering the logic from mapListAddressSolanaNftsResponse.test.ts', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce({
...MOCK_NFTS_LIST_RESPONSE_RAW,
cursor: null,
}),
});
const nfts = await client.listAddressSolanaNfts('some-address');
expect(nfts).toStrictEqual(MOCK_NFTS_LIST_RESPONSE_MAPPED.items);
});
it('should handle pagination', async () => {
const MOCK_NFTS_LIST_RESPONSE_RAW_PAGE_1 = {
...MOCK_NFTS_LIST_RESPONSE_RAW,
cursor: 'some-cursor',
};
const MOCK_NFTS_LIST_RESPONSE_RAW_PAGE_2 = {
...MOCK_NFTS_LIST_RESPONSE_RAW,
cursor: null,
};
mockFetch.mockResolvedValueOnce({
ok: true,
json: jest
.fn()
.mockResolvedValueOnce(MOCK_NFTS_LIST_RESPONSE_RAW_PAGE_1),
});
mockFetch.mockResolvedValueOnce({
ok: true,
json: jest
.fn()
.mockResolvedValueOnce(MOCK_NFTS_LIST_RESPONSE_RAW_PAGE_2),
});
const nfts = await client.listAddressSolanaNfts('some-address');
expect(nfts).toStrictEqual([
...MOCK_NFTS_LIST_RESPONSE_MAPPED.items,
...MOCK_NFTS_LIST_RESPONSE_MAPPED.items,
]);
expect(mockFetch).toHaveBeenCalledTimes(2);
});
});
describe('getNftMetadata', () => {
it('should fetch and parse solana nft metadata for a given token address', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce(MOCK_NFT_METADATA_RESPONSE_RAW),
});
const nft = await client.getNftMetadata('some-token-address');
expect(nft).toStrictEqual(MOCK_NFT_METADATA_RESPONSE_MAPPED);
});
it('should return null if the fetch fails', async () => {
mockFetch.mockRejectedValueOnce(new Error('some-error'));
const nft = await client.getNftMetadata('some-token-address');
expect(nft).toBeNull();
});
});
describe('getNftsMetadata', () => {
it('should fetch and parse solana nft metadata for a given array of token addresses', async () => {
mockFetch.mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue(MOCK_NFT_METADATA_RESPONSE_RAW),
});
const nfts = await client.getNftsMetadata([
'some-token-address-1',
'some-token-address-2',
]);
expect(nfts).toStrictEqual([
MOCK_NFT_METADATA_RESPONSE_MAPPED,
MOCK_NFT_METADATA_RESPONSE_MAPPED,
]);
expect(mockFetch).toHaveBeenCalledTimes(2);
});
});
});