2015-08-19 07:23:18 +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-08-05 07:27:56 +00:00
|
|
|
const object = { foo: 3, bar: [7, 5] };
|
|
|
|
const result = u({}, object);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.equal(object);
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can update with fixed values', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { foo: 3, bar: [7, 5] };
|
|
|
|
const result = u({ foo: 4 }, object);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.deep.equal({ foo: 4, bar: [7, 5] });
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('returns the same instance if an update doesn\'t make changes', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { foo: 3 };
|
|
|
|
const result = u({ foo: 3 }, object);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.equal(object);
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can update a nested structure', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { foo: { bar: 7, bam: 3 }, baz: 32 };
|
|
|
|
const result = u({ foo: { bar: 8 } }, object);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 });
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can update arrays', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = [1, 2, 3];
|
|
|
|
const result = u({ 1: 7 }, object);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.deep.equal([1, 7, 3]);
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
2015-08-05 11:28:32 +00:00
|
|
|
it('replaces the object outright if updates are a constant', () => {
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(u(3, {})).to.equal(3);
|
|
|
|
expect(u(null, {})).to.be.null;
|
2015-08-05 11:28:32 +00:00
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can add an element to an array', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = [];
|
|
|
|
const result = u({ 0: 3 }, object);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql([3]);
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
2015-08-01 15:13:25 +00:00
|
|
|
it('can update nested arrays', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { foo: [1, 2, 3], bar: 9 };
|
|
|
|
const result = u({ foo: { 1: 7 } }, object);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 });
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
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;
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { foo: 3, bar: 4, baz: 7 };
|
|
|
|
const result = u({ foo: inc, bar: inc }, object);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 });
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
2015-08-02 07:32:32 +00:00
|
|
|
it('can be partially applied', () => {
|
2015-07-31 16:16:19 +00:00
|
|
|
const inc = (i) => i + 1;
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { foo: 3 };
|
2015-08-01 16:09:52 +00:00
|
|
|
const incFoo = u({ foo: inc });
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-05 07:27:56 +00:00
|
|
|
const result = incFoo(object);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.deep.equal({ foo: 4 });
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
2015-08-05 11:29:30 +00:00
|
|
|
it('passes additional arguments on to updates if it is a function', () => {
|
|
|
|
const func = (_, x) => x;
|
|
|
|
const result = u(func, 0, 4);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.equal(4);
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
2015-08-05 11:29:30 +00:00
|
|
|
it('can update if the value is an array', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = {};
|
2015-08-05 11:29:30 +00:00
|
|
|
const result = u({ foo: [0, 1] }, object);
|
2015-07-31 16:16:19 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.deep.equal({ foo: [0, 1] });
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.deep.equal({ foo: [0, 1] });
|
2015-07-31 16:16:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql(8);
|
2015-07-31 15:53:25 +00:00
|
|
|
});
|
2015-08-01 16:09:52 +00:00
|
|
|
|
2015-08-02 07:32:32 +00:00
|
|
|
it('deeply freezes the result', () => {
|
|
|
|
const result = u({ foo: { bar: 3 } }, { foo: { bar: 0 } });
|
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(Object.isFrozen(result)).to.be.true;
|
|
|
|
expect(Object.isFrozen(result.foo)).to.be.true;
|
2015-08-02 07:32:32 +00:00
|
|
|
});
|
2015-08-05 04:33:27 +00:00
|
|
|
|
|
|
|
it('assigns null values', () => {
|
2016-01-13 16:19:43 +00:00
|
|
|
expect(u({ isNull: null }, {})).to.eql({ isNull: null });
|
2015-08-05 04:33:27 +00:00
|
|
|
});
|
2015-08-12 15:35:10 +00:00
|
|
|
|
|
|
|
it('can use a placeholder to partially apply', () => {
|
|
|
|
function increment(i) { return i + 1; }
|
|
|
|
const updateJoe = u(u._, { name: 'Joe Merrill', age: 21 });
|
|
|
|
const result = updateJoe({ age: increment });
|
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql({ name: 'Joe Merrill', age: 22 });
|
2015-08-12 15:35:10 +00:00
|
|
|
});
|
2015-08-13 05:04:31 +00:00
|
|
|
|
|
|
|
it('defaults to an empty object when null or undefined', () => {
|
|
|
|
let result = u({ a: { b: 0 } }, { a: null });
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql({ a: { b: 0 } });
|
2015-08-13 05:04:31 +00:00
|
|
|
|
|
|
|
result = u({ a: { b: 0 } }, { a: undefined });
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql({ a: { b: 0 } });
|
2015-08-13 05:04:31 +00:00
|
|
|
|
|
|
|
result = u({ a: { b: 0 } }, { });
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql({ a: { b: 0 } });
|
2015-08-13 05:04:31 +00:00
|
|
|
});
|
2015-08-18 11:28:19 +00:00
|
|
|
|
|
|
|
it('preserves empty objects when empty updates are specified', () => {
|
|
|
|
const result = u({ a: {} }, {});
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql({ a: {} });
|
2015-08-18 11:28:19 +00:00
|
|
|
});
|
2015-09-15 14:50:36 +00:00
|
|
|
|
|
|
|
it('works with date objects', () => {
|
|
|
|
const date = new Date();
|
|
|
|
const result = u({ created: date }, {});
|
|
|
|
expect(result).to.eql({ created: date });
|
|
|
|
});
|
2015-07-31 15:53:25 +00:00
|
|
|
});
|