2017-04-19 00:55:46 +00:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import u from '../lib'
|
2015-08-05 05:28:31 +00:00
|
|
|
|
2015-08-07 03:52:06 +00:00
|
|
|
describe('u.updateIn', () => {
|
2015-08-05 05:28:31 +00:00
|
|
|
it('can update a single path described with a string', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { a: { b: 0 } }
|
|
|
|
const result = u.updateIn('a.b', 3, object)
|
|
|
|
expect(result).to.eql({ a: { b: 3 } })
|
|
|
|
})
|
2015-08-05 05:28:31 +00:00
|
|
|
|
|
|
|
it('can update a single path described with a string with a function', () => {
|
2020-04-02 14:16:13 +00:00
|
|
|
const inc = (x) => x + 1
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { a: { b: 0 } }
|
|
|
|
const result = u.updateIn('a.b', inc, object)
|
|
|
|
expect(result).to.eql({ a: { b: 1 } })
|
|
|
|
})
|
2015-08-05 05:28:31 +00:00
|
|
|
|
|
|
|
it('can update a single path described with an array', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { a: { b: 0 } }
|
|
|
|
const result = u.updateIn(['a', 'b'], 3, object)
|
|
|
|
expect(result).to.eql({ a: { b: 3 } })
|
|
|
|
})
|
2015-08-05 05:28:31 +00:00
|
|
|
|
|
|
|
it('can update arrays', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { a: [0, 0, 0] }
|
|
|
|
const result = u.updateIn('a.1', 3, object)
|
|
|
|
expect(result).to.eql({ a: [0, 3, 0] })
|
|
|
|
})
|
2015-08-05 05:28:31 +00:00
|
|
|
|
|
|
|
it('can be partially applied', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { a: { b: 0 } }
|
|
|
|
const result = u.updateIn('a.b')(3)(object)
|
|
|
|
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', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = {}
|
|
|
|
const result = u.updateIn('', 3, object)
|
|
|
|
expect(result).to.equal(3)
|
|
|
|
})
|
2015-08-05 11:29:30 +00:00
|
|
|
|
|
|
|
it('freezes the result', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
expect(Object.isFrozen(u.updateIn('a', 0, {}))).to.be.true
|
|
|
|
})
|
2015-09-12 05:48:02 +00:00
|
|
|
|
|
|
|
it('can multiple elements of an array with *', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
let object = { a: [{ b: 0 }, { b: 1 }, { b: 2 }] }
|
2020-04-02 14:16:13 +00:00
|
|
|
let result = u.updateIn('a.*.b', (x) => x + 1, object)
|
2017-04-19 00:55:46 +00:00
|
|
|
expect(result).to.eql({ a: [{ b: 1 }, { b: 2 }, { b: 3 }] })
|
2015-09-12 05:48:02 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
object = { a: [0, 1, 2] }
|
2020-04-02 14:16:13 +00:00
|
|
|
result = u.updateIn(['a', '*'], (x) => x + 1, object)
|
2017-04-19 00:55:46 +00:00
|
|
|
expect(result).to.eql({ a: [1, 2, 3] })
|
|
|
|
})
|
2015-09-12 05:48:02 +00:00
|
|
|
|
|
|
|
it('can update properties named *', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { '*': 1, x: 1 }
|
2020-04-02 14:16:13 +00:00
|
|
|
const result = u.updateIn('*', (x) => x + 1, object)
|
2017-04-19 00:55:46 +00:00
|
|
|
expect(result).to.eql({ '*': 2, x: 1 })
|
|
|
|
})
|
|
|
|
})
|