ERC721Burnable.test.js 2.3 KB

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