ERC721Burnable.test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const name = 'Non Fungible Token';
  5. const symbol = 'NFT';
  6. const tokenId = 1n;
  7. const otherTokenId = 2n;
  8. const unknownTokenId = 3n;
  9. async function fixture() {
  10. const [owner, approved, another] = await ethers.getSigners();
  11. const token = await ethers.deployContract('$ERC721Burnable', [name, symbol]);
  12. return { owner, approved, another, token };
  13. }
  14. describe('ERC721Burnable', function () {
  15. beforeEach(async function () {
  16. Object.assign(this, await loadFixture(fixture));
  17. });
  18. describe('like a burnable ERC721', function () {
  19. beforeEach(async function () {
  20. await this.token.$_mint(this.owner, tokenId);
  21. await this.token.$_mint(this.owner, otherTokenId);
  22. });
  23. describe('burn', function () {
  24. describe('when successful', function () {
  25. it('emits a burn event, burns the given token ID and adjusts the balance of the owner', async function () {
  26. const balanceBefore = await this.token.balanceOf(this.owner);
  27. await expect(this.token.connect(this.owner).burn(tokenId))
  28. .to.emit(this.token, 'Transfer')
  29. .withArgs(this.owner, ethers.ZeroAddress, tokenId);
  30. await expect(this.token.ownerOf(tokenId))
  31. .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
  32. .withArgs(tokenId);
  33. expect(await this.token.balanceOf(this.owner)).to.equal(balanceBefore - 1n);
  34. });
  35. });
  36. describe('when there is a previous approval burned', function () {
  37. beforeEach(async function () {
  38. await this.token.connect(this.owner).approve(this.approved, tokenId);
  39. await this.token.connect(this.owner).burn(tokenId);
  40. });
  41. describe('getApproved', function () {
  42. it('reverts', async function () {
  43. await expect(this.token.getApproved(tokenId))
  44. .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
  45. .withArgs(tokenId);
  46. });
  47. });
  48. });
  49. describe('when there is no previous approval burned', function () {
  50. it('reverts', async function () {
  51. await expect(this.token.connect(this.another).burn(tokenId))
  52. .to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
  53. .withArgs(this.another, tokenId);
  54. });
  55. });
  56. describe('when the given token ID was not tracked by this contract', function () {
  57. it('reverts', async function () {
  58. await expect(this.token.connect(this.owner).burn(unknownTokenId))
  59. .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
  60. .withArgs(unknownTokenId);
  61. });
  62. });
  63. });
  64. });
  65. });