ERC721.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const { BN, constants, expectEvent, expectRevert } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const { shouldBehaveLikeERC721 } = require('./ERC721.behavior');
  5. const ERC721Mock = artifacts.require('ERC721Mock.sol');
  6. contract('ERC721', function ([_, creator, tokenOwner, other, ...accounts]) {
  7. beforeEach(async function () {
  8. this.token = await ERC721Mock.new({ from: creator });
  9. });
  10. shouldBehaveLikeERC721(creator, creator, accounts);
  11. describe('internal functions', function () {
  12. const tokenId = new BN('5042');
  13. describe('_mint(address, uint256)', function () {
  14. it('reverts with a null destination address', async function () {
  15. await expectRevert(
  16. this.token.mint(ZERO_ADDRESS, tokenId), 'ERC721: mint to the zero address'
  17. );
  18. });
  19. context('with minted token', async function () {
  20. beforeEach(async function () {
  21. ({ logs: this.logs } = await this.token.mint(tokenOwner, tokenId));
  22. });
  23. it('emits a Transfer event', function () {
  24. expectEvent.inLogs(this.logs, 'Transfer', { from: ZERO_ADDRESS, to: tokenOwner, tokenId });
  25. });
  26. it('creates the token', async function () {
  27. expect(await this.token.balanceOf(tokenOwner)).to.be.bignumber.equal('1');
  28. expect(await this.token.ownerOf(tokenId)).to.equal(tokenOwner);
  29. });
  30. it('reverts when adding a token id that already exists', async function () {
  31. await expectRevert(this.token.mint(tokenOwner, tokenId), 'ERC721: token already minted');
  32. });
  33. });
  34. });
  35. describe('_burn(address, uint256)', function () {
  36. it('reverts when burning a non-existent token id', async function () {
  37. await expectRevert(
  38. this.token.methods['burn(address,uint256)'](tokenOwner, tokenId), 'ERC721: owner query for nonexistent token'
  39. );
  40. });
  41. context('with minted token', function () {
  42. beforeEach(async function () {
  43. await this.token.mint(tokenOwner, tokenId);
  44. });
  45. it('reverts when the account is not the owner', async function () {
  46. await expectRevert(
  47. this.token.methods['burn(address,uint256)'](other, tokenId), 'ERC721: burn of token that is not own'
  48. );
  49. });
  50. context('with burnt token', function () {
  51. beforeEach(async function () {
  52. ({ logs: this.logs } = await this.token.methods['burn(address,uint256)'](tokenOwner, tokenId));
  53. });
  54. it('emits a Transfer event', function () {
  55. expectEvent.inLogs(this.logs, 'Transfer', { from: tokenOwner, to: ZERO_ADDRESS, tokenId });
  56. });
  57. it('deletes the token', async function () {
  58. expect(await this.token.balanceOf(tokenOwner)).to.be.bignumber.equal('0');
  59. await expectRevert(
  60. this.token.ownerOf(tokenId), 'ERC721: owner query for nonexistent token'
  61. );
  62. });
  63. it('reverts when burning a token id that has been deleted', async function () {
  64. await expectRevert(
  65. this.token.methods['burn(address,uint256)'](tokenOwner, tokenId),
  66. 'ERC721: owner query for nonexistent token'
  67. );
  68. });
  69. });
  70. });
  71. });
  72. describe('_burn(uint256)', function () {
  73. it('reverts when burning a non-existent token id', async function () {
  74. await expectRevert(
  75. this.token.methods['burn(uint256)'](tokenId), 'ERC721: owner query for nonexistent token'
  76. );
  77. });
  78. context('with minted token', function () {
  79. beforeEach(async function () {
  80. await this.token.mint(tokenOwner, tokenId);
  81. });
  82. context('with burnt token', function () {
  83. beforeEach(async function () {
  84. ({ logs: this.logs } = await this.token.methods['burn(uint256)'](tokenId));
  85. });
  86. it('emits a Transfer event', function () {
  87. expectEvent.inLogs(this.logs, 'Transfer', { from: tokenOwner, to: ZERO_ADDRESS, tokenId });
  88. });
  89. it('deletes the token', async function () {
  90. expect(await this.token.balanceOf(tokenOwner)).to.be.bignumber.equal('0');
  91. await expectRevert(
  92. this.token.ownerOf(tokenId), 'ERC721: owner query for nonexistent token'
  93. );
  94. });
  95. it('reverts when burning a token id that has been deleted', async function () {
  96. await expectRevert(
  97. this.token.methods['burn(uint256)'](tokenId), 'ERC721: owner query for nonexistent token'
  98. );
  99. });
  100. });
  101. });
  102. });
  103. });
  104. });