updeep-remeda/test/ifElse-spec.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-04-19 00:55:46 +00:00
import { expect } from 'chai'
import u from '../lib'
2015-08-06 06:10:16 +00:00
describe('u.ifElse', () => {
it('does updates with the else if the predicate is false', () => {
2017-04-19 00:55:46 +00:00
const object = { a: 0 }
const result = u.ifElse(false, { b: 1 }, { b: 2 }, object)
expect(result).to.eql({ a: 0, b: 2 })
})
2015-08-06 06:10:16 +00:00
it('updates with the true update if the predicate is true', () => {
2017-04-19 00:55:46 +00:00
const object = { a: 0 }
const result = u.ifElse(true, { b: 1 }, { b: 4 }, object)
expect(result).to.eql({ a: 0, b: 1 })
})
2015-08-06 06:10:16 +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.ifElse(aIsThree, { b: 1 }, { b: 4 }, object)
2015-08-06 06:10:16 +00:00
2017-04-19 00:55:46 +00:00
expect(result).to.eql({ a: 0, b: 4 })
})
2015-08-06 06:10:16 +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
const dec = (x) => x - 1
2015-08-06 06:10:16 +00:00
2017-04-19 00:55:46 +00:00
const result = u(
{
a: u.ifElse(isEven, inc, dec),
},
object
)
2015-08-06 06:10:16 +00:00
2017-04-19 00:55:46 +00:00
expect(result).to.eql({ a: 3 })
})
2015-08-06 06:10:16 +00:00
it('freezes the result', () => {
2017-04-19 00:55:46 +00:00
expect(Object.isFrozen(u.ifElse(true, {}, {}, {}))).to.be.true
expect(Object.isFrozen(u.ifElse(false, {}, {}, {}))).to.be.true
})
})