ERC721URIStorage.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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('reverts when setting for non existent token id', async function () {
  40. await expectRevertCustomError(this.token.$_setTokenURI(nonExistentTokenId, sampleUri), 'ERC721NonexistentToken', [
  41. nonExistentTokenId,
  42. ]);
  43. });
  44. it('base URI can be set', async function () {
  45. await this.token.setBaseURI(baseURI);
  46. expect(await this.token.$_baseURI()).to.equal(baseURI);
  47. });
  48. it('base URI is added as a prefix to the token URI', async function () {
  49. await this.token.setBaseURI(baseURI);
  50. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  51. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + sampleUri);
  52. });
  53. it('token URI can be changed by changing the base URI', async function () {
  54. await this.token.setBaseURI(baseURI);
  55. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  56. const newBaseURI = 'https://api.example.com/v2/';
  57. await this.token.setBaseURI(newBaseURI);
  58. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + sampleUri);
  59. });
  60. it('tokenId is appended to base URI for tokens with no URI', async function () {
  61. await this.token.setBaseURI(baseURI);
  62. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + firstTokenId);
  63. });
  64. it('tokens without URI can be burnt ', async function () {
  65. await this.token.$_burn(firstTokenId, { from: owner });
  66. expect(await this.token.$_exists(firstTokenId)).to.equal(false);
  67. await expectRevertCustomError(this.token.tokenURI(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
  68. });
  69. it('tokens with URI can be burnt ', async function () {
  70. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  71. await this.token.$_burn(firstTokenId, { from: owner });
  72. expect(await this.token.$_exists(firstTokenId)).to.equal(false);
  73. await expectRevertCustomError(this.token.tokenURI(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
  74. });
  75. });
  76. });