Initializable.spec.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import { initializable } from '../../../../src/decorators/Initializable';
  4. import { IInitializable } from '../../../../src/interfaces/IInitializable';
  5. describe('@initializable', () => {
  6. describe('Variant #1: property was initialized', () => {
  7. const testFunc: () => void = () => {
  8. class Foo implements IInitializable {
  9. @initializable()
  10. public property!: string;
  11. public initialize (property: string): void {
  12. this.property = property;
  13. }
  14. }
  15. const foo: Foo = new Foo();
  16. foo.initialize('baz');
  17. foo.property;
  18. };
  19. it('shouldn\'t throws an errors if property was initialized', () => {
  20. assert.doesNotThrow(testFunc, Error);
  21. });
  22. });
  23. describe('Variant #2: `initialize` method should be called first', () => {
  24. describe('Variant #1: `initialize` method was called first', () => {
  25. const testFunc: () => void = () => {
  26. class Foo {
  27. @initializable()
  28. public property!: string;
  29. public initialize (property: string): void {
  30. this.property = property;
  31. }
  32. public bar (): void {}
  33. }
  34. const foo: Foo = new Foo();
  35. foo.initialize('baz');
  36. foo.bar();
  37. };
  38. it('shouldn\'t throw an error if `initialize` method was called first', () => {
  39. assert.doesNotThrow(testFunc, /Class should be initialized/);
  40. });
  41. });
  42. describe('Variant #2: other method was called inside `initialize` method with initialization of the property', () => {
  43. const testFunc: () => void = () => {
  44. class Foo {
  45. @initializable()
  46. public property!: string;
  47. public initialize (property: string): void {
  48. this.innerInitialize(property);
  49. }
  50. public innerInitialize (property: string): void {
  51. this.property = property;
  52. }
  53. }
  54. const foo: Foo = new Foo();
  55. foo.initialize('baz');
  56. };
  57. it('shouldn\'t throw an error if other method was called inside `initialize` method', () => {
  58. assert.doesNotThrow(testFunc, /Class should be initialized/);
  59. });
  60. });
  61. describe('Variant #3: other method was called inside `initialize` method without initialization of the property', () => {
  62. const testFunc: () => void = () => {
  63. class Foo {
  64. @initializable()
  65. public property!: string;
  66. public initialize (property: string): void {
  67. this.innerInitialize(property);
  68. }
  69. public innerInitialize (property: string): void {
  70. }
  71. }
  72. const foo: Foo = new Foo();
  73. foo.initialize('baz');
  74. };
  75. it('should throws an error if other method was called inside `initialize` method without initialization of the property', () => {
  76. assert.throws(testFunc, /Property `property` is not initialized/);
  77. });
  78. });
  79. describe('Variant #4: `initialize` method wasn\'t called first', () => {
  80. const testFunc: () => void = () => {
  81. class Foo {
  82. @initializable()
  83. public property!: string;
  84. public initialize (property: string): void {
  85. this.property = property;
  86. }
  87. public bar (): void {}
  88. }
  89. const foo: Foo = new Foo();
  90. foo.bar();
  91. foo.initialize('baz');
  92. };
  93. it('should throws an error if `initialize` method wasn\'t called first', () => {
  94. assert.throws(testFunc, /Class should be initialized/);
  95. });
  96. });
  97. describe('Variant #5: `initialize` method wasn\'t called', () => {
  98. const testFunc: () => void = () => {
  99. class Foo {
  100. @initializable()
  101. public property!: string;
  102. public initialize (property: string): void {
  103. this.property = property;
  104. }
  105. public bar (): void {}
  106. }
  107. const foo: Foo = new Foo();
  108. foo.bar();
  109. };
  110. it('should throws an error if `initialize` method wasn\'t called first', () => {
  111. assert.throws(testFunc, /Class should be initialized/);
  112. });
  113. });
  114. });
  115. describe('Variant #3: property didn\'t initialized', () => {
  116. const testFunc: () => void = () => {
  117. class Foo implements IInitializable {
  118. @initializable()
  119. public property!: string;
  120. public initialize (property: string): void {
  121. }
  122. }
  123. const foo: Foo = new Foo();
  124. foo.initialize('baz');
  125. foo.property;
  126. };
  127. it('should throws an error if property didn\'t initialized', () => {
  128. assert.throws(testFunc, /Property `property` is not initialized/);
  129. });
  130. });
  131. });