ERC721.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. require('../../helpers/setup');
  2. const { ZERO_ADDRESS } = require('../../helpers/constants');
  3. const expectEvent = require('../../helpers/expectEvent');
  4. const send = require('../../helpers/send');
  5. const shouldFail = require('../../helpers/shouldFail');
  6. const { shouldBehaveLikeERC721 } = require('./ERC721.behavior');
  7. const ERC721Mock = artifacts.require('ERC721Mock.sol');
  8. contract('ERC721', function ([_, creator, tokenOwner, anyone, ...accounts]) {
  9. beforeEach(async function () {
  10. this.token = await ERC721Mock.new({ from: creator });
  11. });
  12. shouldBehaveLikeERC721(creator, creator, accounts);
  13. describe('internal functions', function () {
  14. const tokenId = 5042;
  15. describe('_mint(address, uint256)', function () {
  16. it('reverts with a null destination address', async function () {
  17. await shouldFail.reverting(this.token.mint(ZERO_ADDRESS, tokenId));
  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. (await this.token.balanceOf(tokenOwner)).should.be.bignumber.equal(1);
  28. (await this.token.ownerOf(tokenId)).should.equal(tokenOwner);
  29. });
  30. it('reverts when adding a token id that already exists', async function () {
  31. await shouldFail.reverting(this.token.mint(tokenOwner, tokenId));
  32. });
  33. });
  34. });
  35. describe('_burn(address, uint256)', function () {
  36. it('reverts when burning a non-existent token id', async function () {
  37. await shouldFail.reverting(send.transaction(this.token, 'burn', 'address,uint256', [tokenOwner, tokenId]));
  38. });
  39. context('with minted token', function () {
  40. beforeEach(async function () {
  41. await this.token.mint(tokenOwner, tokenId);
  42. });
  43. it('reverts when the account is not the owner', async function () {
  44. await shouldFail.reverting(send.transaction(this.token, 'burn', 'address,uint256', [anyone, tokenId]));
  45. });
  46. context('with burnt token', function () {
  47. beforeEach(async function () {
  48. ({ logs: this.logs } =
  49. await send.transaction(this.token, 'burn', 'address,uint256', [tokenOwner, tokenId]));
  50. });
  51. it('emits a Transfer event', function () {
  52. expectEvent.inLogs(this.logs, 'Transfer', { from: tokenOwner, to: ZERO_ADDRESS, tokenId });
  53. });
  54. it('deletes the token', async function () {
  55. (await this.token.balanceOf(tokenOwner)).should.be.bignumber.equal(0);
  56. await shouldFail.reverting(this.token.ownerOf(tokenId));
  57. });
  58. it('reverts when burning a token id that has been deleted', async function () {
  59. await shouldFail.reverting(send.transaction(this.token, 'burn', 'address,uint256', [tokenOwner, tokenId]));
  60. });
  61. });
  62. });
  63. });
  64. describe('_burn(uint256)', function () {
  65. it('reverts when burning a non-existent token id', async function () {
  66. await shouldFail.reverting(send.transaction(this.token, 'burn', 'uint256', [tokenId]));
  67. });
  68. context('with minted token', function () {
  69. beforeEach(async function () {
  70. await this.token.mint(tokenOwner, tokenId);
  71. });
  72. context('with burnt token', function () {
  73. beforeEach(async function () {
  74. ({ logs: this.logs } = await send.transaction(this.token, 'burn', 'uint256', [tokenId]));
  75. });
  76. it('emits a Transfer event', function () {
  77. expectEvent.inLogs(this.logs, 'Transfer', { from: tokenOwner, to: ZERO_ADDRESS, tokenId });
  78. });
  79. it('deletes the token', async function () {
  80. (await this.token.balanceOf(tokenOwner)).should.be.bignumber.equal(0);
  81. await shouldFail.reverting(this.token.ownerOf(tokenId));
  82. });
  83. it('reverts when burning a token id that has been deleted', async function () {
  84. await shouldFail.reverting(send.transaction(this.token, 'burn', 'uint256', [tokenId]));
  85. });
  86. });
  87. });
  88. });
  89. });
  90. });