-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingletonLazyAppServicesContainer.test.ts
113 lines (86 loc) · 3.71 KB
/
SingletonLazyAppServicesContainer.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { AppConfiguration, AppServices } from "../abstractions/application";
import { AppConfigurationRendererImplementation } from "./AppConfigurationRendererImplementation";
import {
ServicesFactories,
SingletonLazyAppServicesContainer,
} from "./SingletonLazyAppServicesContainer";
describe(SingletonLazyAppServicesContainer.name, () => {
it("should not call the factory when the service is not required", () => {
// Arrange
const appConfigurationResult = {} as unknown as AppConfiguration;
const appConfigurationFactory = jest.fn((_) => appConfigurationResult);
const factories = {
appConfigurationFactory,
} as unknown as ServicesFactories;
// Act
const appServices = new SingletonLazyAppServicesContainer(factories);
// Assert
expect(appConfigurationFactory).not.toHaveBeenCalled();
});
it("should call the factory when the service is required with appServices as parameter", () => {
// Arrange
const appConfigurationResult = {} as unknown as AppConfiguration;
const appConfigurationFactory = jest.fn((_) => appConfigurationResult);
const factories = {
appConfigurationFactory,
} as unknown as ServicesFactories;
const appServices = new SingletonLazyAppServicesContainer(factories);
// Act
const { appConfiguration } = appServices;
// Assert
expect(appConfigurationFactory).toHaveBeenCalled();
expect(appConfigurationFactory).toHaveBeenCalledWith(appServices);
expect(appConfiguration).toBe(appConfigurationResult);
});
it("should not call other factories when a service is required", () => {
// Arrange
const appConfigurationResult = {} as unknown as AppConfiguration;
const appConfigurationFactory = jest.fn((_) => appConfigurationResult);
const windowResult = {} as unknown as Window;
const windowFactory = jest.fn((_) => windowResult);
const factories = {
appConfigurationFactory,
windowFactory,
} as unknown as ServicesFactories;
const appServices = new SingletonLazyAppServicesContainer(factories);
// Act
const { appConfiguration } = appServices;
// Assert
expect(windowFactory).not.toHaveBeenCalled();
});
it("should call other factories when a service required requires it", () => {
// Arrange
const appConfigurationResult = {} as unknown as AppConfiguration;
const appConfigurationFactory = jest.fn((_) => appConfigurationResult);
const appConfigurationRendererFactory = (appServices: AppServices) =>
new AppConfigurationRendererImplementation(appServices);
const windowResult = {} as unknown as Window;
const windowFactory = jest.fn((_) => windowResult);
const factories = {
appConfigurationFactory,
appConfigurationRendererFactory,
windowFactory,
} as unknown as ServicesFactories;
const appServices = new SingletonLazyAppServicesContainer(factories);
// Act
const { appConfigurationRenderer } = appServices;
// Assert
expect(windowFactory).not.toHaveBeenCalled();
expect(appConfigurationFactory).toHaveBeenCalled();
});
it("should call the factory once when the service is required multiple times", () => {
// Arrange
const appConfigurationResult = {} as unknown as AppConfiguration;
const appConfigurationFactory = jest.fn((_) => appConfigurationResult);
const factories = {
appConfigurationFactory,
} as unknown as ServicesFactories;
const appServices = new SingletonLazyAppServicesContainer(factories);
// Act
const { appConfiguration } = appServices;
const appConfiguration2 = appServices.appConfiguration;
// Assert
expect(appConfigurationFactory).toHaveBeenCalledTimes(1);
expect(appConfiguration).toBe(appConfiguration2);
});
});