ERC721.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const { BN, constants, expectEvent, shouldFail } = 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, anyone, ...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 shouldFail.reverting(this.token.mint(ZERO_ADDRESS, tokenId));
  15. });
  16. context('with minted token', async function () {
  17. beforeEach(async function () {
  18. ({ logs: this.logs } = await this.token.mint(tokenOwner, tokenId));
  19. });
  20. it('emits a Transfer event', function () {
  21. expectEvent.inLogs(this.logs, 'Transfer', { from: ZERO_ADDRESS, to: tokenOwner, tokenId });
  22. });
  23. it('creates the token', async function () {
  24. (await this.token.balanceOf(tokenOwner)).should.be.bignumber.equal('1');
  25. (await this.token.ownerOf(tokenId)).should.equal(tokenOwner);
  26. });
  27. it('reverts when adding a token id that already exists', async function () {
  28. await shouldFail.reverting(this.token.mint(tokenOwner, tokenId));
  29. });
  30. });
  31. });
  32. describe('_burn(address, uint256)', function () {
  33. it('reverts when burning a non-existent token id', async function () {
  34. await shouldFail.reverting(this.token.methods['burn(address,uint256)'](tokenOwner, tokenId));
  35. });
  36. context('with minted token', function () {
  37. beforeEach(async function () {
  38. await this.token.mint(tokenOwner, tokenId);
  39. });
  40. it('reverts when the account is not the owner', async function () {
  41. await shouldFail.reverting(this.token.methods['burn(address,uint256)'](anyone, tokenId));
  42. });
  43. context('with burnt token', function () {
  44. beforeEach(async function () {
  45. ({ logs: this.logs } = await this.token.methods['burn(address,uint256)'](tokenOwner, tokenId));
  46. });
  47. it('emits a Transfer event', function () {
  48. expectEvent.inLogs(this.logs, 'Transfer', { from: tokenOwner, to: ZERO_ADDRESS, tokenId });
  49. });
  50. it('deletes the token', async function () {
  51. (await this.token.balanceOf(tokenOwner)).should.be.bignumber.equal('0');
  52. await shouldFail.reverting(this.token.ownerOf(tokenId));
  53. });
  54. it('reverts when burning a token id that has been deleted', async function () {
  55. await shouldFail.reverting(this.token.methods['burn(address,uint256)'](tokenOwner, tokenId));
  56. });
  57. });
  58. });
  59. });
  60. describe('_burn(uint256)', function () {
  61. it('reverts when burning a non-existent token id', async function () {
  62. await shouldFail.reverting(this.token.methods['burn(uint256)'](tokenId));
  63. });
  64. context('with minted token', function () {
  65. beforeEach(async function () {
  66. await this.token.mint(tokenOwner, tokenId);
  67. });
  68. context('with burnt token', function () {
  69. beforeEach(async function () {
  70. ({ logs: this.logs } = await this.token.methods['burn(uint256)'](tokenId));
  71. });
  72. it('emits a Transfer event', function () {
  73. expectEvent.inLogs(this.logs, 'Transfer', { from: tokenOwner, to: ZERO_ADDRESS, tokenId });
  74. });
  75. it('deletes the token', async function () {
  76. (await this.token.balanceOf(tokenOwner)).should.be.bignumber.equal('0');
  77. await shouldFail.reverting(this.token.ownerOf(tokenId));
  78. });
  79. it('reverts when burning a token id that has been deleted', async function () {
  80. await shouldFail.reverting(this.token.methods['burn(uint256)'](tokenId));
  81. });
  82. });
  83. });
  84. });
  85. });
  86. });