-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton.test.ts
87 lines (68 loc) · 1.66 KB
/
singleton.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
import { Singleton } from 'src';
import { test, expect } from 'vitest';
test('should return the same instance when attempting to create multiple instances', () => {
@Singleton
class Foo {
value: string;
constructor(value: string) {
this.value = value;
}
}
const foo1 = new Foo('first');
const foo2 = new Foo('second');
expect(foo1).toBe(foo2);
expect(foo1.value).toBe('first');
});
test('should have no problem with instantiation', () => {
@Singleton
class Foo {
value: string;
constructor(value: string) {
this.value = value;
}
}
expect(() => {
new Foo('value');
}).not.toThrow();
});
test('should ensure singleton across different invocations', () => {
@Singleton
class Foo {
value: string;
constructor(value: string) {
this.value = value;
}
}
const foo1 = new Foo('foo');
const foo2 = new Foo('bar');
expect(foo1).toBe(foo2);
expect(foo1.value).toBe('foo');
});
test('should not allow overriding the singleton instance', () => {
@Singleton
class Foo {
value: string;
constructor(value: string) {
this.value = value;
}
}
const foo1 = new Foo('initial');
const foo2 = new Foo('changed');
expect(foo1).toBe(foo2);
expect(foo2.value).toBe('initial');
});
test('should return the same instance even after multiple constructor calls', () => {
@Singleton
class Foo {
value: string;
constructor(value: string) {
this.value = value;
}
}
const foo1 = new Foo('first');
const foo2 = new Foo('second');
const foo3 = new Foo('third');
expect(foo1).toBe(foo2);
expect(foo1).toBe(foo3);
expect(foo3.value).toBe('first');
});