-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject-graph.test.ts
278 lines (206 loc) · 9.05 KB
/
object-graph.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { describe, expect, expectTypeOf, test, vi } from "vitest";
import {
type Color,
type Shirt,
type Size,
extraShirtsMock,
shirtsMock,
} from "../mocks/object-graph.mock";
import { ObjectGraph } from "../src/object-graph";
describe("length", () => {
test("gets the length of the object graph", () => {
const shirtToAdd: Shirt = { sku: "9", color: "orange", size: "small" };
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
expect(shirtsGraph.length).toBe(8);
shirtsGraph.add(shirtToAdd);
expect(shirtsGraph.length).toBe(9);
});
});
describe("size", () => {
test("gets the size of the object graph", () => {
const shirtToAdd: Shirt = { sku: "9", color: "orange", size: "small" };
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
expect(shirtsGraph.size).toBe(8);
shirtsGraph.add(shirtToAdd);
expect(shirtsGraph.size).toBe(9);
});
});
describe("keys()", () => {
test("gets an iterator object that contains the keys of the object graph", () => {
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const shirtsGraphKeysIterator = shirtsGraph.keys();
const shirtsGraphKeys = Array.from(shirtsGraphKeysIterator);
expectTypeOf(shirtsGraphKeysIterator[Symbol.iterator]).toBeFunction();
expect(shirtsGraphKeys).toEqual(["1", "2", "3", "4", "5", "6", "7", "8"]);
});
});
describe("values()", () => {
test("gets an iterator object that contains the values of the object graph", () => {
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const shirtsGraphValuesIterator = shirtsGraph.values();
const shirtsGraphValues = Array.from(shirtsGraphValuesIterator);
expectTypeOf(shirtsGraphValuesIterator[Symbol.iterator]).toBeFunction();
expect(shirtsGraphValues).toHaveLength(8);
expect(shirtsGraphValues[0]).toStrictEqual(shirtsMock[0]);
});
});
describe("get()", () => {
test("logs an error when there is no node with the provided key in the object graph", () => {
const consoleErrorSpy = vi.spyOn(console, "error");
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const returnedNode = shirtsGraph.get("9");
expect(consoleErrorSpy).toHaveBeenCalled();
expect(returnedNode).toBeUndefined();
});
test("gets a node of the object graph", () => {
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const returnedNode = shirtsGraph.get(shirtsMock[0].sku);
expect(returnedNode?.color).toBe(shirtsMock[0].color);
expect(returnedNode?.size).toBe(shirtsMock[0].size);
});
});
describe("copy()", () => {
test("gets a copy of the original object graph", () => {
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const copiedShirtsGraph = shirtsGraph.copy();
expect(shirtsGraph.get("1")).toEqual(copiedShirtsGraph.get("1"));
});
});
describe("subgraph()", () => {
test("gets a subgraph of the original object graph", () => {
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const shirtsSubgraph = shirtsGraph.subgraph(["1", "2", "3", "4"]);
expect(shirtsGraph).toHaveLength(8);
expect(shirtsSubgraph).toHaveLength(4);
});
});
describe("add()", () => {
test("throws an error when a node with the same key already exists in the object graph", () => {
const shirtToAdd: Shirt = { sku: "1", color: "purple", size: "large" };
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
expect(shirtsGraph.get("1")?.color).toBe("red");
expect(() => {
shirtsGraph.add(shirtToAdd);
}).toThrowError();
expect(shirtsGraph.get("1")?.color).toBe("red");
});
test("adds a node to the object graph", () => {
const shirtsGraph = new ObjectGraph<Shirt>([], (shirt) => shirt.sku);
shirtsGraph.add(shirtsMock[0]);
expect(shirtsGraph.get("1")).toEqual(shirtsMock[0]);
});
});
describe("toAdded()", () => {
test("gets a copy of the original object graph with a received node added", () => {
const consoleErrorSpy = vi.spyOn(console, "error");
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const copiedShirtsGraph = shirtsGraph.toAdded(extraShirtsMock[0]);
expect(shirtsGraph.size).toBe(8);
expect(copiedShirtsGraph.size).toBe(9);
const returnedNodeFromCopy = copiedShirtsGraph.get("9");
expect(consoleErrorSpy).not.toHaveBeenCalled();
expect(returnedNodeFromCopy).toBeDefined();
const returnedNodeFromOriginal = shirtsGraph.get("9");
expect(consoleErrorSpy).toHaveBeenCalled();
expect(returnedNodeFromOriginal).toBeUndefined();
});
});
describe("update()", () => {
test("throws an error when there is no node with the same key in the object graph", () => {
const shirtToUpdate: Shirt = { sku: "9", color: "orange", size: "small" };
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
expect(shirtsGraph.size).toBe(8);
expect(() => {
shirtsGraph.update(shirtToUpdate);
}).toThrowError();
expect(shirtsGraph.size).toBe(8);
});
test("updates a node in the object graph", () => {
const shirtToUpdate: Shirt = { sku: "1", color: "red", size: "large" };
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
expect(shirtsGraph.get("1")?.size).toBe("small");
shirtsGraph.update(shirtToUpdate);
expect(shirtsGraph.get("1")?.size).toBe("large");
});
});
describe("toUpdated()", () => {
test("gets a copy of the original object graph with a received node updated", () => {
const shirtToUpdate: Shirt = { sku: "1", color: "red", size: "large" };
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const copiedShirtsGraph = shirtsGraph.toUpdated(shirtToUpdate);
expect(shirtsGraph.get("1")?.size).toBe("small");
expect(copiedShirtsGraph.get("1")?.size).toBe("large");
});
});
describe("remove()", () => {
test("removes a node from the object graph", () => {
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
expect(shirtsGraph.get("1")).toBeDefined();
shirtsGraph.remove("1");
expect(shirtsGraph.get("1")).toBeUndefined();
});
});
describe("toRemoved()", () => {
test("gets a copy of the original object graph with a received node removed", () => {
const consoleErrorSpy = vi.spyOn(console, "error");
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const copiedShirtsGraph = shirtsGraph.toRemoved("1");
expect(shirtsGraph.size).toBe(8);
expect(copiedShirtsGraph.size).toBe(7);
const returnedNodeFromOriginal = shirtsGraph.get("1");
expect(consoleErrorSpy).not.toHaveBeenCalled();
expect(returnedNodeFromOriginal).toBeDefined();
const returnedNodeFromCopy = copiedShirtsGraph.get("1");
expect(returnedNodeFromCopy).toBeUndefined();
expect(consoleErrorSpy).toHaveBeenCalled();
});
});
describe("valuesOf()", () => {
test("gets all values of the provided property", () => {
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const sizePropertyValues = shirtsGraph.valuesOf("size");
expect(sizePropertyValues).toContain("small");
expect(sizePropertyValues).toContain("medium");
expect(sizePropertyValues).toContain("large");
});
test("gets all values of the provided property from selected nodes", () => {
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const sizePropertyValues = shirtsGraph.valuesOf("size", ["1", "2", "3", "4"]);
expect(sizePropertyValues).toContain("small");
expect(sizePropertyValues).toContain("medium");
expect(sizePropertyValues).not.toContain("large");
});
});
describe("match()", () => {
test("gets all nodes that match with the provided shape", () => {
const colorsToMatch: Color[] = ["red", "blue"];
const sizesToMatch: Size[] = ["small", "medium"];
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const matchedShirts = shirtsGraph.match({
color: colorsToMatch,
size: sizesToMatch,
});
expect(matchedShirts).toHaveLength(4);
for (const matchedShirt of matchedShirts) {
expect(matchedShirt.color).oneOf(colorsToMatch);
expect(matchedShirt.size).oneOf(sizesToMatch);
}
});
test("gets matched nodes ignoring undefined value in the shape", () => {
const colorsToMatch: Color[] = ["yellow", "green"];
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const matchedShirts = shirtsGraph.match({
color: colorsToMatch,
size: undefined,
});
expect(matchedShirts).toHaveLength(3);
for (const matchedShirt of matchedShirts) {
expect(matchedShirt.color).oneOf(colorsToMatch);
}
});
test("gets all nodes for an empty shape", () => {
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
const matchedShirts = shirtsGraph.match({});
expect(matchedShirts).toHaveLength(8);
});
});