ERC721URIStorage.test.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const { BN, 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('reverts when setting for non existent token id', async function () {
  30. await expectRevert(
  31. this.token.$_setTokenURI(nonExistentTokenId, sampleUri),
  32. 'ERC721URIStorage: URI set of nonexistent token',
  33. );
  34. });
  35. it('base URI can be set', async function () {
  36. await this.token.setBaseURI(baseURI);
  37. expect(await this.token.$_baseURI()).to.equal(baseURI);
  38. });
  39. it('base URI is added as a prefix to the token URI', async function () {
  40. await this.token.setBaseURI(baseURI);
  41. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  42. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + sampleUri);
  43. });
  44. it('token URI can be changed by changing the base URI', async function () {
  45. await this.token.setBaseURI(baseURI);
  46. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  47. const newBaseURI = 'https://api.example.com/v2/';
  48. await this.token.setBaseURI(newBaseURI);
  49. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + sampleUri);
  50. });
  51. it('tokenId is appended to base URI for tokens with no URI', async function () {
  52. await this.token.setBaseURI(baseURI);
  53. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + firstTokenId);
  54. });
  55. it('tokens without URI can be burnt ', async function () {
  56. await this.token.$_burn(firstTokenId, { from: owner });
  57. expect(await this.token.$_exists(firstTokenId)).to.equal(false);
  58. await expectRevert(this.token.tokenURI(firstTokenId), 'ERC721: invalid token ID');
  59. });
  60. it('tokens with URI can be burnt ', async function () {
  61. await this.token.$_setTokenURI(firstTokenId, sampleUri);
  62. await this.token.$_burn(firstTokenId, { from: owner });
  63. expect(await this.token.$_exists(firstTokenId)).to.equal(false);
  64. await expectRevert(this.token.tokenURI(firstTokenId), 'ERC721: invalid token ID');
  65. });
  66. });
  67. });