ERC721Burnable.test.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const { BN, constants, expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { expectRevertCustomError } = require('../../../helpers/customError');
  4. const ERC721Burnable = artifacts.require('$ERC721Burnable');
  5. contract('ERC721Burnable', function (accounts) {
  6. const [owner, approved, another] = accounts;
  7. const firstTokenId = new BN(1);
  8. const secondTokenId = new BN(2);
  9. const unknownTokenId = new BN(3);
  10. const name = 'Non Fungible Token';
  11. const symbol = 'NFT';
  12. beforeEach(async function () {
  13. this.token = await ERC721Burnable.new(name, symbol);
  14. });
  15. describe('like a burnable ERC721', function () {
  16. beforeEach(async function () {
  17. await this.token.$_mint(owner, firstTokenId);
  18. await this.token.$_mint(owner, secondTokenId);
  19. });
  20. describe('burn', function () {
  21. const tokenId = firstTokenId;
  22. let receipt = null;
  23. describe('when successful', function () {
  24. beforeEach(async function () {
  25. receipt = await this.token.burn(tokenId, { from: owner });
  26. });
  27. it('burns the given token ID and adjusts the balance of the owner', async function () {
  28. await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
  29. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  30. });
  31. it('emits a burn event', async function () {
  32. expectEvent(receipt, 'Transfer', {
  33. from: owner,
  34. to: constants.ZERO_ADDRESS,
  35. tokenId: tokenId,
  36. });
  37. });
  38. });
  39. describe('when there is a previous approval burned', function () {
  40. beforeEach(async function () {
  41. await this.token.approve(approved, tokenId, { from: owner });
  42. receipt = await this.token.burn(tokenId, { from: owner });
  43. });
  44. context('getApproved', function () {
  45. it('reverts', async function () {
  46. await expectRevertCustomError(this.token.getApproved(tokenId), 'ERC721NonexistentToken', [tokenId]);
  47. });
  48. });
  49. });
  50. describe('when there is no previous approval burned', function () {
  51. it('reverts', async function () {
  52. await expectRevertCustomError(this.token.burn(tokenId, { from: another }), 'ERC721InsufficientApproval', [
  53. another,
  54. tokenId,
  55. ]);
  56. });
  57. });
  58. describe('when the given token ID was not tracked by this contract', function () {
  59. it('reverts', async function () {
  60. await expectRevertCustomError(this.token.burn(unknownTokenId, { from: owner }), 'ERC721NonexistentToken', [
  61. unknownTokenId,
  62. ]);
  63. });
  64. });
  65. });
  66. });
  67. });