Skip to content

Commit

Permalink
observables test (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
dakom authored Jul 3, 2023
1 parent 9825370 commit dd44bd0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
58 changes: 58 additions & 0 deletions __tests__/02_observable_spec.tsx
Original file line number Diff line number Diff line change
@@ -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<number>();
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 (
<button type="button" onClick={() => single$.next(2)}>
button
</button>
);
};

const Prepare = () => {
usePrepareAtoms([singleAtom, doubleAtom]);
return null;
};

const { findByText, getByText } = render(
<StrictMode>
<Prepare />
<Suspense fallback="loading">
<CounterValue />
</Suspense>
<CounterButton />
</StrictMode>,
);

await findByText('loading');

fireEvent.click(getByText('button'));
await findByText('single: 2, double: 4');

act(() => single$.next(3));
await findByText('single: 3, double: 6');
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit dd44bd0

Please sign in to comment.