-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDK] Update to NFT Components (#5655)
CNCT-2584 - add custom resolver methods - add better test coverage - add caching for the getNFT method - small fix: handling falsy values <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on enhancing the NFT components in the `thirdweb` library by improving functionality, performance, and test coverage. It introduces custom resolver methods and caching mechanisms, along with fixes for handling falsy values. ### Detailed summary - Added custom resolver methods to `NFTMedia`, `NFTName`, and `NFTDescription`. - Implemented caching for the NFT info getter to improve performance. - Fixed handling of falsy values for NFT media `src`, `name`, and `description`. - Enhanced test coverage by extracting internal logic for testing. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
- Loading branch information
Showing
12 changed files
with
626 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"thirdweb": minor | ||
--- | ||
|
||
Improve NFT Components | ||
- Add custom resolver methods to NFTMedia, NFTName and NFTDescription | ||
- Add caching for the NFT-info-getter method to improve performance | ||
- Small fix to handle falsy values for NFT media src, name and description | ||
- Improve test coverage by extracting internal logics and testing them |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/thirdweb/src/react/web/ui/prebuilt/NFT/description.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { | ||
DOODLES_CONTRACT, | ||
DROP1155_CONTRACT, | ||
UNISWAPV3_FACTORY_CONTRACT, | ||
} from "~test/test-contracts.js"; | ||
import { fetchNftDescription } from "./description.js"; | ||
|
||
describe.runIf(process.env.TW_SECRET_KEY)("NFTDescription", () => { | ||
it("fetchNftDescription should work with ERC721", async () => { | ||
const desc = await fetchNftDescription({ | ||
contract: DOODLES_CONTRACT, | ||
tokenId: 0n, | ||
}); | ||
expect(desc).toBe( | ||
"A community-driven collectibles project featuring art by Burnt Toast. Doodles come in a joyful range of colors, traits and sizes with a collection size of 10,000. Each Doodle allows its owner to vote for experiences and activations paid for by the Doodles Community Treasury. Burnt Toast is the working alias for Scott Martin, a Canadian–based illustrator, designer, animator and muralist.", | ||
); | ||
}); | ||
|
||
it("fetchNftDescription should work with ERC1155", async () => { | ||
const desc = await fetchNftDescription({ | ||
contract: DROP1155_CONTRACT, | ||
tokenId: 0n, | ||
}); | ||
expect(desc).toBe(""); | ||
}); | ||
|
||
it("fetchNftDescription should respect descriptionResolver as a string", async () => { | ||
const desc = await fetchNftDescription({ | ||
contract: DOODLES_CONTRACT, | ||
tokenId: 0n, | ||
descriptionResolver: "string", | ||
}); | ||
expect(desc).toBe("string"); | ||
}); | ||
|
||
it("fetchNftDescription should respect descriptionResolver as a non-async function", async () => { | ||
const desc = await fetchNftDescription({ | ||
contract: DOODLES_CONTRACT, | ||
tokenId: 0n, | ||
descriptionResolver: () => "non-async", | ||
}); | ||
expect(desc).toBe("non-async"); | ||
}); | ||
|
||
it("fetchNftDescription should respect descriptionResolver as a async function", async () => { | ||
const desc = await fetchNftDescription({ | ||
contract: DOODLES_CONTRACT, | ||
tokenId: 0n, | ||
descriptionResolver: async () => "async", | ||
}); | ||
expect(desc).toBe("async"); | ||
}); | ||
|
||
it("fetchNftDescription should throw error if failed to resolve nft info", async () => { | ||
await expect(() => | ||
fetchNftDescription({ | ||
contract: UNISWAPV3_FACTORY_CONTRACT, | ||
tokenId: 0n, | ||
}), | ||
).rejects.toThrowError("Failed to resolve NFT info"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
packages/thirdweb/src/react/web/ui/prebuilt/NFT/media.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { | ||
DOODLES_CONTRACT, | ||
DROP1155_CONTRACT, | ||
UNISWAPV3_FACTORY_CONTRACT, | ||
} from "~test/test-contracts.js"; | ||
import { fetchNftMedia } from "./media.js"; | ||
|
||
describe.runIf(process.env.TW_SECRET_KEY)("NFTMedia", () => { | ||
it("fetchNftMedia should work with ERC721", async () => { | ||
const desc = await fetchNftMedia({ | ||
contract: DOODLES_CONTRACT, | ||
tokenId: 0n, | ||
}); | ||
expect(desc).toStrictEqual({ | ||
src: "ipfs://QmUEfFfwAh4wyB5UfHCVPUxis4j4Q4kJXtm5x5p3g1fVUn", | ||
poster: undefined, | ||
}); | ||
}); | ||
|
||
it("fetchNftMedia should work with ERC1155", async () => { | ||
const desc = await fetchNftMedia({ | ||
contract: DROP1155_CONTRACT, | ||
tokenId: 0n, | ||
}); | ||
expect(desc).toStrictEqual({ | ||
src: "ipfs://QmeGCqV1mSHTZrvuFzW1XZdCRRGXB6AmSotTqHoxA2xfDo/1.mp4", | ||
poster: "ipfs://QmeGCqV1mSHTZrvuFzW1XZdCRRGXB6AmSotTqHoxA2xfDo/0.png", | ||
}); | ||
}); | ||
|
||
it("fetchNftMedia should respect mediaResolver as a string", async () => { | ||
const desc = await fetchNftMedia({ | ||
contract: DOODLES_CONTRACT, | ||
tokenId: 0n, | ||
mediaResolver: { | ||
src: "string", | ||
poster: undefined, | ||
}, | ||
}); | ||
expect(desc).toStrictEqual({ src: "string", poster: undefined }); | ||
}); | ||
|
||
it("fetchNftMedia should respect mediaResolver as a non-async function", async () => { | ||
const desc = await fetchNftMedia({ | ||
contract: DOODLES_CONTRACT, | ||
tokenId: 0n, | ||
mediaResolver: () => ({ | ||
src: "non-async", | ||
poster: undefined, | ||
}), | ||
}); | ||
expect(desc).toStrictEqual({ src: "non-async", poster: undefined }); | ||
}); | ||
|
||
it("fetchNftMedia should respect mediaResolver as a async function", async () => { | ||
const desc = await fetchNftMedia({ | ||
contract: DOODLES_CONTRACT, | ||
tokenId: 0n, | ||
mediaResolver: async () => | ||
await { | ||
src: "async", | ||
poster: undefined, | ||
}, | ||
}); | ||
expect(desc).toStrictEqual({ src: "async", poster: undefined }); | ||
}); | ||
|
||
it("fetchNftMedia should throw error if failed to resolve nft info", async () => { | ||
await expect(() => | ||
fetchNftMedia({ | ||
contract: UNISWAPV3_FACTORY_CONTRACT, | ||
tokenId: 0n, | ||
}), | ||
).rejects.toThrowError("Failed to resolve NFT info"); | ||
}); | ||
}); |
Oops, something went wrong.