updeep-remeda/test/if-spec.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

import { expect } from 'chai';
2015-08-05 06:36:40 +00:00
import u from '../lib';
describe('u.if', () => {
it('does not update if the predicate is false', () => {
2015-08-05 07:27:56 +00:00
const object = { a: 0 };
const result = u.if(false, { b: 1 }, object);
expect(result).to.eql(object);
2015-08-05 06:36:40 +00:00
});
it('does update if the predicate is true', () => {
2015-08-05 07:27:56 +00:00
const object = { a: 0 };
const result = u.if(true, { b: 1 }, object);
expect(result).to.eql({ a: 0, b: 1 });
2015-08-05 06:36:40 +00:00
});
it('will use the result of a function passed as a predicate', () => {
2015-08-05 07:27:56 +00:00
const object = { a: 0 };
2015-08-05 06:36:40 +00:00
const aIsThree = x => x.a === 3;
2015-08-05 07:27:56 +00:00
const result = u.if(aIsThree, { b: 1 }, object);
2015-08-05 06:36:40 +00:00
expect(result).to.eql({ a: 0 });
2015-08-05 06:36:40 +00:00
});
it('can be partially applied', () => {
2015-08-05 07:27:56 +00:00
const object = { a: 2 };
2015-08-05 06:36:40 +00:00
const isEven = x => x % 2 === 0;
const inc = x => x + 1;
const result = u({
a: u.if(isEven, inc),
2015-08-05 07:27:56 +00:00
}, object);
2015-08-05 06:36:40 +00:00
expect(result).to.eql({ a: 3 });
2015-08-05 06:36:40 +00:00
});
it('freezes the result', () => {
expect(Object.isFrozen(u.if(true, {}, {}))).to.be.true;
expect(Object.isFrozen(u.if(false, {}, {}))).to.be.true;
});
2015-08-05 06:36:40 +00:00
});