add settingsdiff tests

improve settingdiff
fix setting.test.js
indiachan-spamvector
Thomas Lynch 2 years ago
parent 5a41cf1bfa
commit 23356f9d9d
  1. 8
      helpers/setting.test.js
  2. 13
      helpers/settingsdiff.js
  3. 59
      helpers/settingsdiff.test.js

@ -9,7 +9,7 @@ describe('trimSetting, numberSetting, booleanSetting, arraySetting', () => {
{ in: null, out: 'OLDSETTING' },
];
for(let i in trimCases) {
test(`should output ${trimCases[i].out} for an input of ${trimCases[i].in}`, () => {
test(`trimSetting should output ${trimCases[i].out} for an input of ${trimCases[i].in}`, () => {
expect(trimSetting(trimCases[i].in, 'OLDSETTING')).toStrictEqual(trimCases[i].out);
});
}
@ -23,7 +23,7 @@ describe('trimSetting, numberSetting, booleanSetting, arraySetting', () => {
{ in: 'string', out: 'OLDSETTING' },
];
for(let i in numberCases) {
test(`should output ${numberCases[i].out} for an input of ${numberCases[i].in}`, () => {
test(`numberSetting should output ${numberCases[i].out} for an input of ${numberCases[i].in}`, () => {
expect(numberSetting(numberCases[i].in, 'OLDSETTING')).toStrictEqual(numberCases[i].out);
});
}
@ -38,7 +38,7 @@ describe('trimSetting, numberSetting, booleanSetting, arraySetting', () => {
{ in: [1], out: true },
];
for(let i in booleanCases) {
test(`should output ${booleanCases[i].out} for an input of ${booleanCases[i].in}`, () => {
test(`booleanSetting should output ${booleanCases[i].out} for an input of ${booleanCases[i].in}`, () => {
expect(booleanSetting(booleanCases[i].in)).toStrictEqual(booleanCases[i].out);
});
}
@ -61,7 +61,7 @@ describe('trimSetting, numberSetting, booleanSetting, arraySetting', () => {
xxx`, out: [' hello ', '123', 'xxx'] },
];
for(let i in arrayCases) {
test(`should output ${arrayCases[i].out} for an input of ${arrayCases[i].in}`, () => {
test(`arraySetting should output ${arrayCases[i].out} for an input of ${arrayCases[i].in}`, () => {
expect(arraySetting(arrayCases[i].in, 'OLDSETTING', 10)).toStrictEqual(arrayCases[i].out);
});
}

@ -1,15 +1,20 @@
'use strict';
const { isDeepStrictEqual } = require('util')
const { isDeepStrictEqual } = require('util');
function getDotProp(obj, prop) {
return prop
.split('.')
.reduce((a, b) => a[b], obj);
.reduce((a, b) => {
if (a && a[b]) {
return a[b];
}
return null;
}, obj);
}
function includeChildren(template, prop, tasks) {
return Object.keys(getDotProp(template, prop))
return Object.keys(getDotProp(template, prop) || {})
.reduce((a, x) => {
a[`${prop}.${x}`] = tasks;
return a;
@ -24,7 +29,7 @@ function compareSettings(entries, oldObject, newObject, maxSetSize) {
if (!isDeepStrictEqual(oldValue, newValue)) {
entry[1].forEach(t => resultSet.add(t));
}
return resultSet.size < maxSetSize;
return resultSet.size <= maxSetSize;
});
return resultSet;
}

@ -0,0 +1,59 @@
const { getDotProp, includeChildren, compareSettings } = require('./settingsdiff.js');
describe('getDotProp, includeChildren, compareSettings in settingsdiff', () => {
const getDotPropCases = [
{ in: { object: {a:{b:{c:1}}}, prop: 'a.b.c' }, out: 1 },
{ in: { object: {a:null}, prop: 'a.b.c' }, out: null },
{ in: { object: {}, prop: 'a.b.c' }, out: null },
];
for(let i in getDotPropCases) {
test(`getDotProp should output ${getDotPropCases[i].out} for an input of ${getDotPropCases[i].in}`, () => {
expect(getDotProp(getDotPropCases[i].in.object, getDotPropCases[i].in.prop)).toStrictEqual(getDotPropCases[i].out);
});
}
const includeChildrenCases = [
{ in: { object: {a:{b:1,c:2,d:3},a2:{b:'notme'}}, prop: 'a' }, out: {'a.b':['example'], 'a.c':['example'], 'a.d':['example']} },
{ in: { object: null, prop: 'a' }, out: {} },
{ in: { object: {a:null}, prop: 'a' }, out: {} },
];
for(let i in includeChildrenCases) {
test(`includeChildren should output ${includeChildrenCases[i].out} for an input of ${includeChildrenCases[i].in}`, () => {
expect(includeChildren(includeChildrenCases[i].in.object, includeChildrenCases[i].in.prop, ['example'])).toStrictEqual(includeChildrenCases[i].out);
});
}
const compareSettingsCases = [
{
in: {
entries: [['a.b.c',['1']]],
old: {a:{b:{c:1,d:2}}},
new: {a:{b:{c:1,d:2}}},
},
out: new Set(),
},
{
in: {
entries: [['a.b.c',['1']]],
old: {a:{b:{c:1,d:2}}},
new: {a:{b:{c:1,d:3}}},
},
out: new Set(),
},
{
in: {
entries: [['a.b.c',['1']]],
old: {a:{b:{c:1,d:2}}},
new: {a:{b:{c:2,d:2}}},
},
out: new Set(['1']),
},
];
for(let i in compareSettingsCases) {
test(`compareSettings should output ${compareSettingsCases[i].out} for an input of ${compareSettingsCases[i].in}`, () => {
expect(compareSettings(compareSettingsCases[i].in.entries, compareSettingsCases[i].in.old, compareSettingsCases[i].in.new, 4)).toStrictEqual(compareSettingsCases[i].out);
});
}
});
Loading…
Cancel
Save