Initializable.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { assert } from 'chai';
  2. import { initializable } from '../../../../src/decorators/Initializable';
  3. import { IInitializable } from '../../../../src/interfaces/IInitializable';
  4. describe('@initializable', () => {
  5. describe('initializable (initializeMethodKey: string): any', () => {
  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: custom initialization method name is passed', () => {
  24. const testFunc: () => void = () => {
  25. class Foo implements IInitializable {
  26. @initializable()
  27. public property: string;
  28. public initialize (): void {
  29. }
  30. public bar (property: string): void {
  31. this.property = property;
  32. }
  33. }
  34. const foo: Foo = new Foo();
  35. foo.bar('baz');
  36. foo.property;
  37. };
  38. it('shouldn\'t throws an errors if custom initialization method name is passed', () => {
  39. assert.doesNotThrow(testFunc, Error);
  40. });
  41. });
  42. describe('variant #3: property didn\'t initialized', () => {
  43. const testFunc: () => void = () => {
  44. class Foo implements IInitializable {
  45. @initializable()
  46. public property: string;
  47. public initialize (property: string): void {
  48. }
  49. }
  50. const foo: Foo = new Foo();
  51. foo.initialize('baz');
  52. foo.property;
  53. };
  54. it('should throws an error if property didn\'t initialized', () => {
  55. assert.throws(testFunc, /Property `property` is not initialized/);
  56. });
  57. });
  58. describe('variant #4: `initialize` method with custom name', () => {
  59. const testFunc: () => void = () => {
  60. class Foo {
  61. @initializable('bar')
  62. public property: string;
  63. public initialize (property: string): void {
  64. this.property = property;
  65. }
  66. }
  67. const foo: Foo = new Foo();
  68. foo.initialize('baz');
  69. foo.property;
  70. };
  71. it('should throws an error if `initialize` method with custom name wasn\'t found', () => {
  72. assert.throws(testFunc, /method with initialization logic not found/);
  73. });
  74. });
  75. });
  76. });