Skip to content

Commit aeccae1

Browse files
committed
chore: test stream-list-diff
1 parent aaf5f3f commit aeccae1

File tree

2 files changed

+177
-8
lines changed

2 files changed

+177
-8
lines changed

src/lib/stream-list-diff/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function isDataValid<T extends Record<string, unknown>>(
8787
emitter.emit(
8888
StreamEvent.Error,
8989
new Error(
90-
`Your ${listType} must only contain valid objects. Found ${data}`,
90+
`Your ${listType} must only contain valid objects. Found '${data}'`,
9191
),
9292
);
9393
return false;
@@ -96,7 +96,7 @@ function isDataValid<T extends Record<string, unknown>>(
9696
emitter.emit(
9797
StreamEvent.Error,
9898
new Error(
99-
`The reference property ${String(referenceProperty)} is not available in all the objects of your ${listType}.`,
99+
`The reference property '${String(referenceProperty)}' is not available in all the objects of your ${listType}.`,
100100
),
101101
);
102102
return false;
@@ -110,17 +110,17 @@ function getDiffChunks<T extends Record<string, unknown>>(
110110
referenceProperty: ReferenceProperty<T>,
111111
emitter: Emitter<T>,
112112
options: ListStreamOptions = DEFAULT_LIST_STREAM_OPTIONS,
113-
) {
113+
): void {
114114
if (!isValidChunkSize(options?.chunksSize)) {
115115
return emitter.emit(
116116
StreamEvent.Error,
117117
new Error(
118-
`The chunk size can't be negative. You entered the value ${options.chunksSize}`,
118+
`The chunk size can't be negative. You entered the value '${options.chunksSize}'`,
119119
),
120120
);
121121
}
122122
if (!prevList && !nextList) {
123-
return [];
123+
return emitter.emit(StreamEvent.Finish);
124124
}
125125
if (!prevList) {
126126
const nextDiff = formatSingleListStreamDiff(
@@ -176,7 +176,7 @@ function getDiffChunks<T extends Record<string, unknown>>(
176176
}
177177

178178
for (let i = 0; i < nextList.length; i++) {
179-
const data = prevList[i];
179+
const data = nextList[i];
180180
if (data) {
181181
const isValid = isDataValid(data, referenceProperty, emitter, "nextList");
182182
if (!isValid) {
@@ -210,7 +210,7 @@ function getDiffChunks<T extends Record<string, unknown>>(
210210
for (const data of listsReferences.values()) {
211211
streamedChunks++;
212212
const isLastChunk = totalChunks === streamedChunks;
213-
if (!data.nextIndex) {
213+
if (typeof data.nextIndex === "undefined") {
214214
handleDiffChunk(
215215
{
216216
previousValue: prevList[data.prevIndex],
@@ -227,7 +227,7 @@ function getDiffChunks<T extends Record<string, unknown>>(
227227
const prevData = prevList[data.prevIndex];
228228
const nextData = nextList[data.nextIndex];
229229
const isDataEqual = isEqual(prevData, nextData);
230-
const indexDiff = data.prevIndex - data.nextIndex;
230+
const indexDiff = data.nextIndex - data.prevIndex;
231231
if (isDataEqual) {
232232
if (indexDiff === 0) {
233233
handleDiffChunk(
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import { LIST_STATUS } from "@models/list";
2+
import { streamListsDiff } from ".";
3+
4+
describe("streamListsDiff data", () => {
5+
it("emits 'data' event with one object diff by chunk", (done) => {
6+
const prevList = [
7+
{ id: 1, name: "Item 1" },
8+
{ id: 2, name: "Item 2" },
9+
];
10+
const nextList = [
11+
{ id: 2, name: "Item 2" },
12+
{ id: 3, name: "Item 3" },
13+
];
14+
const diff = streamListsDiff(prevList, nextList, "id", { chunksSize: 1 });
15+
16+
const expectedChunks = [
17+
[
18+
{
19+
previousValue: null,
20+
currentValue: { id: 3, name: "Item 3" },
21+
prevIndex: null,
22+
newIndex: 1,
23+
indexDiff: null,
24+
status: LIST_STATUS.ADDED,
25+
},
26+
],
27+
[
28+
{
29+
previousValue: { id: 1, name: "Item 1" },
30+
currentValue: null,
31+
prevIndex: 0,
32+
newIndex: null,
33+
indexDiff: null,
34+
status: LIST_STATUS.DELETED,
35+
},
36+
],
37+
[
38+
{
39+
previousValue: { id: 2, name: "Item 2" },
40+
currentValue: { id: 2, name: "Item 2" },
41+
prevIndex: 1,
42+
newIndex: 0,
43+
indexDiff: -1,
44+
status: LIST_STATUS.MOVED,
45+
},
46+
],
47+
];
48+
49+
let chunkCount = 0;
50+
51+
diff.on("data", (chunk) => {
52+
expect(chunk).toStrictEqual(expectedChunks[chunkCount]);
53+
chunkCount++;
54+
});
55+
diff.on("finish", () => done());
56+
});
57+
});
58+
59+
describe("streamListsDiff finish", () => {
60+
const prevList = [
61+
{ id: 1, name: "Item 1" },
62+
{ id: 2, name: "Item 2" },
63+
];
64+
const nextList = [
65+
{ id: 2, name: "Item 2" },
66+
{ id: 3, name: "Item 3" },
67+
];
68+
it("emits 'finish' event when all the chunks have been processed", (done) => {
69+
const diff = streamListsDiff(prevList, nextList, "id");
70+
diff.on("finish", () => done());
71+
});
72+
});
73+
74+
describe("streamListsDiff error", () => {
75+
test("emits 'error' event when prevList has invalid data", (done) => {
76+
const prevList = [
77+
{ id: 1, name: "Item 1" },
78+
"hello",
79+
{ id: 2, name: "Item 2" },
80+
];
81+
const nextList = [
82+
{ id: 1, name: "Item 1" },
83+
{ id: 2, name: "Item 2" },
84+
];
85+
86+
// @ts-expect-error prevList is invalid by design for the test
87+
const diff = streamListsDiff(prevList, nextList, "id");
88+
89+
diff.on("error", (err) => {
90+
expect(err["message"]).toEqual(
91+
`Your prevList must only contain valid objects. Found 'hello'`,
92+
);
93+
done();
94+
});
95+
});
96+
97+
test("emits 'error' event when nextList has invalid data", (done) => {
98+
const prevList = [
99+
{ id: 1, name: "Item 1" },
100+
{ id: 2, name: "Item 2" },
101+
];
102+
const nextList = [
103+
{ id: 1, name: "Item 1" },
104+
"hello",
105+
{ id: 2, name: "Item 2" },
106+
];
107+
108+
// @ts-expect-error nextList is invalid by design for the test
109+
const diff = streamListsDiff(prevList, nextList, "id");
110+
111+
diff.on("error", (err) => {
112+
expect(err["message"]).toEqual(
113+
`Your nextList must only contain valid objects. Found 'hello'`,
114+
);
115+
done();
116+
});
117+
});
118+
119+
test("emits 'error' event when all prevList ojects don't have the requested reference property", (done) => {
120+
const prevList = [{ id: 1, name: "Item 1" }, { name: "Item 2" }];
121+
const nextList = [
122+
{ id: 1, name: "Item 1" },
123+
{ id: 2, name: "Item 2" },
124+
];
125+
126+
const diff = streamListsDiff(prevList, nextList, "id");
127+
128+
diff.on("error", (err) => {
129+
expect(err["message"]).toEqual(
130+
`The reference property 'id' is not available in all the objects of your prevList.`,
131+
);
132+
done();
133+
});
134+
});
135+
136+
test("emits 'error' event when all nextList ojects don't have the requested reference property", (done) => {
137+
const prevList = [
138+
{ id: 1, name: "Item 1" },
139+
{ id: 2, name: "Item 2" },
140+
];
141+
const nextList = [{ id: 1, name: "Item 1" }, { name: "Item 2" }];
142+
143+
const diff = streamListsDiff(prevList, nextList, "id");
144+
145+
diff.on("error", (err) => {
146+
expect(err["message"]).toEqual(
147+
`The reference property 'id' is not available in all the objects of your nextList.`,
148+
);
149+
done();
150+
});
151+
});
152+
153+
test("emits 'error' event when the chunkSize option is negative", (done) => {
154+
const prevList = [
155+
{ id: 1, name: "Item 1" },
156+
{ id: 2, name: "Item 2" },
157+
];
158+
const nextList = [{ id: 1, name: "Item 1" }, { name: "Item 2" }];
159+
160+
const diff = streamListsDiff(prevList, nextList, "id", { chunksSize: -3 });
161+
162+
diff.on("error", (err) => {
163+
expect(err["message"]).toEqual(
164+
"The chunk size can't be negative. You entered the value '-3'",
165+
);
166+
done();
167+
});
168+
});
169+
});

0 commit comments

Comments
 (0)