2021-04-12 13:32:25 +00:00
|
|
|
import deepDifference from "utilities/deep_difference";
|
2017-01-17 22:45:07 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("deepDifference - utility", () => {
|
|
|
|
|
it("returns the difference for 2 un-nested objects", () => {
|
|
|
|
|
const obj1 = { id: 1, first_name: "Joe", last_name: "Smith" };
|
|
|
|
|
const obj2 = { id: 1, first_name: "Joe", last_name: "Smyth" };
|
2017-01-17 22:45:07 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
expect(deepDifference(obj1, obj2)).toEqual({ last_name: "Smith" });
|
|
|
|
|
expect(deepDifference(obj2, obj1)).toEqual({ last_name: "Smyth" });
|
2017-01-17 22:45:07 +00:00
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("returns the difference for 2 nested objects", () => {
|
2017-01-17 22:45:07 +00:00
|
|
|
const obj1 = {
|
2021-04-12 13:32:25 +00:00
|
|
|
profile: { id: 1, first_name: "Joe", last_name: "Smith" },
|
2017-01-17 22:45:07 +00:00
|
|
|
preferences: { email: true, push: false },
|
2017-01-18 17:10:37 +00:00
|
|
|
post_ids: [1, 2, 3],
|
2017-01-17 22:45:07 +00:00
|
|
|
};
|
|
|
|
|
const obj2 = {
|
2021-04-12 13:32:25 +00:00
|
|
|
profile: { id: 1, first_name: "Joe", last_name: "Smyth" },
|
2017-01-17 22:45:07 +00:00
|
|
|
preferences: { email: false, push: false },
|
2017-01-18 17:10:37 +00:00
|
|
|
post_ids: [1, 3],
|
2017-01-17 22:45:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
expect(deepDifference(obj1, obj2)).toEqual({
|
2021-04-12 13:32:25 +00:00
|
|
|
profile: { last_name: "Smith" },
|
2017-01-17 22:45:07 +00:00
|
|
|
preferences: { email: true },
|
2017-01-18 17:10:37 +00:00
|
|
|
post_ids: [2],
|
2017-01-17 22:45:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(deepDifference(obj2, obj1)).toEqual({
|
2021-04-12 13:32:25 +00:00
|
|
|
profile: { last_name: "Smyth" },
|
2017-01-17 22:45:07 +00:00
|
|
|
preferences: { email: false },
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("returns the difference for 1 nested object and 1 non-nested object", () => {
|
|
|
|
|
const obj1 = {
|
|
|
|
|
profile: { id: 1, first_name: "Joe", last_name: "Smith" },
|
|
|
|
|
preferences: { email: true, push: false },
|
|
|
|
|
post_ids: [1, 2, 3],
|
|
|
|
|
};
|
|
|
|
|
const obj2 = { profile: "my profile", preferences: "my preferences" };
|
|
|
|
|
|
|
|
|
|
expect(deepDifference(obj1, obj2)).toEqual(obj1);
|
|
|
|
|
expect(deepDifference(obj2, obj1)).toEqual(obj2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns an empty array when comparing an empty array against a non-empty array", () => {
|
|
|
|
|
const obj1 = { pack_name: "My Pack", label_ids: [] };
|
|
|
|
|
const obj2 = { pack_name: "My Pack", label_ids: [1, 2] };
|
|
|
|
|
|
|
|
|
|
expect(deepDifference(obj1, obj2)).toEqual({ label_ids: [] });
|
|
|
|
|
});
|
2017-01-17 22:45:07 +00:00
|
|
|
});
|