ERC20Pausable.test.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { BN, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const ERC20PausableMock = contract.fromArtifact('ERC20PausableMock');
  5. describe('ERC20Pausable', function () {
  6. const [ holder, recipient, anotherAccount ] = accounts;
  7. const initialSupply = new BN(100);
  8. const name = 'My Token';
  9. const symbol = 'MTKN';
  10. beforeEach(async function () {
  11. this.token = await ERC20PausableMock.new(name, symbol, holder, initialSupply);
  12. });
  13. describe('pausable token', function () {
  14. describe('transfer', function () {
  15. it('allows to transfer when unpaused', async function () {
  16. await this.token.transfer(recipient, initialSupply, { from: holder });
  17. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
  18. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(initialSupply);
  19. });
  20. it('allows to transfer when paused and then unpaused', async function () {
  21. await this.token.pause();
  22. await this.token.unpause();
  23. await this.token.transfer(recipient, initialSupply, { from: holder });
  24. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
  25. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(initialSupply);
  26. });
  27. it('reverts when trying to transfer when paused', async function () {
  28. await this.token.pause();
  29. await expectRevert(this.token.transfer(recipient, initialSupply, { from: holder }),
  30. 'ERC20Pausable: token transfer while paused'
  31. );
  32. });
  33. });
  34. describe('transfer from', function () {
  35. const allowance = new BN(40);
  36. beforeEach(async function () {
  37. await this.token.approve(anotherAccount, allowance, { from: holder });
  38. });
  39. it('allows to transfer from when unpaused', async function () {
  40. await this.token.transferFrom(holder, recipient, allowance, { from: anotherAccount });
  41. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(allowance);
  42. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(allowance));
  43. });
  44. it('allows to transfer when paused and then unpaused', async function () {
  45. await this.token.pause();
  46. await this.token.unpause();
  47. await this.token.transferFrom(holder, recipient, allowance, { from: anotherAccount });
  48. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(allowance);
  49. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(allowance));
  50. });
  51. it('reverts when trying to transfer from when paused', async function () {
  52. await this.token.pause();
  53. await expectRevert(this.token.transferFrom(
  54. holder, recipient, allowance, { from: anotherAccount }), 'ERC20Pausable: token transfer while paused'
  55. );
  56. });
  57. });
  58. });
  59. });