ERC721URIStorage.test.js 3.4 KB

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