Initializable.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const { contract } = require('@openzeppelin/test-environment');
  2. const { expectRevert } = require('@openzeppelin/test-helpers');
  3. const { assert } = require('chai');
  4. const InitializableMock = contract.fromArtifact('InitializableMock');
  5. const SampleChild = contract.fromArtifact('SampleChild');
  6. describe('Initializable', function () {
  7. describe('basic testing without inheritance', function () {
  8. beforeEach('deploying', async function () {
  9. this.contract = await InitializableMock.new();
  10. });
  11. context('before initialize', function () {
  12. it('initializer has not run', async function () {
  13. assert.isFalse(await this.contract.initializerRan());
  14. });
  15. });
  16. context('after initialize', function () {
  17. beforeEach('initializing', async function () {
  18. await this.contract.initialize();
  19. });
  20. it('initializer has run', async function () {
  21. assert.isTrue(await this.contract.initializerRan());
  22. });
  23. it('initializer does not run again', async function () {
  24. await expectRevert(this.contract.initialize(), 'Initializable: contract is already initialized');
  25. });
  26. });
  27. context('after nested initialize', function () {
  28. beforeEach('initializing', async function () {
  29. await this.contract.initializeNested();
  30. });
  31. it('initializer has run', async function () {
  32. assert.isTrue(await this.contract.initializerRan());
  33. });
  34. });
  35. });
  36. describe('complex testing with inheritance', function () {
  37. const mother = 12;
  38. const gramps = '56';
  39. const father = 34;
  40. const child = 78;
  41. beforeEach('deploying', async function () {
  42. this.contract = await SampleChild.new();
  43. });
  44. beforeEach('initializing', async function () {
  45. await this.contract.initialize(mother, gramps, father, child);
  46. });
  47. it('initializes human', async function () {
  48. assert.equal(await this.contract.isHuman(), true);
  49. });
  50. it('initializes mother', async function () {
  51. assert.equal(await this.contract.mother(), mother);
  52. });
  53. it('initializes gramps', async function () {
  54. assert.equal(await this.contract.gramps(), gramps);
  55. });
  56. it('initializes father', async function () {
  57. assert.equal(await this.contract.father(), father);
  58. });
  59. it('initializes child', async function () {
  60. assert.equal(await this.contract.child(), child);
  61. });
  62. });
  63. });