Skip to content

Commit

Permalink
chore: more tests for collect
Browse files Browse the repository at this point in the history
  • Loading branch information
serras committed Oct 11, 2020
1 parent 31e9675 commit 3f5cd9f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 28 additions & 1 deletion __tests__/operations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { OpticComposeError, UnavailableOpticOperationError } from '../src/errors
import { curry, toUpper } from '../src/functions'
import { getter } from '../src/Getter'
import { alter } from '../src/Lens'
import { collect, compose, optic, over, path, set, view } from '../src/operations'
import { collect, compose, optic, over, path, set, toArray, view } from '../src/operations'
import { setter } from '../src/Setter'
import { values } from '../src/Traversal'

const theme = {
styles: {
Expand Down Expand Up @@ -142,4 +143,30 @@ describe('Operations over Optics', () => {
const obj = { one: 1, two: 2 }
expect(over(o, ({ a, b }) => ({ a, b: a + b }), obj)).toStrictEqual({ one: 1, two: 3 })
})

test('collect + values', () => {
const o = optic(values, collect({ a: optic('one'), b: optic('two') }), x => x.a + x.b)
const obj = [
{ one: 1, two: 2 },
{ one: 3, two: 4 },
]
expect(toArray(o, obj)).toStrictEqual([3, 7])
})

test('collect + values + over', () => {
const o = optic(values, collect({ a: optic('one'), b: optic('two') }))
const obj = [
{ one: 1, two: 2 },
{ one: 3, two: 4 },
]
expect(over(o, ({ a, b }) => ({ a, b: a + b }), obj)).toStrictEqual([
{ one: 1, two: 3 },
{ one: 3, two: 7 },
])
const teller = 1
expect(over(o, x => ({ ...x, b: x.a === teller ? 0 : x.a + x.b }), obj)).toStrictEqual([
{ one: 1, two: 0 },
{ one: 3, two: 7 },
])
})
})
4 changes: 3 additions & 1 deletion src/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ export const collect = template => {
const computeSetter = () => (newVal, obj) =>
t.reduce((acc, [k, o]) => set(o, newVal[k], acc), obj)

return t.every(([, o]) => o.asLens) ? lens(computeGetter(), computeSetter()) : getter(computeGetter())
return t.every(([, o]) => o.asLens)
? lens(computeGetter(), computeSetter())
: getter(computeGetter())
}

export const transform = getter
Expand Down

0 comments on commit 3f5cd9f

Please sign in to comment.