Passed
Push — es2015 ( 64e5b0...a07c6e )
by Luís
01:47
created

ServicesContainer.test.js ➔ service   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A ServicesContainer.test.js ➔ ... ➔ this.getVar 0 3 1
A ServicesContainer.test.js ➔ ... ➔ this.setVar 0 3 1
1
jest.autoMockOff();
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

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.

Loading history...
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());
0 ignored issues
show
Coding Style Best Practice introduced by
By convention, constructors like service should be capitalized.
Loading history...
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