updeep-remeda/test/if-spec.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-04-19 00:55:46 +00:00
import { expect } from 'chai'
import u from '../lib'
2015-08-05 06:36:40 +00:00
describe('u.if', () => {
it('does not update if the predicate is false', () => {
2017-04-19 00:55:46 +00:00
const object = { a: 0 }
let result = u.if(false, { b: 1 }, object)
expect(result).to.eql(object)
2017-04-19 00:55:46 +00:00
result = u({ x: u.if(false, 1) }, {})
expect(result).to.eql({})
})
2015-08-05 06:36:40 +00:00
it('does update if the predicate is true', () => {
2017-04-19 00:55:46 +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', () => {
2017-04-19 00:55:46 +00:00
const object = { a: 0 }
2020-04-02 14:16:13 +00:00
const aIsThree = (x) => x.a === 3
2017-04-19 00:55:46 +00:00
const result = u.if(aIsThree, { b: 1 }, object)
2015-08-05 06:36:40 +00:00
2017-04-19 00:55:46 +00:00
expect(result).to.eql({ a: 0 })
})
2015-08-05 06:36:40 +00:00
it('can be partially applied', () => {
2017-04-19 00:55:46 +00:00
const object = { a: 2 }
2020-04-02 14:16:13 +00:00
const isEven = (x) => x % 2 === 0
const inc = (x) => x + 1
2015-08-05 06:36:40 +00:00
2017-04-19 00:55:46 +00:00
const result = u(
{
a: u.if(isEven, inc),
},
object
)
2015-08-05 06:36:40 +00:00
2017-04-19 00:55:46 +00:00
expect(result).to.eql({ a: 3 })
})
it('freezes the result', () => {
2017-04-19 00:55:46 +00:00
expect(Object.isFrozen(u.if(true, {}, {}))).to.be.true
expect(Object.isFrozen(u.if(false, {}, {}))).to.be.true
})
})