ERC721Burnable.test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const ERC721BurnableMock = artifacts.require('ERC721BurnableMock');
  5. contract('ERC721Burnable', function (accounts) {
  6. const [owner, approved] = 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 ERC721BurnableMock.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 logs = null;
  23. describe('when successful', function () {
  24. beforeEach(async function () {
  25. const result = await this.token.burn(tokenId, { from: owner });
  26. logs = result.logs;
  27. });
  28. it('burns the given token ID and adjusts the balance of the owner', async function () {
  29. await expectRevert(
  30. this.token.ownerOf(tokenId),
  31. 'ERC721: owner query for nonexistent token',
  32. );
  33. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  34. });
  35. it('emits a burn event', async function () {
  36. expectEvent.inLogs(logs, 'Transfer', {
  37. from: owner,
  38. to: ZERO_ADDRESS,
  39. tokenId: tokenId,
  40. });
  41. });
  42. });
  43. describe('when there is a previous approval burned', function () {
  44. beforeEach(async function () {
  45. await this.token.approve(approved, tokenId, { from: owner });
  46. const result = await this.token.burn(tokenId, { from: owner });
  47. logs = result.logs;
  48. });
  49. context('getApproved', function () {
  50. it('reverts', async function () {
  51. await expectRevert(
  52. this.token.getApproved(tokenId), 'ERC721: approved query for nonexistent token',
  53. );
  54. });
  55. });
  56. });
  57. describe('when the given token ID was not tracked by this contract', function () {
  58. it('reverts', async function () {
  59. await expectRevert(
  60. this.token.burn(unknownTokenId, { from: owner }), 'ERC721: operator query for nonexistent token',
  61. );
  62. });
  63. });
  64. });
  65. });
  66. });