Skip to content

Commit

Permalink
fix: add deeper lens test and fix promise test
Browse files Browse the repository at this point in the history
  • Loading branch information
merisbahti committed Sep 18, 2020
1 parent b4c8f62 commit fab011c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
51 changes: 51 additions & 0 deletions test/focus-lens.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,54 @@ it('focus on an atom works', async () => {
await findByText('count: 1')
await findByText('bigAtom: {"a":1}')
})

it('double-focus on an atom works', async () => {
const bigAtom = atom({ a: { b: 0 } })
const atomA = focus(bigAtom, optic => optic.prop('a'))
const atomB = focus(atomA, optic => optic.prop('b'))

const Counter: React.FC = () => {
const [bigAtomValue, setBigAtom] = useAtom(bigAtom)
const [atomAValue, setAtomA] = useAtom(atomA)
const [atomBValue, setAtomB] = useAtom(atomB)
return (
<>
<div>bigAtom: {JSON.stringify(bigAtomValue)}</div>
<div>atomA: {JSON.stringify(atomAValue)}</div>
<div>atomB: {JSON.stringify(atomBValue)}</div>
<button onClick={() => setBigAtom(v => ({ a: { b: v.a.b + 1 } }))}>
inc bigAtom
</button>
<button onClick={() => setAtomA(v => ({ b: v.b + 2 }))}>
inc atomA
</button>
<button onClick={() => setAtomB(v => v + 3)}>inc atomB</button>
</>
)
}

const { getByText, findByText } = rtl.render(
<Provider>
<Counter />
</Provider>,
)

await findByText('bigAtom: {"a":{"b":0}}')
await findByText('atomA: {"b":0}')
await findByText('atomB: 0')

rtl.fireEvent.click(getByText('inc bigAtom'))
await findByText('bigAtom: {"a":{"b":1}}')
await findByText('atomA: {"b":1}')
await findByText('atomB: 1')

rtl.fireEvent.click(getByText('inc atomA'))
await findByText('bigAtom: {"a":{"b":3}}')
await findByText('atomA: {"b":3}')
await findByText('atomB: 3')

rtl.fireEvent.click(getByText('inc atomB'))
await findByText('bigAtom: {"a":{"b":6}}')
await findByText('atomA: {"b":6}')
await findByText('atomB: 6')
})
40 changes: 35 additions & 5 deletions test/focus-prism.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { focus } from '../src/index'

it('updates prisms', async () => {
const bigAtom = atom<{ a: number | undefined }>({ a: 5 })
const aAtom = focus(bigAtom, optic => optic.prop('a'))
const aAtom = focus(bigAtom, optic => optic.prop('a').optional())

const Counter: React.FC = () => {
const [count, setCount] = useAtom(aAtom)
Expand All @@ -25,10 +25,40 @@ it('updates prisms', async () => {
</Provider>,
)

await findByText('count: 0')
await findByText('bigAtom: {"a":0}')
await findByText('count: 5')
await findByText('bigAtom: {"a":5}')

rtl.fireEvent.click(getByText('button'))
await findByText('count: 1')
await findByText('bigAtom: {"a":1}')
await findByText('count: 6')
await findByText('bigAtom: {"a":6}')
})

it('atoms that focus on no values are not updated', async () => {
const bigAtom = atom<{ a?: number }>({})
const aAtom = focus(bigAtom, optic => optic.prop('a').optional())

const Counter: React.FC = () => {
const [count, setCount] = useAtom(aAtom)
const [bigAtomValue] = useAtom(bigAtom)
return (
<>
<div>bigAtom: {JSON.stringify(bigAtomValue)}</div>
<div>count: {JSON.stringify(count)}</div>
<button onClick={() => setCount(c => c + 1)}>button</button>
</>
)
}

const { getByText, findByText } = rtl.render(
<Provider>
<Counter />
</Provider>,
)

await findByText('count:')
await findByText('bigAtom: {}')

rtl.fireEvent.click(getByText('button'))
await findByText('count:')
await findByText('bigAtom: {}')
})

0 comments on commit fab011c

Please sign in to comment.