json-version-control/test/jsonDiff.test.js
2018-03-10 22:23:22 +02:00

114 lines
3.7 KiB
JavaScript

import 'babel-polyfill';
import { describe, it } from 'mocha';
import { expect } from 'chai';
import {
getJsonDiff,
checkArryTypeDiffAdd,
checkObjectTypeDiffAdd,
checkObjectDiffDelete,
checkArryTypeDiffDelete,
} from '../src/JsonDiff';
describe('Json Diff', () => {
describe('#checkObjectTypeDiffAdd', () => {
it('should return the property diff between two object', () => {
const res = [];
checkObjectTypeDiffAdd({}, { color: 'blue' }, res);
expect(res[0]).to.equal('Add .color:blue');
});
it('should return the property inside property diff between two object', () => {
const res = [];
checkObjectTypeDiffAdd({}, { color: { deep: 'blue' } }, res);
expect(res[0]).to.equal('Add .color.deep:blue');
});
it('should return multi properties diff ', () => {
const res = [];
checkObjectTypeDiffAdd({}, { color: { deep: 'blue' }, shadow: 'light' }, res);
expect(res).to.deep.equal(['Add .color.deep:blue', 'Add .shadow:light']);
});
it('should ignore properties that also exists in the old json ', () => {
const res = [];
checkObjectTypeDiffAdd(
{ shadow: 'light' },
{ color: { deep: 'blue' }, shadow: 'light' },
res,
);
expect(res).to.deep.equal(['Add .color.deep:blue']);
});
it('should not return anything when two json are the same ', () => {
const res = [];
checkObjectTypeDiffAdd(
{ color: { deep: 'blue' }, shadow: 'light' },
{ color: { deep: 'blue' }, shadow: 'light' },
res,
);
expect(res).to.deep.equal([]);
});
it('write update if value where changed ', () => {
const res = [];
checkObjectTypeDiffAdd(
{ color: { deep: 'blue' }, shadow: 'light' },
{ color: { deep: 'blue' }, shadow: 'dark' },
res,
);
expect(res).to.deep.equal(['Update .shadow:dark']);
});
});
describe('#checkObjectDiffDelete', () => {
it('write Delete if the head is ahead ', () => {
const res = [];
checkObjectDiffDelete(
{ color: { deep: 'blue' }, shadow: 'light' },
{ color: { deep: 'blue' } },
res,
);
expect(res).to.deep.equal(['Delete .shadow:light']);
});
it('should return new element in array', () => {
const res = getJsonDiff(
{ color: { deep: 'blue' }, shadow: 'light' },
{ color: { deep: 'blue' } },
);
expect(res).to.deep.equal(['Delete .shadow:light']);
});
});
describe('#checkArryTypeDiffAdd', () => {
it('should return the array diff Add', () => {
const res = [];
checkArryTypeDiffAdd([], ['1'], res);
expect(res[0]).to.equal('Add .[0]>1');
});
it('should return the array update', () => {
const res = [];
checkArryTypeDiffAdd(['3'], ['1'], res);
expect(res[0]).to.equal('Update .[0]>1');
});
it('should return the diff of a complex array', () => {
const res = [];
checkArryTypeDiffAdd([], [[1, 2], [3, 4]], res);
expect(res).to.deep.equal([
'Add .[0].[0]>1',
'Add .[0].[1]>2',
'Add .[1].[0]>3',
'Add .[1].[1]>4',
]);
});
});
describe('#checkArryTypeDiffDelete', () => {
it('should return the array diff delete', () => {
const res = [];
checkArryTypeDiffDelete(['1'], [], res);
expect(res[0]).to.equal('Delete .[0]>1');
});
it('should return the diff (Delete) of a complex array', () => {
const res = [];
checkArryTypeDiffDelete([[1, 2], [3, 4]], [], res);
expect(res).to.deep.equal([
'Delete .[0].[0]>1',
'Delete .[0].[1]>2',
'Delete .[1].[0]>3',
'Delete .[1].[1]>4',
]);
});
});
});