Initializable.test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { MAX_UINT64 } = require('../../helpers/constants');
  4. describe('Initializable', function () {
  5. describe('basic testing without inheritance', function () {
  6. beforeEach('deploying', async function () {
  7. this.mock = await ethers.deployContract('InitializableMock');
  8. });
  9. describe('before initialize', function () {
  10. it('initializer has not run', async function () {
  11. expect(await this.mock.initializerRan()).to.be.false;
  12. });
  13. it('_initializing returns false before initialization', async function () {
  14. expect(await this.mock.isInitializing()).to.be.false;
  15. });
  16. });
  17. describe('after initialize', function () {
  18. beforeEach('initializing', async function () {
  19. await this.mock.initialize();
  20. });
  21. it('initializer has run', async function () {
  22. expect(await this.mock.initializerRan()).to.be.true;
  23. });
  24. it('_initializing returns false after initialization', async function () {
  25. expect(await this.mock.isInitializing()).to.be.false;
  26. });
  27. it('initializer does not run again', async function () {
  28. await expect(this.mock.initialize()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
  29. });
  30. });
  31. describe('nested under an initializer', function () {
  32. it('initializer modifier reverts', async function () {
  33. await expect(this.mock.initializerNested()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
  34. });
  35. it('onlyInitializing modifier succeeds', async function () {
  36. await this.mock.onlyInitializingNested();
  37. expect(await this.mock.onlyInitializingRan()).to.be.true;
  38. });
  39. });
  40. it('cannot call onlyInitializable function outside the scope of an initializable function', async function () {
  41. await expect(this.mock.initializeOnlyInitializing()).to.be.revertedWithCustomError(this.mock, 'NotInitializing');
  42. });
  43. });
  44. it('nested initializer can run during construction', async function () {
  45. const mock = await ethers.deployContract('ConstructorInitializableMock');
  46. expect(await mock.initializerRan()).to.be.true;
  47. expect(await mock.onlyInitializingRan()).to.be.true;
  48. });
  49. it('multiple constructor levels can be initializers', async function () {
  50. const mock = await ethers.deployContract('ChildConstructorInitializableMock');
  51. expect(await mock.initializerRan()).to.be.true;
  52. expect(await mock.childInitializerRan()).to.be.true;
  53. expect(await mock.onlyInitializingRan()).to.be.true;
  54. });
  55. describe('reinitialization', function () {
  56. beforeEach('deploying', async function () {
  57. this.mock = await ethers.deployContract('ReinitializerMock');
  58. });
  59. it('can reinitialize', async function () {
  60. expect(await this.mock.counter()).to.equal(0n);
  61. await this.mock.initialize();
  62. expect(await this.mock.counter()).to.equal(1n);
  63. await this.mock.reinitialize(2);
  64. expect(await this.mock.counter()).to.equal(2n);
  65. await this.mock.reinitialize(3);
  66. expect(await this.mock.counter()).to.equal(3n);
  67. });
  68. it('can jump multiple steps', async function () {
  69. expect(await this.mock.counter()).to.equal(0n);
  70. await this.mock.initialize();
  71. expect(await this.mock.counter()).to.equal(1n);
  72. await this.mock.reinitialize(128);
  73. expect(await this.mock.counter()).to.equal(2n);
  74. });
  75. it('cannot nest reinitializers', async function () {
  76. expect(await this.mock.counter()).to.equal(0n);
  77. await expect(this.mock.nestedReinitialize(2, 2)).to.be.revertedWithCustomError(
  78. this.mock,
  79. 'InvalidInitialization',
  80. );
  81. await expect(this.mock.nestedReinitialize(2, 3)).to.be.revertedWithCustomError(
  82. this.mock,
  83. 'InvalidInitialization',
  84. );
  85. await expect(this.mock.nestedReinitialize(3, 2)).to.be.revertedWithCustomError(
  86. this.mock,
  87. 'InvalidInitialization',
  88. );
  89. });
  90. it('can chain reinitializers', async function () {
  91. expect(await this.mock.counter()).to.equal(0n);
  92. await this.mock.chainReinitialize(2, 3);
  93. expect(await this.mock.counter()).to.equal(2n);
  94. });
  95. it('_getInitializedVersion returns right version', async function () {
  96. await this.mock.initialize();
  97. expect(await this.mock.getInitializedVersion()).to.equal(1n);
  98. await this.mock.reinitialize(12);
  99. expect(await this.mock.getInitializedVersion()).to.equal(12n);
  100. });
  101. describe('contract locking', function () {
  102. it('prevents initialization', async function () {
  103. await this.mock.disableInitializers();
  104. await expect(this.mock.initialize()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
  105. });
  106. it('prevents re-initialization', async function () {
  107. await this.mock.disableInitializers();
  108. await expect(this.mock.reinitialize(255n)).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
  109. });
  110. it('can lock contract after initialization', async function () {
  111. await this.mock.initialize();
  112. await this.mock.disableInitializers();
  113. await expect(this.mock.reinitialize(255n)).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
  114. });
  115. });
  116. });
  117. describe('events', function () {
  118. it('constructor initialization emits event', async function () {
  119. const mock = await ethers.deployContract('ConstructorInitializableMock');
  120. await expect(mock.deploymentTransaction()).to.emit(mock, 'Initialized').withArgs(1n);
  121. });
  122. it('initialization emits event', async function () {
  123. const mock = await ethers.deployContract('ReinitializerMock');
  124. await expect(mock.initialize()).to.emit(mock, 'Initialized').withArgs(1n);
  125. });
  126. it('reinitialization emits event', async function () {
  127. const mock = await ethers.deployContract('ReinitializerMock');
  128. await expect(mock.reinitialize(128)).to.emit(mock, 'Initialized').withArgs(128n);
  129. });
  130. it('chained reinitialization emits multiple events', async function () {
  131. const mock = await ethers.deployContract('ReinitializerMock');
  132. await expect(mock.chainReinitialize(2, 3))
  133. .to.emit(mock, 'Initialized')
  134. .withArgs(2n)
  135. .to.emit(mock, 'Initialized')
  136. .withArgs(3n);
  137. });
  138. });
  139. describe('complex testing with inheritance', function () {
  140. const mother = 12n;
  141. const gramps = '56';
  142. const father = 34n;
  143. const child = 78n;
  144. beforeEach('deploying', async function () {
  145. this.mock = await ethers.deployContract('SampleChild');
  146. await this.mock.initialize(mother, gramps, father, child);
  147. });
  148. it('initializes human', async function () {
  149. expect(await this.mock.isHuman()).to.be.true;
  150. });
  151. it('initializes mother', async function () {
  152. expect(await this.mock.mother()).to.equal(mother);
  153. });
  154. it('initializes gramps', async function () {
  155. expect(await this.mock.gramps()).to.equal(gramps);
  156. });
  157. it('initializes father', async function () {
  158. expect(await this.mock.father()).to.equal(father);
  159. });
  160. it('initializes child', async function () {
  161. expect(await this.mock.child()).to.equal(child);
  162. });
  163. });
  164. describe('disabling initialization', function () {
  165. it('old and new patterns in bad sequence', async function () {
  166. const DisableBad1 = await ethers.getContractFactory('DisableBad1');
  167. await expect(DisableBad1.deploy()).to.be.revertedWithCustomError(DisableBad1, 'InvalidInitialization');
  168. const DisableBad2 = await ethers.getContractFactory('DisableBad2');
  169. await expect(DisableBad2.deploy()).to.be.revertedWithCustomError(DisableBad2, 'InvalidInitialization');
  170. });
  171. it('old and new patterns in good sequence', async function () {
  172. const ok = await ethers.deployContract('DisableOk');
  173. await expect(ok.deploymentTransaction())
  174. .to.emit(ok, 'Initialized')
  175. .withArgs(1n)
  176. .to.emit(ok, 'Initialized')
  177. .withArgs(MAX_UINT64);
  178. });
  179. });
  180. });