Skip to content

Commit d828163

Browse files
committed
add random tests for arrays
1 parent 195cba6 commit d828163

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

test/spec/duplexSpec.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,36 @@ function variantIt(name, variants, fn) {
6464
});
6565
}
6666

67+
68+
function randomArray(maxSize, maxValue) {
69+
const size = Math.round(Math.random() * maxSize)
70+
const arr = [];
71+
for (let i = 0; i < size; i++) {
72+
arr.push(Math.round(Math.random() * maxValue))
73+
}
74+
return arr;
75+
}
76+
77+
function createTest(counter) {
78+
// create a random arrays with random sizes and max value.
79+
const arr1 = randomArray(20, 20);
80+
const arr2 = randomArray(20, 20);
81+
const testName = JSON.stringify(arr1) + " VS " + JSON.stringify(arr2);
82+
it(counter + ". " + testName, () => {
83+
const arr3 = JSON.parse(JSON.stringify(arr1));
84+
const patches = jsonpatch.compare(arr1, arr2);
85+
jsonpatch.applyPatch(arr1, patches);
86+
expect(arr2).toReallyEqual(arr1);
87+
})
88+
}
89+
90+
function nIt(n) {
91+
for (let i = 1; i <= n; i++) {
92+
createTest(i);
93+
}
94+
95+
}
96+
6797
function insertIf(condition, ...elements) {
6898
return condition ? elements : [];
6999
}
@@ -1658,6 +1688,24 @@ describe('duplex', function() {
16581688
]);
16591689
}
16601690
});
1691+
1692+
1693+
it('Diff array should remove not replace', function () {
1694+
const obj1 = { id: 1 };
1695+
const obj2 = { id: 2 };
1696+
const obj3 = { id: 3 };
1697+
const objA = {
1698+
arr: [obj1, obj2, obj3]
1699+
};
1700+
const objB = { arr: objA.arr.filter(o => o.id !== 1) }
1701+
const patches = jsonpatch.compare(objA, objB);
1702+
expect(patches).toEqual([
1703+
{
1704+
op: "remove", path: '/arr/0'
1705+
}
1706+
])
1707+
})
1708+
16611709
variantIt('Replacing an array with an object should be handled well', [
16621710
['invertible = FALSE', false],
16631711
['invertible = TRUE', true]
@@ -1993,6 +2041,18 @@ describe('duplex', function() {
19932041
});
19942042
});
19952043

2044+
describe('random', function() {
2045+
it('found a', () => {
2046+
const arr1 = [ 2, 10, 14, 2, 13];
2047+
const arr2 = [ 8, 2, 19];
2048+
const arr3 = JSON.parse(JSON.stringify(arr1));
2049+
const patches = jsonpatch.compare(arr1, arr2);
2050+
jsonpatch.applyPatch(arr1, patches);
2051+
expect(arr2).toReallyEqual(arr1);
2052+
});
2053+
nIt(1000);
2054+
});
2055+
19962056
describe('compare', function() {
19972057
it('should return patch difference between objects', function() {
19982058
const obj = {

0 commit comments

Comments
 (0)