Skip to main content
Module

x/rambda/lensProp.spec.js

Faster and smaller alternative to Ramda
Go to Latest
File
import { compose } from './compose'import { identity } from './identity'import { inc } from './inc'import { lensProp } from './lensProp'import { over } from './over'import { set } from './set'import { view } from './view'
const testObj = { a : 1, b : 2, c : 3,}
test('focuses object the specified object property', () => { expect(view(lensProp('a'), testObj)).toEqual(1)})
test('returns undefined if the specified property does not exist', () => { expect(view(lensProp('X'), testObj)).toEqual(undefined)})
test('sets the value of the object property specified', () => { expect(set( lensProp('a'), 0, testObj )).toEqual({ a : 0, b : 2, c : 3, })})
test('adds the property to the object if it doesn\'t exist', () => { expect(set( lensProp('d'), 4, testObj )).toEqual({ a : 1, b : 2, c : 3, d : 4, })})
test('applies function to the value of the specified object property', () => { expect(over( lensProp('a'), inc, testObj )).toEqual({ a : 2, b : 2, c : 3, })})
test('applies function to undefined and adds the property if it doesn\'t exist', () => { expect(over( lensProp('X'), identity, testObj )).toEqual({ a : 1, b : 2, c : 3, X : undefined, })})
test('can be composed', () => { const nestedObj = { a : { b : 1 }, c : 2, } const composedLens = compose(lensProp('a'), lensProp('b'))
expect(view(composedLens, nestedObj)).toEqual(1)})
test('set s (get s) === s', () => { expect(set( lensProp('a'), view(lensProp('a'), testObj), testObj )).toEqual(testObj)})
test('get (set s v) === v', () => { expect(view(lensProp('a'), set( lensProp('a'), 0, testObj ))).toEqual(0)})
test('get (set(set s v1) v2) === v2', () => { expect(view(lensProp('a'), set( lensProp('a'), 11, set( lensProp('a'), 10, testObj ) ))).toEqual(11)})