1- import { renderHook } from " @testing-library/react-hooks" ;
1+ import { renderHook } from ' @testing-library/react-hooks' ;
22
3- import fetchMock from " ./setupFetchMock" ;
3+ import fetchMock from ' ./setupFetchMock' ;
44
5- import { useNFTMetadata } from " ../src" ;
6- import { defaultFetchAgent } from " ../src/context/NFTFetchContext" ;
5+ import { useNFTMetadata } from ' ../src' ;
6+ import { defaultFetchAgent } from ' ../src/context/NFTFetchContext' ;
77
8- describe ( " useNFTContent" , ( ) => {
8+ describe ( ' useNFTContent' , ( ) => {
99 afterEach ( ( ) => {
1010 defaultFetchAgent . clearCache ( ) ;
1111 fetchMock . reset ( ) ;
1212 } ) ;
1313
14- it ( "loads text content for NFT from server" , async ( ) => {
15- fetchMock . get (
16- "https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE" ,
17- { name : "test" , description : "test" } ,
18- ) ;
14+ it ( 'loads text content for NFT from server' , async ( ) => {
15+ fetchMock . get ( 'https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE' , {
16+ name : 'test' ,
17+ description : 'test' ,
18+ mimeType : 'text/plain' ,
19+ version : 'zora-20210101' ,
20+ } ) ;
1921
2022 const { waitFor, result } = renderHook ( ( ) =>
21- useNFTMetadata ( " https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE" )
23+ useNFTMetadata ( ' https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE' )
2224 ) ;
2325
2426 await waitFor ( ( ) => result . current . loading === false ) ;
@@ -27,37 +29,72 @@ describe("useNFTContent", () => {
2729 expect ( result . current . loading ) . toBeFalsy ( ) ;
2830 expect ( result . current . metadata ) . toEqual ( {
2931 description : 'test' ,
32+ mimeType : 'text/plain' ,
33+ version : 'zora-20210101' ,
34+ name : 'test' ,
35+ } ) ;
36+ } ) ;
37+
38+ it ( 'throws an error for invalid metadata' , async ( ) => {
39+ fetchMock . get ( 'https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE' , { name : 'test' } ) ;
40+
41+ const { waitFor, result } = renderHook ( ( ) =>
42+ useNFTMetadata ( 'https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE' )
43+ ) ;
44+
45+ await waitFor ( ( ) => result . current . loading === false ) ;
46+
47+ expect ( result . current . error ) . toEqual (
48+ 'RequestError: Metadata Invalid: Metadata could not be validated.'
49+ ) ;
50+ expect ( result . current . loading ) . toBeFalsy ( ) ;
51+ expect ( result . current . metadata ) . toBeUndefined ( ) ;
52+ } ) ;
53+
54+ it ( 'allows invalid metadata when validation exception disabled' , async ( ) => {
55+ fetchMock . get ( 'https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE' , { name : 'test' } ) ;
56+
57+ const { waitFor, result } = renderHook ( ( ) =>
58+ useNFTMetadata ( 'https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE' , { allowInvalid : true } )
59+ ) ;
60+
61+ await waitFor ( ( ) => result . current . loading === false ) ;
62+
63+ expect ( result . current . error ) . toBeUndefined ( ) ;
64+ expect ( result . current . loading ) . toBeFalsy ( ) ;
65+ expect ( result . current . metadata ) . toEqual ( {
3066 name : 'test' ,
3167 } ) ;
3268 } ) ;
33- it ( "returns error when metadata does not exist" , async ( ) => {
34- fetchMock . get ( "https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE" , "Not Found" , {
69+
70+ it ( 'returns error when metadata does not exist' , async ( ) => {
71+ fetchMock . get ( 'https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE' , 'Not Found' , {
3572 response : { status : 404 } ,
3673 } ) ;
3774
3875 const { waitFor, result } = renderHook ( ( ) =>
39- useNFTMetadata ( " https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE" )
76+ useNFTMetadata ( ' https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE' )
4077 ) ;
4178
4279 await waitFor ( ( ) => result . current . loading === false ) ;
4380
44- expect ( result . current . error ) . toEqual ( " RequestError: Request Status = 404" ) ;
81+ expect ( result . current . error ) . toEqual ( ' RequestError: Request Status = 404' ) ;
4582 expect ( result . current . loading ) . toBeFalsy ( ) ;
4683 expect ( result . current . metadata ) . toBeUndefined ( ) ;
4784 } ) ;
48- it ( " throws exception for invalid json" , async ( ) => {
49- fetchMock . get ( " https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE" , " INVALID JSON" , {
50- response : { headers : { " content-type" : " application/json" } } ,
85+ it ( ' throws exception for invalid json' , async ( ) => {
86+ fetchMock . get ( ' https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE' , ' INVALID JSON' , {
87+ response : { headers : { ' content-type' : ' application/json' } } ,
5188 } ) ;
5289
5390 const { waitFor, result } = renderHook ( ( ) =>
54- useNFTMetadata ( " https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE" )
91+ useNFTMetadata ( ' https://ipfs.io/ipfs/IPFS_SHA_EXAMPLE' )
5592 ) ;
5693
5794 await waitFor ( ( ) => result . current . loading === false ) ;
5895
5996 expect ( result . current . error ) . toEqual (
60- " RequestError: Cannot read JSON metadata from IPFS"
97+ ' RequestError: Cannot read JSON metadata from IPFS'
6198 ) ;
6299 expect ( result . current . loading ) . toBeFalsy ( ) ;
63100 expect ( result . current . metadata ) . toBeUndefined ( ) ;
0 commit comments