Skip to content

Commit 6158dd2

Browse files
authored
fix: throw an error on invalid CRUD actions (#78)
1 parent d71714d commit 6158dd2

4 files changed

Lines changed: 28 additions & 46 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kasnix/structured-objects",
3-
"version": "1.4.1",
3+
"version": "1.4.2",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/luckasnix/structured-objects.git"

src/object-graph.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
114114
}
115115
const nodeKey = this.keyExtractor(nodeValue);
116116
if (this.nodes.get(nodeKey)) {
117-
console.error("A node with the same key already exists in the object graph");
117+
throw new Error("A node with the same key already exists in the object graph");
118118
}
119119
this.nodes.set(nodeKey, nodeValue);
120120
}
@@ -139,7 +139,7 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
139139
}
140140
const nodeKey = this.keyExtractor(nodeValue);
141141
if (!this.nodes.get(nodeKey)) {
142-
console.error("A node with the provided key does not exist in the object graph");
142+
throw new Error("A node with the provided key does not exist in the object graph");
143143
}
144144
this.nodes.set(nodeKey, nodeValue);
145145
}
@@ -165,9 +165,6 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
165165
if (typeof nodeKey !== "string") {
166166
throw new TypeError("The parameter 'nodeKey' must be a string");
167167
}
168-
if (!this.nodes.get(nodeKey)) {
169-
console.error("A node with this key does not exist in this object graph");
170-
}
171168
this.nodes.delete(nodeKey);
172169
}
173170

tests/object-graph.test.ts

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,15 @@ describe("subgraph()", () => {
101101
});
102102

103103
describe("add()", () => {
104-
test("logs an error when a node with the same key already exists in the object graph", () => {
105-
const consoleErrorSpy = vi.spyOn(console, "error");
104+
test("throws an error when a node with the same key already exists in the object graph", () => {
105+
const shirtToAdd: Shirt = { sku: "1", color: "purple", size: "large" };
106106
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
107107

108-
shirtsGraph.add(shirtsMock[0]);
109-
110-
expect(consoleErrorSpy).toHaveBeenCalled();
108+
expect(shirtsGraph.get("1")?.color).toBe("red");
109+
expect(() => {
110+
shirtsGraph.add(shirtToAdd);
111+
}).toThrowError();
112+
expect(shirtsGraph.get("1")?.color).toBe("red");
111113
});
112114

113115
test("adds a node to the object graph", () => {
@@ -122,33 +124,35 @@ describe("add()", () => {
122124
describe("toAdded()", () => {
123125
test("gets a copy of the original object graph with a received node added", () => {
124126
const consoleErrorSpy = vi.spyOn(console, "error");
125-
const shirtsGraph = new ObjectGraph<Shirt>([], (shirt) => shirt.sku);
127+
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
126128

127-
const copiedShirtsGraph = shirtsGraph.toAdded(shirtsMock[0]);
129+
const copiedShirtsGraph = shirtsGraph.toAdded(extraShirtsMock[0]);
128130

129-
expect(shirtsGraph.size).toBe(0);
130-
expect(copiedShirtsGraph.size).toBe(1);
131+
expect(shirtsGraph.size).toBe(8);
132+
expect(copiedShirtsGraph.size).toBe(9);
131133

132-
const returnedNodeFromCopy = copiedShirtsGraph.get("1");
134+
const returnedNodeFromCopy = copiedShirtsGraph.get("9");
133135

134136
expect(consoleErrorSpy).not.toHaveBeenCalled();
135137
expect(returnedNodeFromCopy).toBeDefined();
136138

137-
const returnedNodeFromOriginal = shirtsGraph.get("1");
139+
const returnedNodeFromOriginal = shirtsGraph.get("9");
138140

139141
expect(consoleErrorSpy).toHaveBeenCalled();
140142
expect(returnedNodeFromOriginal).toBeUndefined();
141143
});
142144
});
143145

144146
describe("update()", () => {
145-
test("logs an error when there is no node with the same key in the object graph", () => {
146-
const consoleErrorSpy = vi.spyOn(console, "error");
147+
test("throws an error when there is no node with the same key in the object graph", () => {
148+
const shirtToUpdate: Shirt = { sku: "9", color: "orange", size: "small" };
147149
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
148150

149-
shirtsGraph.update(extraShirtsMock[0]);
150-
151-
expect(consoleErrorSpy).toHaveBeenCalled();
151+
expect(shirtsGraph.size).toBe(8);
152+
expect(() => {
153+
shirtsGraph.update(shirtToUpdate);
154+
}).toThrowError();
155+
expect(shirtsGraph.size).toBe(8);
152156
});
153157

154158
test("updates a node in the object graph", () => {
@@ -176,33 +180,14 @@ describe("toUpdated()", () => {
176180
});
177181

178182
describe("remove()", () => {
179-
test("logs an error when there is no node with the provided key in the object graph", () => {
180-
const consoleErrorSpy = vi.spyOn(console, "error");
181-
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
182-
183-
const returnedNode = shirtsGraph.remove("9");
184-
185-
expect(consoleErrorSpy).toHaveBeenCalled();
186-
expect(returnedNode).toBeUndefined();
187-
});
188-
189183
test("removes a node from the object graph", () => {
190-
const consoleErrorSpy = vi.spyOn(console, "error");
191184
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
192185

193-
const returnedNodeFromFirstAttempt = shirtsGraph.get("1");
194-
195-
expect(consoleErrorSpy).not.toHaveBeenCalled();
196-
expect(returnedNodeFromFirstAttempt).toBeDefined();
186+
expect(shirtsGraph.get("1")).toBeDefined();
197187

198188
shirtsGraph.remove("1");
199189

200-
expect(consoleErrorSpy).not.toHaveBeenCalled();
201-
202-
const returnedNodeFromSecondAttempt = shirtsGraph.get("1");
203-
204-
expect(consoleErrorSpy).toHaveBeenCalled();
205-
expect(returnedNodeFromSecondAttempt).toBeUndefined();
190+
expect(shirtsGraph.get("1")).toBeUndefined();
206191
});
207192
});
208193

@@ -223,8 +208,8 @@ describe("toRemoved()", () => {
223208

224209
const returnedNodeFromCopy = copiedShirtsGraph.get("1");
225210

226-
expect(consoleErrorSpy).toHaveBeenCalled();
227211
expect(returnedNodeFromCopy).toBeUndefined();
212+
expect(consoleErrorSpy).toHaveBeenCalled();
228213
});
229214
});
230215

0 commit comments

Comments
 (0)