-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
177 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import { LIST_STATUS } from "@models/list"; | ||
import { streamListsDiff } from "."; | ||
|
||
describe("streamListsDiff data", () => { | ||
it("emits 'data' event with one object diff by chunk", (done) => { | ||
const prevList = [ | ||
{ id: 1, name: "Item 1" }, | ||
{ id: 2, name: "Item 2" }, | ||
]; | ||
const nextList = [ | ||
{ id: 2, name: "Item 2" }, | ||
{ id: 3, name: "Item 3" }, | ||
]; | ||
const diff = streamListsDiff(prevList, nextList, "id", { chunksSize: 1 }); | ||
|
||
const expectedChunks = [ | ||
[ | ||
{ | ||
previousValue: null, | ||
currentValue: { id: 3, name: "Item 3" }, | ||
prevIndex: null, | ||
newIndex: 1, | ||
indexDiff: null, | ||
status: LIST_STATUS.ADDED, | ||
}, | ||
], | ||
[ | ||
{ | ||
previousValue: { id: 1, name: "Item 1" }, | ||
currentValue: null, | ||
prevIndex: 0, | ||
newIndex: null, | ||
indexDiff: null, | ||
status: LIST_STATUS.DELETED, | ||
}, | ||
], | ||
[ | ||
{ | ||
previousValue: { id: 2, name: "Item 2" }, | ||
currentValue: { id: 2, name: "Item 2" }, | ||
prevIndex: 1, | ||
newIndex: 0, | ||
indexDiff: -1, | ||
status: LIST_STATUS.MOVED, | ||
}, | ||
], | ||
]; | ||
|
||
let chunkCount = 0; | ||
|
||
diff.on("data", (chunk) => { | ||
expect(chunk).toStrictEqual(expectedChunks[chunkCount]); | ||
chunkCount++; | ||
}); | ||
diff.on("finish", () => done()); | ||
}); | ||
}); | ||
|
||
describe("streamListsDiff finish", () => { | ||
const prevList = [ | ||
{ id: 1, name: "Item 1" }, | ||
{ id: 2, name: "Item 2" }, | ||
]; | ||
const nextList = [ | ||
{ id: 2, name: "Item 2" }, | ||
{ id: 3, name: "Item 3" }, | ||
]; | ||
it("emits 'finish' event when all the chunks have been processed", (done) => { | ||
const diff = streamListsDiff(prevList, nextList, "id"); | ||
diff.on("finish", () => done()); | ||
}); | ||
}); | ||
|
||
describe("streamListsDiff error", () => { | ||
test("emits 'error' event when prevList has invalid data", (done) => { | ||
const prevList = [ | ||
{ id: 1, name: "Item 1" }, | ||
"hello", | ||
{ id: 2, name: "Item 2" }, | ||
]; | ||
const nextList = [ | ||
{ id: 1, name: "Item 1" }, | ||
{ id: 2, name: "Item 2" }, | ||
]; | ||
|
||
// @ts-expect-error prevList is invalid by design for the test | ||
const diff = streamListsDiff(prevList, nextList, "id"); | ||
|
||
diff.on("error", (err) => { | ||
expect(err["message"]).toEqual( | ||
`Your prevList must only contain valid objects. Found 'hello'`, | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
test("emits 'error' event when nextList has invalid data", (done) => { | ||
const prevList = [ | ||
{ id: 1, name: "Item 1" }, | ||
{ id: 2, name: "Item 2" }, | ||
]; | ||
const nextList = [ | ||
{ id: 1, name: "Item 1" }, | ||
"hello", | ||
{ id: 2, name: "Item 2" }, | ||
]; | ||
|
||
// @ts-expect-error nextList is invalid by design for the test | ||
const diff = streamListsDiff(prevList, nextList, "id"); | ||
|
||
diff.on("error", (err) => { | ||
expect(err["message"]).toEqual( | ||
`Your nextList must only contain valid objects. Found 'hello'`, | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
test("emits 'error' event when all prevList ojects don't have the requested reference property", (done) => { | ||
const prevList = [{ id: 1, name: "Item 1" }, { name: "Item 2" }]; | ||
const nextList = [ | ||
{ id: 1, name: "Item 1" }, | ||
{ id: 2, name: "Item 2" }, | ||
]; | ||
|
||
const diff = streamListsDiff(prevList, nextList, "id"); | ||
|
||
diff.on("error", (err) => { | ||
expect(err["message"]).toEqual( | ||
`The reference property 'id' is not available in all the objects of your prevList.`, | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
test("emits 'error' event when all nextList ojects don't have the requested reference property", (done) => { | ||
const prevList = [ | ||
{ id: 1, name: "Item 1" }, | ||
{ id: 2, name: "Item 2" }, | ||
]; | ||
const nextList = [{ id: 1, name: "Item 1" }, { name: "Item 2" }]; | ||
|
||
const diff = streamListsDiff(prevList, nextList, "id"); | ||
|
||
diff.on("error", (err) => { | ||
expect(err["message"]).toEqual( | ||
`The reference property 'id' is not available in all the objects of your nextList.`, | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
test("emits 'error' event when the chunkSize option is negative", (done) => { | ||
const prevList = [ | ||
{ id: 1, name: "Item 1" }, | ||
{ id: 2, name: "Item 2" }, | ||
]; | ||
const nextList = [{ id: 1, name: "Item 1" }, { name: "Item 2" }]; | ||
|
||
const diff = streamListsDiff(prevList, nextList, "id", { chunksSize: -3 }); | ||
|
||
diff.on("error", (err) => { | ||
expect(err["message"]).toEqual( | ||
"The chunk size can't be negative. You entered the value '-3'", | ||
); | ||
done(); | ||
}); | ||
}); | ||
}); |