Browse Source

feat: add object tests

pull/1/head
antoine lanoe 2 years ago
parent
commit
e3f2ab6dcf
  1. 14
      src/list-diff.ts
  2. 2
      src/model.ts
  3. 16
      src/object-diff.ts
  4. 105
      test/list-diff.test.ts
  5. 148
      test/object-diff.test.ts
  6. 18
      test/utils.test.ts

14
src/list-diff.ts

@ -5,9 +5,9 @@ function formatSingleListDiff( @@ -5,9 +5,9 @@ function formatSingleListDiff(
listData: ListData[],
status: DiffStatus
): ListDiff {
console.log("formatsingle", listData);
return {
type: "list",
status,
diff: listData.map((data: ListData, i) => ({
value: data,
prevIndex: status === STATUS.ADDED ? null : i,
@ -18,6 +18,12 @@ function formatSingleListDiff( @@ -18,6 +18,12 @@ function formatSingleListDiff(
};
}
function getListStatus(listDiff: ListDiff["diff"]): DiffStatus {
return listDiff.some((value) => value.status !== STATUS.EQUAL)
? STATUS.UPDATED
: STATUS.EQUAL;
}
export const getListDiff = (
prevList: ListData[] | undefined | null,
nextList: ListData[] | undefined | null
@ -25,6 +31,7 @@ export const getListDiff = ( @@ -25,6 +31,7 @@ export const getListDiff = (
if (!prevList && !nextList) {
return {
type: "list",
status: STATUS.EQUAL,
diff: [],
};
}
@ -80,10 +87,7 @@ export const getListDiff = ( @@ -80,10 +87,7 @@ export const getListDiff = (
});
return {
type: "list",
status: getListStatus(diff),
diff,
};
};
export function hasListChanged(listDiff: ListDiff): boolean {
return listDiff.diff.some((d) => d.status !== STATUS.EQUAL);
}

2
src/model.ts

@ -12,6 +12,7 @@ export type ListData = any; @@ -12,6 +12,7 @@ export type ListData = any;
export type ListDiff = {
type: "list";
status: DiffStatus;
diff: {
value: ListData;
prevIndex: number | null;
@ -30,6 +31,7 @@ export type Subproperties = { @@ -30,6 +31,7 @@ export type Subproperties = {
export type ObjectDiff = {
type: "object";
status: DiffStatus;
diff: {
property: string;
previousValue: any;

16
src/object-diff.ts

@ -7,10 +7,23 @@ import { @@ -7,10 +7,23 @@ import {
} from "./model";
import { hasNestedValues, isEqual } from "./utils";
function getObjectStatus(diff: ObjectDiff["diff"]): DiffStatus {
return diff.some((property) => property.status !== STATUS.EQUAL)
? STATUS.UPDATED
: STATUS.EQUAL;
}
function formatSingleObjectDiff(
data: ObjectData,
status: DiffStatus
): ObjectDiff {
if (!data) {
return {
type: "object",
status: STATUS.isEqual,
diff: [],
};
}
const diff: ObjectDiff["diff"] = [];
Object.entries(data).forEach(([property, value]) => {
if (hasNestedValues(value)) {
@ -40,6 +53,7 @@ function formatSingleObjectDiff( @@ -40,6 +53,7 @@ function formatSingleObjectDiff(
});
return {
type: "object",
status,
diff,
};
}
@ -51,6 +65,7 @@ export function getObjectDiff( @@ -51,6 +65,7 @@ export function getObjectDiff(
if (!prevData && !nextData) {
return {
type: "object",
status: STATUS.EQUAL,
diff: [],
};
}
@ -108,6 +123,7 @@ export function getObjectDiff( @@ -108,6 +123,7 @@ export function getObjectDiff(
});
return {
type: "object",
status: getObjectStatus(diff),
diff,
};
}

105
test/list-diff.test.ts

@ -2,15 +2,18 @@ import { getListDiff } from "../src/list-diff"; @@ -2,15 +2,18 @@ import { getListDiff } from "../src/list-diff";
describe("getListDiff", () => {
it("returns an empty diff if no lists are provided", () => {
expect(getListDiff(null, null)).toStrictEqual({ type: "list", diff: [] });
expect(getListDiff(null, null)).toStrictEqual({
type: "list",
status: "equal",
diff: [],
});
});
it("consider previous list as completely deleted if no next list is provided", () => {
const res = getListDiff(["mbappe", "mendes", "verratti", "ruiz"], null);
console.log("res", JSON.stringify(res, null, 2));
expect(
getListDiff(["mbappe", "mendes", "verratti", "ruiz"], null)
).toStrictEqual({
type: "list",
status: "deleted",
diff: [
{
value: "mbappe",
@ -48,6 +51,7 @@ describe("getListDiff", () => { @@ -48,6 +51,7 @@ describe("getListDiff", () => {
getListDiff(null, ["mbappe", "mendes", "verratti", "ruiz"])
).toStrictEqual({
type: "list",
status: "added",
diff: [
{
value: "mbappe",
@ -80,7 +84,7 @@ describe("getListDiff", () => { @@ -80,7 +84,7 @@ describe("getListDiff", () => {
],
});
});
it("detects changed values in the list", () => {
it("detects changed values in a string list", () => {
expect(
getListDiff(
["mbappe", "mendes", "verratti", "ruiz"],
@ -88,6 +92,7 @@ describe("getListDiff", () => { @@ -88,6 +92,7 @@ describe("getListDiff", () => {
)
).toStrictEqual({
type: "list",
status: "updated",
diff: [
{
value: "mbappe",
@ -127,4 +132,96 @@ describe("getListDiff", () => { @@ -127,4 +132,96 @@ describe("getListDiff", () => {
],
});
});
it("detects changed values in a number list", () => {
expect(getListDiff([54, 234, 76, 0], [54, 200, 0])).toStrictEqual({
type: "list",
status: "updated",
diff: [
{
value: 54,
prevIndex: 0,
newIndex: 0,
indexDiff: 0,
status: "equal",
},
{
value: 234,
prevIndex: 1,
newIndex: null,
indexDiff: null,
status: "deleted",
},
{
value: 76,
prevIndex: 2,
newIndex: null,
indexDiff: null,
status: "deleted",
},
{
value: 200,
prevIndex: null,
newIndex: 1,
indexDiff: null,
status: "added",
},
{
value: 0,
prevIndex: 3,
newIndex: 2,
indexDiff: -1,
status: "moved",
},
],
});
});
it("detects changed values in an object list", () => {
expect(
getListDiff(
[
{ name: "joe", age: 87 },
{ name: "nina", age: 23 },
{ name: "paul", age: 32 },
],
[
{ name: "paul", age: 32 },
{ name: "joe", age: 88 },
{ name: "nina", age: 23 },
]
)
).toStrictEqual({
type: "list",
status: "updated",
diff: [
{
value: { name: "joe", age: 87 },
prevIndex: 0,
newIndex: null,
indexDiff: null,
status: "deleted",
},
{
value: { name: "paul", age: 32 },
prevIndex: 2,
newIndex: 0,
indexDiff: -2,
status: "moved",
},
{
value: { name: "joe", age: 88 },
prevIndex: null,
newIndex: 1,
indexDiff: null,
status: "added",
},
{
value: { name: "nina", age: 23 },
prevIndex: 1,
newIndex: 2,
indexDiff: 1,
status: "moved",
},
],
});
});
});

148
test/object-diff.test.ts

@ -1,7 +1,151 @@ @@ -1,7 +1,151 @@
import { getObjectDiff } from "../src/object-diff";
describe("getObjectDiff", () => {
it("", () => {
expect(getObjectDiff(null, null)).toStrictEqual(null);
it("returns an empty diff if no objects are provided", () => {
expect(getObjectDiff(null, null)).toStrictEqual({
type: "object",
status: "equal",
diff: [],
});
});
it("consider previous object as completely deleted if no next object is provided", () => {
expect(
getObjectDiff(
{ name: "joe", age: 54, hobbies: ["golf", "football"] },
null
)
).toStrictEqual({
type: "object",
status: "deleted",
diff: [
{
property: "name",
previousValue: "joe",
currentValue: undefined,
status: "deleted",
},
{
property: "age",
previousValue: 54,
currentValue: undefined,
status: "deleted",
},
{
property: "hobbies",
previousValue: ["golf", "football"],
currentValue: undefined,
status: "deleted",
},
],
});
});
it("consider next object as completely added if no previous object is provided", () => {
expect(
getObjectDiff(null, {
name: "joe",
age: 54,
hobbies: ["golf", "football"],
})
).toStrictEqual({
type: "object",
status: "added",
diff: [
{
property: "name",
previousValue: undefined,
currentValue: "joe",
status: "added",
},
{
property: "age",
previousValue: undefined,
currentValue: 54,
status: "added",
},
{
property: "hobbies",
previousValue: undefined,
currentValue: ["golf", "football"],
status: "added",
},
],
});
});
it("detects changed between two objects", () => {
expect(
getObjectDiff(
{
id: 54,
user: {
name: "joe",
member: true,
hobbies: ["golf", "football"],
age: 66,
},
},
{
id: 54,
user: {
name: "joe",
member: false,
hobbies: ["golf", "chess"],
age: 66,
},
}
)
).toStrictEqual({
type: "object",
status: "updated",
diff: [
{
property: "id",
previousValue: 54,
currentValue: 54,
status: "equal",
},
{
property: "user",
previousValue: {
name: "joe",
member: true,
hobbies: ["golf", "football"],
age: 66,
},
currentValue: {
name: "joe",
member: false,
hobbies: ["golf", "chess"],
age: 66,
},
status: "updated",
subPropertiesDiff: [
{
name: "name",
previousValue: "joe",
currentValue: "joe",
status: "equal",
},
{
name: "member",
previousValue: true,
currentValue: false,
status: "updated",
},
{
name: "hobbies",
previousValue: ["golf", "football"],
currentValue: ["golf", "chess"],
status: "updated",
},
{
name: "age",
previousValue: 66,
currentValue: 66,
status: "equal",
},
],
},
],
});
});
});

18
test/utils.test.ts

@ -1,10 +1,10 @@ @@ -1,10 +1,10 @@
import { isEqual, hasNestedValues } from "../src/utils";
// import { isEqual, hasNestedValues } from "../src/utils";
describe("isEqual", () => {
it("", () => {
expect(isEqual(null, null)).toStrictEqual(null);
});
it("", () => {
expect(hasNestedValues(null)).toStrictEqual(null);
});
});
// describe("isEqual", () => {
// it("", () => {
// expect(isEqual(null, null)).toStrictEqual(null);
// });
// it("", () => {
// expect(hasNestedValues(null)).toStrictEqual(null);
// });
// });

Loading…
Cancel
Save