Skip to content

Commit

Permalink
chore: test stream-list-diff
Browse files Browse the repository at this point in the history
  • Loading branch information
DoneDeal0 committed Oct 5, 2024
1 parent aaf5f3f commit aeccae1
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/lib/stream-list-diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function isDataValid<T extends Record<string, unknown>>(
emitter.emit(
StreamEvent.Error,
new Error(
`Your ${listType} must only contain valid objects. Found ${data}`,
`Your ${listType} must only contain valid objects. Found '${data}'`,
),
);
return false;
Expand All @@ -96,7 +96,7 @@ function isDataValid<T extends Record<string, unknown>>(
emitter.emit(
StreamEvent.Error,
new Error(
`The reference property ${String(referenceProperty)} is not available in all the objects of your ${listType}.`,
`The reference property '${String(referenceProperty)}' is not available in all the objects of your ${listType}.`,
),
);
return false;
Expand All @@ -110,17 +110,17 @@ function getDiffChunks<T extends Record<string, unknown>>(
referenceProperty: ReferenceProperty<T>,
emitter: Emitter<T>,
options: ListStreamOptions = DEFAULT_LIST_STREAM_OPTIONS,
) {
): void {
if (!isValidChunkSize(options?.chunksSize)) {
return emitter.emit(
StreamEvent.Error,
new Error(
`The chunk size can't be negative. You entered the value ${options.chunksSize}`,
`The chunk size can't be negative. You entered the value '${options.chunksSize}'`,
),
);
}
if (!prevList && !nextList) {
return [];
return emitter.emit(StreamEvent.Finish);
}
if (!prevList) {
const nextDiff = formatSingleListStreamDiff(
Expand Down Expand Up @@ -176,7 +176,7 @@ function getDiffChunks<T extends Record<string, unknown>>(
}

for (let i = 0; i < nextList.length; i++) {
const data = prevList[i];
const data = nextList[i];
if (data) {
const isValid = isDataValid(data, referenceProperty, emitter, "nextList");
if (!isValid) {
Expand Down Expand Up @@ -210,7 +210,7 @@ function getDiffChunks<T extends Record<string, unknown>>(
for (const data of listsReferences.values()) {
streamedChunks++;
const isLastChunk = totalChunks === streamedChunks;
if (!data.nextIndex) {
if (typeof data.nextIndex === "undefined") {
handleDiffChunk(
{
previousValue: prevList[data.prevIndex],
Expand All @@ -227,7 +227,7 @@ function getDiffChunks<T extends Record<string, unknown>>(
const prevData = prevList[data.prevIndex];
const nextData = nextList[data.nextIndex];
const isDataEqual = isEqual(prevData, nextData);
const indexDiff = data.prevIndex - data.nextIndex;
const indexDiff = data.nextIndex - data.prevIndex;
if (isDataEqual) {
if (indexDiff === 0) {
handleDiffChunk(
Expand Down
169 changes: 169 additions & 0 deletions src/lib/stream-list-diff/stream-list-diff.test.ts
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();
});
});
});

0 comments on commit aeccae1

Please sign in to comment.