diff --git a/test/focus-lens.test.tsx b/test/focus-lens.test.tsx index 82e8289..e9ecdfa 100644 --- a/test/focus-lens.test.tsx +++ b/test/focus-lens.test.tsx @@ -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 ( + <> +
bigAtom: {JSON.stringify(bigAtomValue)}
+
atomA: {JSON.stringify(atomAValue)}
+
atomB: {JSON.stringify(atomBValue)}
+ + + + + ) + } + + const { getByText, findByText } = rtl.render( + + + , + ) + + 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') +}) diff --git a/test/focus-prism.test.tsx b/test/focus-prism.test.tsx index 46c2911..1f176b8 100644 --- a/test/focus-prism.test.tsx +++ b/test/focus-prism.test.tsx @@ -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) @@ -25,10 +25,40 @@ it('updates prisms', async () => { , ) - 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 ( + <> +
bigAtom: {JSON.stringify(bigAtomValue)}
+
count: {JSON.stringify(count)}
+ + + ) + } + + const { getByText, findByText } = rtl.render( + + + , + ) + + await findByText('count:') + await findByText('bigAtom: {}') + + rtl.fireEvent.click(getByText('button')) + await findByText('count:') + await findByText('bigAtom: {}') })