1
|
|
|
jest.autoMockOff(); |
|
|
|
|
2
|
|
|
|
3
|
|
|
import ServicesContainer from "../ServicesContainer"; |
4
|
|
|
|
5
|
|
|
const equals = (actual, expected) => expect(actual).toBe(expected); |
6
|
|
|
const instance = new ServicesContainer(); |
7
|
|
|
const service = function () { |
8
|
|
|
let internalVar = "initial value"; |
9
|
|
|
|
10
|
|
|
this.setVar = function (value) { |
11
|
|
|
internalVar = value; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
this.getVar = function () { |
15
|
|
|
return internalVar; |
16
|
|
|
} |
17
|
|
|
}; |
18
|
|
|
|
19
|
|
|
test("error when service is not registered", () => { |
20
|
|
|
expect(() => instance.get("undefinedService")).toThrowError("Service undefinedService not found"); |
21
|
|
|
}); |
22
|
|
|
|
23
|
|
|
test("share same instance with multiple calls", () => { |
24
|
|
|
instance.register("a", service); |
25
|
|
|
equals(instance.get("a").getVar(), "initial value"); |
26
|
|
|
|
27
|
|
|
instance.get("a").setVar("x"); |
28
|
|
|
equals(instance.get("a").getVar(), "x"); |
29
|
|
|
equals(instance.get("a").getVar(), "x"); |
30
|
|
|
}); |
31
|
|
|
|
32
|
|
|
test("it's possible to get a new instance", () => { |
33
|
|
|
instance.register("b", service); |
34
|
|
|
equals(instance.get("b").getVar(), "initial value"); |
35
|
|
|
|
36
|
|
|
instance.get("b").setVar("x"); |
37
|
|
|
equals(instance.get("b").getVar(), "x"); |
38
|
|
|
equals(instance.getNewInstance("b").getVar(), "initial value"); |
39
|
|
|
equals(instance.get("b").getVar(), "x"); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
test("impossible to get a new instance when it is a singleton", () => { |
43
|
|
|
instance.setInstance("c", new service()); |
|
|
|
|
44
|
|
|
equals(instance.get("c").getVar(), "initial value"); |
45
|
|
|
|
46
|
|
|
expect(() => instance.getNewInstance("c")).toThrowError("Service c is an singleton, you cannot create a new instance"); |
47
|
|
|
}); |
48
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.