|
| 1 | +import { ETHEREUM_NETWORK } from '$env/networks/networks.eth.env'; |
| 2 | +import { clearIdbNfts, getIdbAllNfts, setIdbAllNfts } from '$lib/api/idb-nfts.api'; |
| 3 | +import type { SerializableNft } from '$lib/types/idb-nfts'; |
| 4 | +import { mockIdentity, mockPrincipal } from '$tests/mocks/identity.mock'; |
| 5 | +import { mockValidErc1155Nft, mockValidErc721Nft } from '$tests/mocks/nfts.mock'; |
| 6 | +import * as idbKeyval from 'idb-keyval'; |
| 7 | + |
| 8 | +vi.mock('$app/environment', () => ({ |
| 9 | + browser: true |
| 10 | +})); |
| 11 | + |
| 12 | +describe('idb-nfts.api', () => { |
| 13 | + const mockNfts = [mockValidErc721Nft, mockValidErc1155Nft]; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + vi.clearAllMocks(); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('setIdbAllNfts', () => { |
| 20 | + it('should not set NFTs in IDB if identity is nullish', async () => { |
| 21 | + await setIdbAllNfts({ identity: null, nfts: mockNfts }); |
| 22 | + |
| 23 | + expect(idbKeyval.set).not.toHaveBeenCalled(); |
| 24 | + |
| 25 | + await setIdbAllNfts({ identity: undefined, nfts: mockNfts }); |
| 26 | + |
| 27 | + expect(idbKeyval.set).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should serialize and store NFTs in IDB', async () => { |
| 31 | + await setIdbAllNfts({ identity: mockIdentity, nfts: mockNfts }); |
| 32 | + |
| 33 | + expect(idbKeyval.set).toHaveBeenCalledOnce(); |
| 34 | + |
| 35 | + const [[key, storedNfts]] = vi.mocked(idbKeyval.set).mock.calls; |
| 36 | + |
| 37 | + expect(key).toBe(mockPrincipal.toText()); |
| 38 | + |
| 39 | + const serialized = storedNfts as SerializableNft[]; |
| 40 | + |
| 41 | + expect(serialized).toHaveLength(2); |
| 42 | + |
| 43 | + expect(typeof serialized[0].collection.id).toBe('string'); |
| 44 | + expect(typeof serialized[0].collection.network.id).toBe('string'); |
| 45 | + expect(serialized[0].collection.id).toBe(`${mockValidErc721Nft.collection.id.description}`); |
| 46 | + expect(serialized[0].collection.network.id).toBe( |
| 47 | + `${mockValidErc721Nft.collection.network.id.description}` |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle empty NFTs list', async () => { |
| 52 | + await setIdbAllNfts({ identity: mockIdentity, nfts: [] }); |
| 53 | + |
| 54 | + expect(idbKeyval.set).toHaveBeenCalledOnce(); |
| 55 | + |
| 56 | + const [[, storedNfts]] = vi.mocked(idbKeyval.set).mock.calls; |
| 57 | + |
| 58 | + expect(storedNfts).toEqual([]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should preserve non-symbol fields during serialization', async () => { |
| 62 | + await setIdbAllNfts({ identity: mockIdentity, nfts: [mockValidErc1155Nft] }); |
| 63 | + |
| 64 | + const [[, storedNfts]] = vi.mocked(idbKeyval.set).mock.calls; |
| 65 | + const [serialized] = storedNfts as SerializableNft[]; |
| 66 | + |
| 67 | + expect(serialized.id).toBe(mockValidErc1155Nft.id); |
| 68 | + expect(serialized.name).toBe(mockValidErc1155Nft.name); |
| 69 | + expect(serialized.balance).toBe(mockValidErc1155Nft.balance); |
| 70 | + expect(serialized.imageUrl).toBe(mockValidErc1155Nft.imageUrl); |
| 71 | + expect(serialized.acquiredAt).toEqual(mockValidErc1155Nft.acquiredAt); |
| 72 | + expect(serialized.collection.address).toBe(mockValidErc1155Nft.collection.address); |
| 73 | + expect(serialized.collection.name).toBe(mockValidErc1155Nft.collection.name); |
| 74 | + expect(serialized.collection.symbol).toBe(mockValidErc1155Nft.collection.symbol); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('getIdbAllNfts', () => { |
| 79 | + it('should return undefined when no cached data exists', async () => { |
| 80 | + vi.mocked(idbKeyval.get).mockResolvedValue(undefined); |
| 81 | + |
| 82 | + const result = await getIdbAllNfts(mockPrincipal); |
| 83 | + |
| 84 | + expect(result).toBeUndefined(); |
| 85 | + expect(idbKeyval.get).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should deserialize NFTs restoring Symbol IDs', async () => { |
| 89 | + const serialized: SerializableNft[] = [ |
| 90 | + { |
| 91 | + ...mockValidErc721Nft, |
| 92 | + collection: { |
| 93 | + ...mockValidErc721Nft.collection, |
| 94 | + id: `${mockValidErc721Nft.collection.id.description}`, |
| 95 | + network: { |
| 96 | + ...mockValidErc721Nft.collection.network, |
| 97 | + id: `${mockValidErc721Nft.collection.network.id.description}` |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + ]; |
| 102 | + |
| 103 | + vi.mocked(idbKeyval.get).mockResolvedValue(serialized); |
| 104 | + |
| 105 | + const result = await getIdbAllNfts(mockPrincipal); |
| 106 | + |
| 107 | + expect(result).toHaveLength(1); |
| 108 | + expect(typeof result?.[0].collection.id).toBe('symbol'); |
| 109 | + expect(typeof result?.[0].collection.network.id).toBe('symbol'); |
| 110 | + expect(result?.[0].collection.id.description).toBe( |
| 111 | + mockValidErc721Nft.collection.id.description |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should restore singleton Network for known networks', async () => { |
| 116 | + const serialized: SerializableNft[] = [ |
| 117 | + { |
| 118 | + ...mockValidErc721Nft, |
| 119 | + collection: { |
| 120 | + ...mockValidErc721Nft.collection, |
| 121 | + id: `${mockValidErc721Nft.collection.id.description}`, |
| 122 | + network: { |
| 123 | + ...ETHEREUM_NETWORK, |
| 124 | + id: `${ETHEREUM_NETWORK.id.description}` |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + ]; |
| 129 | + |
| 130 | + vi.mocked(idbKeyval.get).mockResolvedValue(serialized); |
| 131 | + |
| 132 | + const result = await getIdbAllNfts(mockPrincipal); |
| 133 | + |
| 134 | + expect(result?.[0].collection.network.id).toBe(ETHEREUM_NETWORK.id); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should preserve non-symbol fields during deserialization', async () => { |
| 138 | + const serialized: SerializableNft[] = [ |
| 139 | + { |
| 140 | + ...mockValidErc1155Nft, |
| 141 | + collection: { |
| 142 | + ...mockValidErc1155Nft.collection, |
| 143 | + id: `${mockValidErc1155Nft.collection.id.description}`, |
| 144 | + network: { |
| 145 | + ...mockValidErc1155Nft.collection.network, |
| 146 | + id: `${mockValidErc1155Nft.collection.network.id.description}` |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + ]; |
| 151 | + |
| 152 | + vi.mocked(idbKeyval.get).mockResolvedValue(serialized); |
| 153 | + |
| 154 | + const result = await getIdbAllNfts(mockPrincipal); |
| 155 | + const [nft] = result ?? []; |
| 156 | + |
| 157 | + expect(nft.id).toBe(mockValidErc1155Nft.id); |
| 158 | + expect(nft.name).toBe(mockValidErc1155Nft.name); |
| 159 | + expect(nft.balance).toBe(mockValidErc1155Nft.balance); |
| 160 | + expect(nft.acquiredAt).toEqual(mockValidErc1155Nft.acquiredAt); |
| 161 | + expect(nft.collection.address).toBe(mockValidErc1155Nft.collection.address); |
| 162 | + expect(nft.collection.name).toBe(mockValidErc1155Nft.collection.name); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should roundtrip serialize then deserialize correctly', async () => { |
| 166 | + await setIdbAllNfts({ identity: mockIdentity, nfts: mockNfts }); |
| 167 | + |
| 168 | + const [[, storedNfts]] = vi.mocked(idbKeyval.set).mock.calls; |
| 169 | + |
| 170 | + vi.mocked(idbKeyval.get).mockResolvedValue(storedNfts); |
| 171 | + |
| 172 | + const result = await getIdbAllNfts(mockPrincipal); |
| 173 | + |
| 174 | + expect(result).toHaveLength(mockNfts.length); |
| 175 | + |
| 176 | + result?.forEach((nft, i) => { |
| 177 | + expect(nft.id).toBe(mockNfts[i].id); |
| 178 | + expect(nft.name).toBe(mockNfts[i].name); |
| 179 | + expect(typeof nft.collection.id).toBe('symbol'); |
| 180 | + expect(typeof nft.collection.network.id).toBe('symbol'); |
| 181 | + expect(nft.collection.id.description).toBe(mockNfts[i].collection.id.description); |
| 182 | + expect(nft.collection.network.id.description).toBe( |
| 183 | + mockNfts[i].collection.network.id.description |
| 184 | + ); |
| 185 | + }); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + describe('clearIdbNfts', () => { |
| 190 | + it('should clear the NFTs IDB store', async () => { |
| 191 | + await clearIdbNfts(); |
| 192 | + |
| 193 | + expect(idbKeyval.clear).toHaveBeenCalledExactlyOnceWith(expect.any(Object)); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments