Skip to content

Commit

Permalink
[ci skip] adding tests/doc around update
Browse files Browse the repository at this point in the history
  • Loading branch information
selfcontained committed Jun 24, 2014
1 parent a12fe11 commit d9ae4da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ deap.merge(a, { name: 'Jack', age: 26, phone: '555-555-5555', address: { number:
### deap.update()
Deep update. Fill an object's existing properties from another object.
Deep update. Fill an object's existing properties from another object. For nested objects, a deep update will only update existing properties. Shallow updates will replace nested objects entirely.
Takes *n* number of arguments, modifies the first argument and returns it.
Expand Down
31 changes: 31 additions & 0 deletions test/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ describe('shallow update', function() {
assert.isUndefined(a.grr);
});

it('should replace a top level property with an multi-level object', function() {
var a = { burp: 'adurp' },
b = { burp: { foo: 'bar', biz: { baz: 'buz', zing: 'zoing' } } };

var result = shallowUpdate(a, b);

assert.deepEqual(result, a);
assert.deepEqual(result.burp, b.burp);
});

it('should replace a top level object with a string', function() {
var a = { burp: { foo: 'bar' } },
b = { burp: 'adurp' };

var result = shallowUpdate(a, b);

assert.deepEqual(result, a);
assert.deepEqual(result.burp, b.burp);
});

});

describe('deep update', function() {
Expand Down Expand Up @@ -133,4 +153,15 @@ describe('deep update', function() {
assert.notStrictEqual(result.nested[0], deeper);
});

it('should only replace existing properties in nested objects', function() {
var a = { burp: { thing: 'thang', biz: { burp: 'adurp' } } },
b = { burp: { foo: 'bar', biz: { burp: 'boop', baz: 'buz', zing: 'zoing' } } };

var result = deepUpdate(a, b);

assert.deepEqual(result, a);
assert.deepEqual(result.burp.thing, 'thang');
assert.deepEqual(result.burp.biz.burp, 'boop');
});

});

0 comments on commit d9ae4da

Please sign in to comment.