ERC721.test.js 4.6 KB

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