2015-07-31 16:16:19 +00:00
|
|
|
import { expect } from 'chai';
|
2015-08-01 16:09:52 +00:00
|
|
|
import u from '../lib';
|
2015-07-31 15:53:25 +00:00
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
describe('updeep', () => {
|
|
|
|
it('does not change anything if no updates are specified', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const obj = { foo: 3, bar: [7, 5] };
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({}, obj);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.equal(obj);
|
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can update with fixed values', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const obj = { foo: 3, bar: [7, 5] };
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({ foo: 4 }, obj);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.deep.equal({ foo: 4, bar: [7, 5] });
|
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('returns the same instance if an update doesn\'t make changes', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const obj = { foo: 3 };
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({ foo: 3 }, obj);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.equal(obj);
|
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can update a nested structure', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const obj = { foo: { bar: 7, bam: 3 }, baz: 32 };
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({ foo: { bar: 8 } }, obj);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 });
|
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can update arrays', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const obj = [1, 2, 3];
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({ 1: 7 }, obj);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.deep.equal([1, 7, 3]);
|
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can add an element to an array', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const obj = [];
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({ 0: 3 }, obj);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.eql([3]);
|
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can update nested arrays', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const obj = { foo: [1, 2, 3], bar: 9 };
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({ foo: { 1: 7 } }, obj);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 });
|
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can use functions to update values', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const inc = (i) => i + 1;
|
|
|
|
const obj = { foo: 3, bar: 4, baz: 7 };
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({ foo: inc, bar: inc }, obj);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 });
|
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('is curryable', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const inc = (i) => i + 1;
|
|
|
|
const obj = { foo: 3 };
|
2015-08-01 16:09:52 +00:00
|
|
|
const incFoo = u({ foo: inc });
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
const result = incFoo(obj);
|
|
|
|
|
|
|
|
expect(result).to.deep.equal({ foo: 4 });
|
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can update if the value is an array', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const obj = {};
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({ foo: [0, 1] }, obj);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.deep.equal({ foo: [0, 1] });
|
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can use withDefault to default things', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const obj = {};
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({
|
2015-08-02 06:25:08 +00:00
|
|
|
foo: u.withDefault([], { 0: 'bar' }),
|
2015-07-31 16:16:19 +00:00
|
|
|
}, obj);
|
|
|
|
|
|
|
|
expect(result).to.eql({ foo: ['bar'] });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can update when original object is undefined', () => {
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u({ foo: [0, 1] }, undefined);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.deep.equal({ foo: [0, 1] });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can take a function as the updater', () => {
|
2015-08-01 16:09:52 +00:00
|
|
|
const result = u(i => i + 1, 7);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
|
|
|
expect(result).to.eql(8);
|
2015-07-31 15:53:25 +00:00
|
|
|
});
|
2015-08-01 16:09:52 +00:00
|
|
|
|
|
|
|
it('can omit a key', () => {
|
|
|
|
const result = u({ foo: u.omit('bar') }, { foo: { bar: 7 } });
|
|
|
|
|
|
|
|
expect(result).to.eql({ foo: {} });
|
|
|
|
});
|
2015-07-31 15:53:25 +00:00
|
|
|
});
|