Pausable.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. async function fixture() {
  5. const [pauser] = await ethers.getSigners();
  6. const mock = await ethers.deployContract('PausableMock');
  7. return { pauser, mock };
  8. }
  9. describe('Pausable', function () {
  10. beforeEach(async function () {
  11. Object.assign(this, await loadFixture(fixture));
  12. });
  13. describe('when unpaused', function () {
  14. beforeEach(async function () {
  15. expect(await this.mock.paused()).to.be.false;
  16. });
  17. it('can perform normal process in non-pause', async function () {
  18. expect(await this.mock.count()).to.equal(0n);
  19. await this.mock.normalProcess();
  20. expect(await this.mock.count()).to.equal(1n);
  21. });
  22. it('cannot take drastic measure in non-pause', async function () {
  23. await expect(this.mock.drasticMeasure()).to.be.revertedWithCustomError(this.mock, 'ExpectedPause');
  24. expect(await this.mock.drasticMeasureTaken()).to.be.false;
  25. });
  26. describe('when paused', function () {
  27. beforeEach(async function () {
  28. this.tx = await this.mock.pause();
  29. });
  30. it('emits a Paused event', async function () {
  31. await expect(this.tx).to.emit(this.mock, 'Paused').withArgs(this.pauser);
  32. });
  33. it('cannot perform normal process in pause', async function () {
  34. await expect(this.mock.normalProcess()).to.be.revertedWithCustomError(this.mock, 'EnforcedPause');
  35. });
  36. it('can take a drastic measure in a pause', async function () {
  37. await this.mock.drasticMeasure();
  38. expect(await this.mock.drasticMeasureTaken()).to.be.true;
  39. });
  40. it('reverts when re-pausing', async function () {
  41. await expect(this.mock.pause()).to.be.revertedWithCustomError(this.mock, 'EnforcedPause');
  42. });
  43. describe('unpausing', function () {
  44. it('is unpausable by the pauser', async function () {
  45. await this.mock.unpause();
  46. expect(await this.mock.paused()).to.be.false;
  47. });
  48. describe('when unpaused', function () {
  49. beforeEach(async function () {
  50. this.tx = await this.mock.unpause();
  51. });
  52. it('emits an Unpaused event', async function () {
  53. await expect(this.tx).to.emit(this.mock, 'Unpaused').withArgs(this.pauser);
  54. });
  55. it('should resume allowing normal process', async function () {
  56. expect(await this.mock.count()).to.equal(0n);
  57. await this.mock.normalProcess();
  58. expect(await this.mock.count()).to.equal(1n);
  59. });
  60. it('should prevent drastic measure', async function () {
  61. await expect(this.mock.drasticMeasure()).to.be.revertedWithCustomError(this.mock, 'ExpectedPause');
  62. });
  63. it('reverts when re-unpausing', async function () {
  64. await expect(this.mock.unpause()).to.be.revertedWithCustomError(this.mock, 'ExpectedPause');
  65. });
  66. });
  67. });
  68. });
  69. });
  70. });