|
| 1 | +import { type Mock, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { getTokenDetails } from './getTokenDetails'; |
| 3 | +import { sendRequest } from '../network/request'; |
| 4 | +import { |
| 5 | + CDP_GET_MINT_DETAILS, |
| 6 | + CDP_GET_TOKEN_DETAILS, |
| 7 | +} from '../network/definitions/nft'; |
| 8 | +import type { GetTokenDetailsParams } from './types'; |
| 9 | + |
| 10 | +vi.mock('../network/request', () => ({ |
| 11 | + sendRequest: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +describe('getTokenDetails', () => { |
| 15 | + const mockSendRequest = sendRequest as Mock; |
| 16 | + |
| 17 | + const params: GetTokenDetailsParams = { |
| 18 | + contractAddress: '0x123', |
| 19 | + tokenId: '1', |
| 20 | + chainId: 1, |
| 21 | + includeFloorPrice: false, |
| 22 | + }; |
| 23 | + |
| 24 | + it('should return token details when request is successful', async () => { |
| 25 | + const mockResponse = { |
| 26 | + result: { |
| 27 | + name: 'NFT Name', |
| 28 | + description: 'NFT Description', |
| 29 | + imageUrl: 'https://nft-image-url.com', |
| 30 | + animationUrl: 'https://nft-animation-url.com', |
| 31 | + mimeType: 'image/png', |
| 32 | + ownerAddress: '0x123', |
| 33 | + lastSoldPrice: { |
| 34 | + amount: '1', |
| 35 | + currency: 'ETH', |
| 36 | + amountUsd: '2000', |
| 37 | + }, |
| 38 | + contractType: 'ERC721', |
| 39 | + }, |
| 40 | + }; |
| 41 | + |
| 42 | + mockSendRequest.mockResolvedValueOnce(mockResponse); |
| 43 | + |
| 44 | + const result = await getTokenDetails(params); |
| 45 | + |
| 46 | + expect(result).toEqual(mockResponse.result); |
| 47 | + expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_TOKEN_DETAILS, [ |
| 48 | + params, |
| 49 | + ]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return error details when request fails with an error', async () => { |
| 53 | + const mockErrorResponse = { |
| 54 | + error: { |
| 55 | + code: '404', |
| 56 | + message: 'Not Found', |
| 57 | + }, |
| 58 | + }; |
| 59 | + |
| 60 | + mockSendRequest.mockResolvedValueOnce(mockErrorResponse); |
| 61 | + |
| 62 | + const result = await getTokenDetails(params); |
| 63 | + |
| 64 | + expect(result).toEqual({ |
| 65 | + code: '404', |
| 66 | + error: 'Error fetching token details', |
| 67 | + message: 'Not Found', |
| 68 | + }); |
| 69 | + expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_TOKEN_DETAILS, [ |
| 70 | + params, |
| 71 | + ]); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should return uncaught error details when an exception is thrown', async () => { |
| 75 | + mockSendRequest.mockRejectedValue(new Error('Network Error')); |
| 76 | + |
| 77 | + const result = await getTokenDetails(params); |
| 78 | + |
| 79 | + expect(result).toEqual({ |
| 80 | + code: 'uncaught-nft', |
| 81 | + error: 'Something went wrong', |
| 82 | + message: 'Error fetching token details', |
| 83 | + }); |
| 84 | + expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [ |
| 85 | + params, |
| 86 | + ]); |
| 87 | + }); |
| 88 | +}); |
0 commit comments