-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit.ts
152 lines (132 loc) · 4.31 KB
/
unit.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { MergeObjectInput } from ".";
import { Input } from "@shanzhai/interfaces";
describe(`MergeObjectInput`, () => {
type TestValue =
| `Test Value A A`
| `Test Value A B`
| `Test Value B A`
| `Test Value B B`
| `Test Value B C`
| `Test Value B D`
| `Test Value C A`
| `Test Value C B`
| `Test Value C C`;
describe(`on construction`, () => {
let mergeObjectInput: MergeObjectInput<TestValue>;
let inputGet: jasmine.Spy;
let input: Input<{
readonly [keyA: string]: { readonly [keyB: string]: TestValue };
}>;
beforeAll(() => {
inputGet = jasmine.createSpy(`inputGet`);
input = { get: inputGet };
mergeObjectInput = new MergeObjectInput(input);
});
it(`exposes its input`, () => {
expect(mergeObjectInput.input).toEqual(input);
});
it(`does not get its input`, () => {
expect(inputGet).not.toHaveBeenCalled();
});
});
describe(`get`, () => {
describe(`when no collisions occur`, () => {
let mergeObjectInput: MergeObjectInput<TestValue>;
let inputGet: jasmine.Spy;
let input: Input<{
readonly [keyA: string]: { readonly [keyB: string]: TestValue };
}>;
let result: {
readonly [key: string]: TestValue;
};
beforeAll(async () => {
inputGet = jasmine.createSpy(`inputGet`).and.resolveTo({
"Test Key A": {
"Test Key A A": `Test Value A A`,
"Test Key A B": `Test Value A B`,
},
"Test Key B": {
"Test Key B A": `Test Value B A`,
"Test Key B B": `Test Value B B`,
"Test Key B C": `Test Value B C`,
"Test Key B D": `Test Value B D`,
},
"Test Key C": {
"Test Key C A": `Test Value C A`,
"Test Key C B": `Test Value C B`,
"Test Key C C": `Test Value C C`,
},
});
input = { get: inputGet };
mergeObjectInput = new MergeObjectInput(input);
result = await mergeObjectInput.get();
});
it(`continues to expose its input`, () => {
expect(mergeObjectInput.input).toEqual(input);
});
it(`gets its input once`, () => {
expect(inputGet).toHaveBeenCalledTimes(1);
});
it(`returns an object containing everything gotten`, () => {
expect(result).toEqual({
"Test Key A A": `Test Value A A`,
"Test Key A B": `Test Value A B`,
"Test Key B A": `Test Value B A`,
"Test Key B B": `Test Value B B`,
"Test Key B C": `Test Value B C`,
"Test Key B D": `Test Value B D`,
"Test Key C A": `Test Value C A`,
"Test Key C B": `Test Value C B`,
"Test Key C C": `Test Value C C`,
});
});
});
describe(`when a collision occurs`, () => {
let mergeObjectInput: MergeObjectInput<TestValue>;
let inputGet: jasmine.Spy;
let input: Input<{
readonly [keyA: string]: { readonly [keyB: string]: TestValue };
}>;
let error: Error;
beforeAll(async () => {
inputGet = jasmine.createSpy(`inputGet`).and.resolveTo({
"Test Key A": {
"Test Key A A": `Test Value A A`,
"Test Key C B": `Test Value A B`,
},
"Test Key B": {
"Test Key B A": `Test Value B A`,
"Test Key B B": `Test Value B B`,
"Test Key B C": `Test Value B C`,
"Test Key B D": `Test Value B D`,
},
"Test Key C": {
"Test Key C A": `Test Value C A`,
"Test Key C B": `Test Value C B`,
"Test Key C C": `Test Value C C`,
},
});
input = { get: inputGet };
mergeObjectInput = new MergeObjectInput(input);
try {
await mergeObjectInput.get();
} catch (e) {
error = e as Error;
}
});
it(`continues to expose its input`, () => {
expect(mergeObjectInput.input).toEqual(input);
});
it(`gets its input once`, () => {
expect(inputGet).toHaveBeenCalledTimes(1);
});
it(`throws the expected error`, () => {
expect(error).toEqual(
new Error(
`Unable to merge objects as key "Test Key C B" exists in more than one source.`
)
);
});
});
});
});