Initializable.spec.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. it('shouldn\'t throws an errors if property was initialized', () => {
  7. assert.doesNotThrow(() => {
  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. }, Error);
  19. });
  20. it('shouldn\'t throws an errors if custom initialization method name is passed', () => {
  21. assert.doesNotThrow(() => {
  22. class Foo implements IInitializable {
  23. @initializable()
  24. public property: string;
  25. public initialize (): void {
  26. }
  27. public bar (property: string): void {
  28. this.property = property;
  29. }
  30. }
  31. const foo: Foo = new Foo();
  32. foo.bar('baz');
  33. foo.property;
  34. }, Error);
  35. });
  36. it('should throws an error if property didn\'t initialized', () => {
  37. assert.throws(() => {
  38. class Foo implements IInitializable {
  39. @initializable()
  40. public property: string;
  41. public initialize (property: string): void {
  42. }
  43. }
  44. const foo: Foo = new Foo();
  45. foo.initialize('baz');
  46. foo.property;
  47. }, /Property `property` is not initialized/);
  48. });
  49. it('should throws an error if `initialize` method with custom name wasn\'t found', () => {
  50. assert.throws(() => {
  51. class Foo {
  52. @initializable('bar')
  53. public property: string;
  54. public initialize (property: string): void {
  55. this.property = property;
  56. }
  57. }
  58. const foo: Foo = new Foo();
  59. foo.initialize('baz');
  60. foo.property;
  61. }, /method with initialization logic not found/);
  62. });
  63. });
  64. });