Passed
Branch feature/typescript5 (203f49)
by Lorenzo
02:43
created

Class.getDep   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import { flushPromises } from '@test/utils/testUtils';
2
import { InjectBean } from '@/decorators/InjectBean';
3
4
vi.mock('@/decorators', () => ({
5
  logger: {
6
    info: vi.fn(),
7
    debug: vi.fn(),
8
    error: vi.fn(),
9
  },
10
}));
11
12
describe('InjectBean.ts', () => {
13
  beforeEach(() => {
14
    vi.resetAllMocks();
15
  });
16
17
  it('injects a dependency', async () => {
18
    // GIVEN
19
    class TypeA {}
20
    const T: any = TypeA;
21
    T.instance = new TypeA();
22
23
    // WHEN
24
    class Class {
25
      @InjectBean(TypeA)
26
        dep!: TypeA;
27
28
      getDep() {
29
        return this.dep;
30
      }
31
    }
32
    const instance = new Class();
33
    await flushPromises();
34
35
    // THEN
36
    expect(instance.getDep()).toBe(T.instance);
37
  });
38
39
  it('injects multiple dependencies', async () => {
40
    // GIVEN
41
    class TypeA {}
42
    const TA: any = TypeA;
43
    TA.instance = new TypeA();
44
45
    class TypeB {}
46
    const TB: any = TypeB;
47
    TB.instance = new TypeB();
48
49
    // WHEN
50
    class Class {
51
      @InjectBean(TypeA)
52
        dep1!: TypeA;
53
54
      @InjectBean(TypeB)
55
        dep2!: TypeB;
56
57
      getDeps() {
58
        return [this.dep1, this.dep2];
59
      }
60
    }
61
    const instance = new Class();
62
    await flushPromises();
63
64
    // THEN
65
    const [dep1, dep2] = instance.getDeps();
66
    expect(dep1).toBe(TA.instance);
67
    expect(dep2).toBe(TB.instance);
68
  });
69
70
  it('throws an error if trying to inject a non injectable dependency', async () => {
71
    // GIVEN
72
    class TypeA {}
73
    let instance;
74
75
    // WHEN
76
    expect(() => {
77
      class Class {
78
        @InjectBean(TypeA)
79
          dep!: TypeA;
80
      }
81
      instance = new Class();
82
    }).toThrow(new Error('Cannot get instance from TypeA. Make sure that TypeA has @Bean as class decorator'));
83
    expect(instance).toBe(undefined);
84
  });
85
86
  it('throws an error if trying to inject without specify a type', async () => {
87
    // GIVEN
88
    class TypeA {}
89
    let instance;
90
91
    // WHEN
92
    expect(() => {
93
      class Class {
94
        @InjectBean(null)
95
          dep!: TypeA;
96
      }
97
      instance = new Class();
98
    }).toThrow(new Error('Please specify the type of Bean. Example: @InjectBean(BeanClass)'));
99
    expect(instance).toBe(undefined);
100
  });
101
102
  it('throws an error when dependency is not found', async () => {
103
    // GIVEN
104
    class TypeA {}
105
    const T: any = TypeA;
106
    T.instance = new TypeA();
107
    class Class {
108
      @InjectBean(TypeA)
109
        dep!: TypeA;
110
111
      getDep() {
112
        return this.dep;
113
      }
114
    }
115
    const instance = new Class();
116
117
    // WHEN
118
    let dep;
119
    expect(() => {
120
      dep = instance.getDep();
121
    }).toThrow(new Error('Injection Failed'));
122
123
    // THEN
124
    expect(dep).toBe(undefined);
125
  });
126
});
127