ERC721.test.js 4.6 KB

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