Initializable.spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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('should throws an error if `initialize` method wasn\'t called first', () => {
  40. assert.doesNotThrow(testFunc, /Class should be initialized/);
  41. });
  42. });
  43. describe('variant #2: `initialize` method wasn\'t called first', () => {
  44. const testFunc: () => void = () => {
  45. class Foo {
  46. @initializable()
  47. public property!: string;
  48. public initialize (property: string): void {
  49. this.property = property;
  50. }
  51. public bar (): void {}
  52. }
  53. const foo: Foo = new Foo();
  54. foo.bar();
  55. foo.initialize('baz');
  56. };
  57. it('should throws an error if `initialize` method wasn\'t called first', () => {
  58. assert.throws(testFunc, /Class should be initialized/);
  59. });
  60. });
  61. describe('variant #3: `initialize` method wasn\'t called', () => {
  62. const testFunc: () => void = () => {
  63. class Foo {
  64. @initializable()
  65. public property!: string;
  66. public initialize (property: string): void {
  67. this.property = property;
  68. }
  69. public bar (): void {}
  70. }
  71. const foo: Foo = new Foo();
  72. foo.bar();
  73. };
  74. it('should throws an error if `initialize` method wasn\'t called first', () => {
  75. assert.throws(testFunc, /Class should be initialized/);
  76. });
  77. });
  78. });
  79. describe('variant #3: property didn\'t initialized', () => {
  80. const testFunc: () => void = () => {
  81. class Foo implements IInitializable {
  82. @initializable()
  83. public property!: string;
  84. public initialize (property: string): void {
  85. }
  86. }
  87. const foo: Foo = new Foo();
  88. foo.initialize('baz');
  89. foo.property;
  90. };
  91. it('should throws an error if property didn\'t initialized', () => {
  92. assert.throws(testFunc, /Property `property` is not initialized/);
  93. });
  94. });
  95. describe('variant #4: `initialize` method with custom name', () => {
  96. const testFunc: () => void = () => {
  97. class Foo {
  98. @initializable('bar')
  99. public property!: string;
  100. public initialize (property: string): void {
  101. this.property = property;
  102. }
  103. }
  104. const foo: Foo = new Foo();
  105. foo.initialize('baz');
  106. foo.property;
  107. };
  108. it('should throws an error if `initialize` method with custom name wasn\'t found', () => {
  109. assert.throws(testFunc, /method with initialization logic not found/);
  110. });
  111. });
  112. });
  113. });