Initializable.spec.ts 6.8 KB

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