ERC721URIStorage.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const { BN, expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
  4. const { expectRevertCustomError } = require('../../../helpers/customError');
  5. const ERC721URIStorageMock = artifacts.require('$ERC721URIStorageMock');
  6. contract('ERC721URIStorage', function (accounts) {
  7. const [owner] = accounts;
  8. const name = 'Non Fungible Token';
  9. const symbol = 'NFT';
  10. const firstTokenId = new BN('5042');
  11. const nonExistentTokenId = new BN('13');
  12. beforeEach(async function () {
  13. this.token = await ERC721URIStorageMock.new(name, symbol);
  14. });
  15. shouldSupportInterfaces(['0x49064906']);
  16. describe('token URI', function () {
  17. beforeEach(async function () {
  18. await this.token.$_mint(owner, firstTokenId);
  19. });
  20. const baseURI = 'https://api.example.com/v1/';
  21. const sampleUri = 'mock://mytoken';
  22. it('it is empty by default', async function () {
  23. expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
  24. });
  25. it('reverts when queried for non existent token id', async function () {
  26. await expectRevertCustomError(this.token.tokenURI(nonExistentTokenId), 'ERC721NonexistentToken', [
  27. nonExistentTokenId,
  28. ]);
  29. });
  30. it('can be set for a token id', async function () {
  31. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  32. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(sampleUri);
  33. });
  34. it('setting the uri emits an event', async function () {
  35. expectEvent(await this.token.$_setTokenURI(firstTokenId, sampleUri), 'MetadataUpdate', {
  36. _tokenId: firstTokenId,
  37. });
  38. });
  39. it('setting the uri for non existent token id is allowed', async function () {
  40. expectEvent(await this.token.$_setTokenURI(nonExistentTokenId, sampleUri), 'MetadataUpdate', {
  41. _tokenId: nonExistentTokenId,
  42. });
  43. // value will be accessible after mint
  44. await this.token.$_mint(owner, nonExistentTokenId);
  45. expect(await this.token.tokenURI(nonExistentTokenId)).to.be.equal(sampleUri);
  46. });
  47. it('base URI can be set', async function () {
  48. await this.token.setBaseURI(baseURI);
  49. expect(await this.token.$_baseURI()).to.equal(baseURI);
  50. });
  51. it('base URI is added as a prefix to the token URI', async function () {
  52. await this.token.setBaseURI(baseURI);
  53. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  54. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + sampleUri);
  55. });
  56. it('token URI can be changed by changing the base URI', async function () {
  57. await this.token.setBaseURI(baseURI);
  58. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  59. const newBaseURI = 'https://api.example.com/v2/';
  60. await this.token.setBaseURI(newBaseURI);
  61. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + sampleUri);
  62. });
  63. it('tokenId is appended to base URI for tokens with no URI', async function () {
  64. await this.token.setBaseURI(baseURI);
  65. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + firstTokenId);
  66. });
  67. it('tokens without URI can be burnt ', async function () {
  68. await this.token.$_burn(firstTokenId);
  69. await expectRevertCustomError(this.token.tokenURI(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
  70. });
  71. it('tokens with URI can be burnt ', async function () {
  72. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  73. await this.token.$_burn(firstTokenId);
  74. await expectRevertCustomError(this.token.tokenURI(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
  75. });
  76. it('tokens URI is kept if token is burnt and reminted ', async function () {
  77. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  78. await this.token.$_burn(firstTokenId);
  79. await expectRevertCustomError(this.token.tokenURI(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
  80. await this.token.$_mint(owner, firstTokenId);
  81. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(sampleUri);
  82. });
  83. });
  84. });