Pausable.test.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const PausableMock = artifacts.require('PausableMock');
  4. contract('Pausable', function (accounts) {
  5. const [pauser] = accounts;
  6. beforeEach(async function () {
  7. this.pausable = await PausableMock.new();
  8. });
  9. context('when unpaused', function () {
  10. beforeEach(async function () {
  11. expect(await this.pausable.paused()).to.equal(false);
  12. });
  13. it('can perform normal process in non-pause', async function () {
  14. expect(await this.pausable.count()).to.be.bignumber.equal('0');
  15. await this.pausable.normalProcess();
  16. expect(await this.pausable.count()).to.be.bignumber.equal('1');
  17. });
  18. it('cannot take drastic measure in non-pause', async function () {
  19. await expectRevert(this.pausable.drasticMeasure(), 'Pausable: not paused');
  20. expect(await this.pausable.drasticMeasureTaken()).to.equal(false);
  21. });
  22. context('when paused', function () {
  23. beforeEach(async function () {
  24. this.receipt = await this.pausable.pause({ from: pauser });
  25. });
  26. it('emits a Paused event', function () {
  27. expectEvent(this.receipt, 'Paused', { account: pauser });
  28. });
  29. it('cannot perform normal process in pause', async function () {
  30. await expectRevert(this.pausable.normalProcess(), 'Pausable: paused');
  31. });
  32. it('can take a drastic measure in a pause', async function () {
  33. await this.pausable.drasticMeasure();
  34. expect(await this.pausable.drasticMeasureTaken()).to.equal(true);
  35. });
  36. it('reverts when re-pausing', async function () {
  37. await expectRevert(this.pausable.pause(), 'Pausable: paused');
  38. });
  39. describe('unpausing', function () {
  40. it('is unpausable by the pauser', async function () {
  41. await this.pausable.unpause();
  42. expect(await this.pausable.paused()).to.equal(false);
  43. });
  44. context('when unpaused', function () {
  45. beforeEach(async function () {
  46. this.receipt = await this.pausable.unpause({ from: pauser });
  47. });
  48. it('emits an Unpaused event', function () {
  49. expectEvent(this.receipt, 'Unpaused', { account: pauser });
  50. });
  51. it('should resume allowing normal process', async function () {
  52. expect(await this.pausable.count()).to.be.bignumber.equal('0');
  53. await this.pausable.normalProcess();
  54. expect(await this.pausable.count()).to.be.bignumber.equal('1');
  55. });
  56. it('should prevent drastic measure', async function () {
  57. await expectRevert(this.pausable.drasticMeasure(), 'Pausable: not paused');
  58. });
  59. it('reverts when re-unpausing', async function () {
  60. await expectRevert(this.pausable.unpause(), 'Pausable: not paused');
  61. });
  62. });
  63. });
  64. });
  65. });
  66. });