ERC721.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 [ owner ] = accounts;
  9. beforeEach(async function () {
  10. this.token = await ERC721Mock.new();
  11. });
  12. shouldBehaveLikeERC721(accounts);
  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', function () {
  38. it('reverts when burning a non-existent token id', async function () {
  39. await expectRevert(
  40. this.token.burn(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. context('with burnt token', function () {
  48. beforeEach(async function () {
  49. ({ logs: this.logs } = await this.token.burn(tokenId));
  50. });
  51. it('emits a Transfer event', function () {
  52. expectEvent.inLogs(this.logs, 'Transfer', { from: owner, to: ZERO_ADDRESS, tokenId });
  53. });
  54. it('deletes the token', async function () {
  55. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('0');
  56. await expectRevert(
  57. this.token.ownerOf(tokenId), 'ERC721: owner query for nonexistent token'
  58. );
  59. });
  60. it('reverts when burning a token id that has been deleted', async function () {
  61. await expectRevert(
  62. this.token.burn(tokenId), 'ERC721: owner query for nonexistent token'
  63. );
  64. });
  65. });
  66. });
  67. });
  68. });
  69. });