UNPKG

1.59 kBJavaScriptView Raw
1// Original source: https://gist.github.com/plukevdh/dec4b41d5b7d67f83be630afecee499e
2
3import * as R from 'ramda';
4import {objectDiff, prettyPrintObjectDiff} from './diffHelpers';
5
6const lhs = {
7 one: 1,
8 two: [1, 2],
9 three: {more: 'items', over: 'here'},
10 four: 'four',
11 five: 5
12};
13
14const rhs = {
15 one: 1,
16 two: [1, 3],
17 three: {more: 'robots', over: 'here'},
18 four: 4,
19 six: 6
20};
21
22describe('deep diffing', () => {
23 const diff = objectDiff(null, lhs, rhs);
24 const diffLabeled = objectDiff(['__v1', '__v2'], lhs, rhs);
25
26 it('only detects changed', () => {
27 expect(R.all(k => R.prop(k, diff), ['two', 'three', 'four', 'five', 'six'])).toEqual(true);
28 });
29
30 it('diffs arrays by returning the full array of items', () => {
31 expect(diff.two).toEqual({1: {__left: 2, __right: 3}});
32 });
33
34 it('diffs objects by returning only changed items', () => {
35 expect(diff.three).toEqual({more: {__left: 'items', __right: 'robots'}});
36 });
37
38 it('detects plain value differences', () => {
39 expect(diff.four).toEqual({__left: 'four', __right: 4});
40 });
41
42 it('detects removed values', () => {
43 expect(diff.five).toEqual([5, undefined]); // eslint-disable-line no-undefined
44 });
45
46 it('detects added values', () => {
47 expect(diff.six).toEqual([undefined, 6]); // eslint-disable-line no-undefined
48 });
49
50 it('detects plain value differences with labels versions', () => {
51 expect(diffLabeled.four).toEqual({__v1: 'four', __v2: 4});
52 });
53
54 it('pretty print', () => {
55 expect(prettyPrintObjectDiff(null, lhs, rhs)).toEqual(JSON.stringify(diff, null, 2));
56 });
57});
58