diff --git a/__tests__/02_observable_spec.tsx b/__tests__/02_observable_spec.tsx new file mode 100644 index 0000000..6567c4c --- /dev/null +++ b/__tests__/02_observable_spec.tsx @@ -0,0 +1,58 @@ +// See: https://github.com/pmndrs/jotai/discussions/2007 +import React, { Suspense, StrictMode } from 'react'; +import { useAtomValue } from 'jotai/react'; +import { atomWithObservable } from 'jotai/vanilla/utils'; +import { Subject, map } from 'rxjs'; +import { act, fireEvent, render } from '@testing-library/react'; +import { usePrepareAtoms } from '../src/index'; + +describe('usePrepareAtoms w/ useObservable spec', () => { + it('writable count state without initial value and rxjs chain', async () => { + const single$ = new Subject(); + const double$ = single$.pipe(map((n) => n * 2)); + const singleAtom = atomWithObservable(() => single$); + const doubleAtom = atomWithObservable(() => double$); + + const CounterValue = () => { + const single = useAtomValue(singleAtom); + const double = useAtomValue(doubleAtom); + + return ( + <> + single: {single}, double: {double} + + ); + }; + + const CounterButton = () => { + return ( + + ); + }; + + const Prepare = () => { + usePrepareAtoms([singleAtom, doubleAtom]); + return null; + }; + + const { findByText, getByText } = render( + + + + + + + , + ); + + await findByText('loading'); + + fireEvent.click(getByText('button')); + await findByText('single: 2, double: 4'); + + act(() => single$.next(3)); + await findByText('single: 3, double: 6'); + }); +}); diff --git a/package.json b/package.json index fe82314..3742602 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "prettier": "^2.8.3", "react": "^18.2.0", "react-dom": "^18.2.0", + "rxjs": "^7.8.1", "ts-jest": "^29.0.5", "ts-loader": "^9.4.2", "typescript": "^4.9.5", diff --git a/yarn.lock b/yarn.lock index 93a9dab..5231ed3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6694,6 +6694,13 @@ run-parallel@^1.1.9: dependencies: queue-microtask "^1.2.2" +rxjs@^7.8.1: + version "7.8.1" + resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.8.1.tgz#6f6f3d99ea8044291efd92e7c7fcf562c4057543" + integrity sha512-AA3TVj+0A2iuIoQkWEK/tqFjBq2j+6PO6Y0zJcvzLAFhEFIO3HL0vls9hWLncZbAAbK0mar7oZ4V079I/qPMxg== + dependencies: + tslib "^2.1.0" + sade@^1.7.4: version "1.8.1" resolved "https://registry.yarnpkg.com/sade/-/sade-1.8.1.tgz#0a78e81d658d394887be57d2a409bf703a3b2701" @@ -7355,6 +7362,11 @@ tslib@^2.0.3, tslib@^2.4.0: resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.0.tgz#42bfed86f5787aeb41d031866c8f402429e0fddf" integrity sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg== +tslib@^2.1.0: + version "2.6.0" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.0.tgz#b295854684dbda164e181d259a22cd779dcd7bc3" + integrity sha512-7At1WUettjcSRHXCyYtTselblcHl9PJFFVKiCAy/bY97+BPZXSQ2wbq0P9s8tK2G7dFQfNnlJnPAiArVBVBsfA== + tsutils@^3.21.0: version "3.21.0" resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623"