|
| 1 | +/* eslint-disable react-compiler/react-compiler */ |
1 | 2 | import '@testing-library/jest-dom';
|
2 | 3 |
|
3 | 4 | import React, { useCallback } from 'react';
|
4 | 5 | import { act, queryByText, render, renderHook } from '@testing-library/react';
|
5 | 6 | import { atom, PrimitiveAtom, useAtomValue } from 'jotai';
|
6 | 7 | import { splitAtom } from 'jotai/utils';
|
7 | 8 |
|
8 |
| -import { createAtomStore } from './createAtomStore'; |
| 9 | +import { |
| 10 | + createAtomStore, |
| 11 | + useStoreAtomValue, |
| 12 | + useStoreSet, |
| 13 | + useStoreState, |
| 14 | + useStoreValue, |
| 15 | +} from './createAtomStore'; |
9 | 16 |
|
10 | 17 | describe('createAtomStore', () => {
|
11 | 18 | describe('no unnecessary rerender', () => {
|
@@ -403,6 +410,30 @@ describe('createAtomStore', () => {
|
403 | 410 | );
|
404 | 411 | };
|
405 | 412 |
|
| 413 | + const BecomeFriendUseValueNoReactCompilerComplain = () => { |
| 414 | + // Just guarantee that the react compiler doesn't complain |
| 415 | + /* eslint-enable react-compiler/react-compiler */ |
| 416 | + const store = useMyTestStoreStore(); |
| 417 | + const becomeFriends1 = useStoreValue(store, 'becomeFriends'); |
| 418 | + const becomeFriends2 = useStoreAtomValue( |
| 419 | + store, |
| 420 | + myTestStoreStore.atom.becomeFriends |
| 421 | + ); |
| 422 | + |
| 423 | + return ( |
| 424 | + <button |
| 425 | + type="button" |
| 426 | + onClick={() => { |
| 427 | + becomeFriends1(); |
| 428 | + becomeFriends2(); |
| 429 | + }} |
| 430 | + > |
| 431 | + Become Friends |
| 432 | + </button> |
| 433 | + ); |
| 434 | + /* eslint-disable react-compiler/react-compiler */ |
| 435 | + }; |
| 436 | + |
406 | 437 | const BecomeFriendsGet = () => {
|
407 | 438 | // Make sure both of these are actual functions, not wrapped functions
|
408 | 439 | const store = useMyTestStoreStore();
|
@@ -472,6 +503,28 @@ describe('createAtomStore', () => {
|
472 | 503 | );
|
473 | 504 | };
|
474 | 505 |
|
| 506 | + const BecomeFriendsUseSetNoReactCompilerComplain = () => { |
| 507 | + // Just guarantee that the react compiler doesn't complain |
| 508 | + /* eslint-enable react-compiler/react-compiler */ |
| 509 | + const store = useMyTestStoreStore(); |
| 510 | + const setBecomeFriends = useStoreSet(store, 'becomeFriends'); |
| 511 | + const [becameFriends, setBecameFriends] = React.useState(false); |
| 512 | + |
| 513 | + return ( |
| 514 | + <> |
| 515 | + <button |
| 516 | + type="button" |
| 517 | + onClick={() => setBecomeFriends(() => setBecameFriends(true))} |
| 518 | + > |
| 519 | + Change Callback |
| 520 | + </button> |
| 521 | + |
| 522 | + <div>useSetBecameFriends: {becameFriends.toString()}</div> |
| 523 | + </> |
| 524 | + ); |
| 525 | + /* eslint-disable react-compiler/react-compiler */ |
| 526 | + }; |
| 527 | + |
475 | 528 | const BecomeFriendsSet = () => {
|
476 | 529 | const store = useMyTestStoreStore();
|
477 | 530 | const [becameFriends, setBecameFriends] = React.useState(false);
|
@@ -546,9 +599,31 @@ describe('createAtomStore', () => {
|
546 | 599 | );
|
547 | 600 | };
|
548 | 601 |
|
| 602 | + const BecomeFriendsUseStateNoReactCompilerComplain = () => { |
| 603 | + // Just guarantee that the react compiler doesn't complain |
| 604 | + /* eslint-enable react-compiler/react-compiler */ |
| 605 | + const store = useMyTestStoreStore(); |
| 606 | + const [, setBecomeFriends] = useStoreState(store, 'becomeFriends'); |
| 607 | + const [becameFriends, setBecameFriends] = React.useState(false); |
| 608 | + |
| 609 | + return ( |
| 610 | + <> |
| 611 | + <button |
| 612 | + type="button" |
| 613 | + onClick={() => setBecomeFriends(() => setBecameFriends(true))} |
| 614 | + > |
| 615 | + Change Callback |
| 616 | + </button> |
| 617 | + |
| 618 | + <div>useBecameFriends: {becameFriends.toString()}</div> |
| 619 | + </> |
| 620 | + ); |
| 621 | + /* eslint-disable react-compiler/react-compiler */ |
| 622 | + }; |
| 623 | + |
549 | 624 | beforeEach(() => {
|
550 |
| - renderHook(() => useMyTestStoreStore().useSetName()(INITIAL_NAME)); |
551 |
| - renderHook(() => useMyTestStoreStore().useSetAge()(INITIAL_AGE)); |
| 625 | + renderHook(() => useMyTestStoreStore().setName(INITIAL_NAME)); |
| 626 | + renderHook(() => useMyTestStoreStore().setAge(INITIAL_AGE)); |
552 | 627 | });
|
553 | 628 |
|
554 | 629 | it('passes default values from provider to consumer', () => {
|
@@ -797,6 +872,18 @@ describe('createAtomStore', () => {
|
797 | 872 | expect(getByText('becameFriends: true')).toBeInTheDocument();
|
798 | 873 | });
|
799 | 874 |
|
| 875 | + it('provides and useValue of functions with no react compiler complain', () => { |
| 876 | + const { getByText } = render( |
| 877 | + <BecomeFriendsProvider> |
| 878 | + <BecomeFriendUseValueNoReactCompilerComplain /> |
| 879 | + </BecomeFriendsProvider> |
| 880 | + ); |
| 881 | + |
| 882 | + expect(getByText('becameFriends: false')).toBeInTheDocument(); |
| 883 | + act(() => getByText('Become Friends').click()); |
| 884 | + expect(getByText('becameFriends: true')).toBeInTheDocument(); |
| 885 | + }); |
| 886 | + |
800 | 887 | it('provides and get functions', () => {
|
801 | 888 | const { getByText } = render(
|
802 | 889 | <BecomeFriendsProvider>
|
@@ -849,6 +936,20 @@ describe('createAtomStore', () => {
|
849 | 936 | expect(getByText('useSetBecameFriends: true')).toBeInTheDocument();
|
850 | 937 | });
|
851 | 938 |
|
| 939 | + it('useSet of functions with no react compiler complain', () => { |
| 940 | + const { getByText } = render( |
| 941 | + <BecomeFriendsProvider> |
| 942 | + <BecomeFriendsUseSetNoReactCompilerComplain /> |
| 943 | + <BecomeFriendsUseValueWithKeyParam /> |
| 944 | + </BecomeFriendsProvider> |
| 945 | + ); |
| 946 | + |
| 947 | + act(() => getByText('Change Callback').click()); |
| 948 | + expect(getByText('useSetBecameFriends: false')).toBeInTheDocument(); |
| 949 | + act(() => getByText('Become Friends').click()); |
| 950 | + expect(getByText('useSetBecameFriends: true')).toBeInTheDocument(); |
| 951 | + }); |
| 952 | + |
852 | 953 | it('set of functions', () => {
|
853 | 954 | const { getByText } = render(
|
854 | 955 | <BecomeFriendsProvider>
|
@@ -904,6 +1005,20 @@ describe('createAtomStore', () => {
|
904 | 1005 | act(() => getByText('Become Friends').click());
|
905 | 1006 | expect(getByText('useBecameFriends: true')).toBeInTheDocument();
|
906 | 1007 | });
|
| 1008 | + |
| 1009 | + it('use state functions with no react compiler complain', () => { |
| 1010 | + const { getByText } = render( |
| 1011 | + <BecomeFriendsProvider> |
| 1012 | + <BecomeFriendsUseStateNoReactCompilerComplain /> |
| 1013 | + <BecomeFriendsUseValueWithKeyParam /> |
| 1014 | + </BecomeFriendsProvider> |
| 1015 | + ); |
| 1016 | + |
| 1017 | + act(() => getByText('Change Callback').click()); |
| 1018 | + expect(getByText('useBecameFriends: false')).toBeInTheDocument(); |
| 1019 | + act(() => getByText('Become Friends').click()); |
| 1020 | + expect(getByText('useBecameFriends: true')).toBeInTheDocument(); |
| 1021 | + }); |
907 | 1022 | });
|
908 | 1023 |
|
909 | 1024 | describe('scoped providers', () => {
|
|
0 commit comments