-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve test coverage for merge utilities
- Loading branch information
Showing
5 changed files
with
221 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
import {DELETE_KEY} from "./types.js"; | ||
import apply from "./apply.js"; | ||
|
||
describe("merge.apply()", function(){ | ||
it("merges a no-op", function(){ | ||
const a = {a:1}; | ||
expect(apply(a, {})).to.deep.equal(a); | ||
}); | ||
|
||
it("merges a deleted key", function(){ | ||
expect(apply({a:1, b:2}, {b:DELETE_KEY})).to.deep.equal({a:1}); | ||
}); | ||
|
||
it("merges an updated key", function(){ | ||
expect(apply({a:1, b:2}, {b:3})).to.deep.equal({a:1, b:3}); | ||
}); | ||
it("merges nested objects", function(){ | ||
expect(apply({a:{b:1}, c:3}, {a:{b:2}})).to.deep.equal({a:{b:2}, c:3}); | ||
}); | ||
it("merges updated arrays", function(){ | ||
expect(apply({a:[1,2]}, {a:{0:1, 1:3}})).to.deep.equal({a:[1,3]}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
import {DELETE_KEY} from "./types.js"; | ||
|
||
import diff from "./diff.js"; | ||
|
||
describe("merge.diff()", function(){ | ||
describe("handle native data types ", function(){ | ||
[ //All JSON-serializable data types with possible bounday values | ||
1, | ||
0, | ||
"1", | ||
"", | ||
true, | ||
false, | ||
null, | ||
{}, | ||
{a:1}, | ||
[], | ||
].forEach(v=>{ | ||
const v_clone :typeof v = JSON.parse(JSON.stringify(v)); | ||
it(`${JSON.stringify(v)} deep equals itself`, function(){ | ||
expect(diff({v}, {v:v_clone})).to.deep.equal({}); | ||
}); | ||
it(`${JSON.stringify({v})} compares from {}`, function(){ | ||
expect(diff({}, {v})).to.deep.equal({v:v_clone}); | ||
}); | ||
it(`${JSON.stringify({v})} compares from {}`, function(){ | ||
expect(diff({v}, {})).to.deep.equal({v:DELETE_KEY}); | ||
}); | ||
}); | ||
it("special-case for null recursion", function(){ | ||
expect(diff({ | ||
v: {a:1}, | ||
}, {v: null})) | ||
}) | ||
}); | ||
|
||
describe("arrays", function(){ | ||
//Arrays behave like objects with numeric keys, with some specific handling | ||
|
||
it("handles an array equality", function(){ | ||
expect(diff({a:["foo"]}, {a:["foo"]})).to.deep.equal({}); | ||
}); | ||
|
||
it("handles nested objects equality", function(){ | ||
expect(diff({a:[{v: "foo"}]}, {a:[{v:"foo"}]})).to.deep.equal({}); | ||
}); | ||
|
||
it("handle array.push", function(){ | ||
expect(diff({a:[]}, {a:[1]})).to.deep.equal({a:{0:1}}); | ||
expect(diff({a:[1]}, {a:[1, 2]})).to.deep.equal({a:{1:2}}); | ||
}); | ||
|
||
it("handle Array.pop", function(){ | ||
expect(diff({a:[1]}, {a:[]})).to.deep.equal({a:{0: DELETE_KEY}}); | ||
expect(diff({a:[1, 2]}, {a:[1]})).to.deep.equal({a:{1: DELETE_KEY}}); | ||
}); | ||
|
||
it("handle Array.replace", function(){ | ||
expect(diff({a:["foo", "bar"]}, {a:["foo", "baz"]})).to.deep.equal({a:{1: "baz"}}); | ||
}); | ||
|
||
it("handle Array.splice", function(){ | ||
// Might be improved to detect a splice and use special syntax to represent it | ||
expect(diff({a:["foo", "bar", "baz"]}, {a:["foo", "baz"]})).to.deep.equal({a:{1: "baz", 2: DELETE_KEY}}); | ||
}); | ||
|
||
it("handle deep changes", function(){ | ||
expect(diff({a:[{v:"foo"}, {v:"bar"}]}, {a:[{v:"foo"}, {v:"baz"}]})).to.deep.equal({a:{1:{v:"baz"}}}); | ||
}); | ||
|
||
it("throws when diffing an array with an object", function(){ | ||
expect(()=>diff({a:[]}, {a:{}})).to.throw("Can't diff an array with an object"); | ||
}); | ||
|
||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.