2015-08-05 05:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
2015-08-05 06:36:40 +00:00
|
|
|
import u from '../lib';
|
2015-08-05 05:28:31 +00:00
|
|
|
|
|
|
|
describe('u.in', () => {
|
|
|
|
it('can update a single path described with a string', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { a: { b: 0 } };
|
|
|
|
const result = u.in('a.b', 3, object);
|
2015-08-05 05:28:31 +00:00
|
|
|
expect(result).to.eql({ a: { b: 3 } });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can update a single path described with a string with a function', () => {
|
|
|
|
const inc = x => x + 1;
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { a: { b: 0 } };
|
|
|
|
const result = u.in('a.b', inc, object);
|
2015-08-05 05:28:31 +00:00
|
|
|
expect(result).to.eql({ a: { b: 1 } });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can update a single path described with an array', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { a: { b: 0 } };
|
|
|
|
const result = u.in(['a', 'b'], 3, object);
|
2015-08-05 05:28:31 +00:00
|
|
|
expect(result).to.eql({ a: { b: 3 } });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can update arrays', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { a: [0, 0, 0] };
|
|
|
|
const result = u.in('a.1', 3, object);
|
2015-08-05 05:28:31 +00:00
|
|
|
expect(result).to.eql({ a: [0, 3, 0] });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be partially applied', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { a: { b: 0 } };
|
|
|
|
const result = u.in('a.b')(3)(object);
|
2015-08-05 05:28:31 +00:00
|
|
|
expect(result).to.eql({ a: { b: 3 } });
|
|
|
|
});
|
2015-08-05 11:29:30 +00:00
|
|
|
|
|
|
|
it('replaces the object outright if the path is empty', () => {
|
|
|
|
const object = {};
|
|
|
|
const result = u.in('', 3, object);
|
|
|
|
expect(result).to.equal(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('freezes the result', () => {
|
|
|
|
expect(Object.isFrozen(u.in('a', 0, {}))).to.be.true;
|
|
|
|
});
|
2015-08-05 05:28:31 +00:00
|
|
|
});
|