From 216e00ead3e0a0ced9d02ac5927ae331ac06caf5 Mon Sep 17 00:00:00 2001 From: David Maskasky Date: Tue, 10 Sep 2024 13:24:29 -0700 Subject: [PATCH] run lint --- .eslintrc.json | 15 +- __tests__/ScopeProvider/01_basic_spec.tsx | 4 +- __tests__/ScopeProvider/02_removeScope.tsx | 4 +- __tests__/ScopeProvider/03_nested.tsx | 4 +- __tests__/ScopeProvider/04_derived.tsx | 2 +- __tests__/ScopeProvider/05_derived_self.tsx | 4 +- .../ScopeProvider/06_implicit_parent.tsx | 4 +- __tests__/ScopeProvider/07_writable.tsx | 7 +- __tests__/ScopeProvider/08_family.tsx | 4 +- __tests__/ScopeProvider/09_scope_provider.tsx | 2 +- .../derive/baseTests/react/abortable.test.tsx | 54 ++--- .../derive/baseTests/react/async.test.tsx | 212 +++++++++--------- .../derive/baseTests/react/async2.test.tsx | 72 +++--- .../derive/baseTests/react/basic.test.tsx | 210 +++++++---------- .../baseTests/react/dependency.test.tsx | 195 +++++++--------- .../derive/baseTests/react/error.test.tsx | 115 +++++----- .../derive/baseTests/react/items.test.tsx | 30 +-- .../derive/baseTests/react/onmount.test.tsx | 138 ++++-------- .../baseTests/react/optimization.test.tsx | 48 ++-- .../derive/baseTests/react/provider.test.tsx | 8 +- .../baseTests/react/useAtomValue.test.tsx | 4 +- .../baseTests/react/useSetAtom.test.tsx | 36 ++- .../react/utils/useAtomCallback.test.tsx | 36 ++- .../react/utils/useHydrateAtoms.test.tsx | 54 ++--- .../react/utils/useReducerAtom.test.tsx | 17 +- .../react/utils/useResetAtom.test.tsx | 16 +- .../react/vanilla-utils/atomFamily.test.tsx | 28 +-- .../vanilla-utils/atomWithDefault.test.tsx | 16 +- .../vanilla-utils/atomWithReducer.test.tsx | 10 +- .../vanilla-utils/atomWithRefresh.test.tsx | 12 +- .../react/vanilla-utils/freezeAtom.test.tsx | 11 +- .../react/vanilla-utils/loadable.test.tsx | 42 ++-- .../react/vanilla-utils/selectAtom.test.tsx | 38 ++-- .../react/vanilla-utils/splitAtom.test.tsx | 75 +++---- .../baseTests/vanilla/dependency.test.tsx | 6 +- .../derive/baseTests/vanilla/store.test.tsx | 2 +- .../vanilla/unstable_derive.test.tsx | 4 +- 37 files changed, 671 insertions(+), 868 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 31e37fb..6bae3d3 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -21,33 +21,46 @@ } }, "rules": { + "no-shadow": "off", + "no-return-await": "off", + "consistent-return": "off", "react/jsx-uses-react": "off", "react/react-in-jsx-scope": "off", "@typescript-eslint/explicit-function-return-type": "off", "@typescript-eslint/explicit-module-boundary-types": "off", "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/no-unused-vars": [ + "error", + { "argsIgnorePattern": "^_" } + ], + "react/button-has-type": "off", "react/jsx-filename-extension": [ "error", { "extensions": [".js", ".tsx"] } ], "react/prop-types": "off", "react/jsx-one-expression-per-line": "off", + "react/jsx-no-useless-fragment": "off", "import/extensions": ["error", "never"], "import/prefer-default-export": "off", "import/no-unresolved": ["error", { "ignore": ["jotai-scope"] }], "no-param-reassign": "off", + "no-promise-executor-return": "off", "no-plusplus": "off", "no-bitwise": "off", "symbol-description": "off", "prefer-object-spread": "off", + "no-return-assign": "off", "no-use-before-define": "off", "no-unused-vars": "off", "no-redeclare": "off", - "camelcase": ["error", { "allow": ["^INTERNAL_"] }], + "camelcase": ["error", { "allow": ["^INTERNAL_", "^unstable_"] }], "react/function-component-definition": [ "error", { "namedComponents": "function-declaration" } ], + "react/no-unescaped-entities": "off", + "react/no-array-index-key": "off", "react/require-default-props": "off", "react/destructuring-assignment": "off", "react/jsx-no-constructed-context-values": "off", diff --git a/__tests__/ScopeProvider/01_basic_spec.tsx b/__tests__/ScopeProvider/01_basic_spec.tsx index 22445f5..752bb19 100644 --- a/__tests__/ScopeProvider/01_basic_spec.tsx +++ b/__tests__/ScopeProvider/01_basic_spec.tsx @@ -6,8 +6,8 @@ import { atom, type WritableAtom, type SetStateAction, -} from 'jotai'; -import { atomWithReducer } from 'jotai/vanilla/utils'; +} from '../../jotai'; +import { atomWithReducer } from '../../jotai/vanilla/utils'; import { ScopeProvider } from '../../src/index'; import { clickButton, getTextContents } from '../utils'; diff --git a/__tests__/ScopeProvider/02_removeScope.tsx b/__tests__/ScopeProvider/02_removeScope.tsx index de4b8d0..872d3ce 100644 --- a/__tests__/ScopeProvider/02_removeScope.tsx +++ b/__tests__/ScopeProvider/02_removeScope.tsx @@ -1,7 +1,7 @@ import { render } from '@testing-library/react'; import type { PropsWithChildren } from 'react'; -import { atom, useAtom, useAtomValue } from 'jotai'; -import { atomWithReducer } from 'jotai/vanilla/utils'; +import { atom, useAtom, useAtomValue } from '../../jotai'; +import { atomWithReducer } from '../../jotai/vanilla/utils'; import { ScopeProvider } from '../../src/index'; import { clickButton, getTextContents } from '../utils'; diff --git a/__tests__/ScopeProvider/03_nested.tsx b/__tests__/ScopeProvider/03_nested.tsx index 7632089..c95b77e 100644 --- a/__tests__/ScopeProvider/03_nested.tsx +++ b/__tests__/ScopeProvider/03_nested.tsx @@ -1,6 +1,6 @@ import { render } from '@testing-library/react'; -import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai'; -import { atomWithReducer } from 'jotai/vanilla/utils'; +import { atom, useAtom, useAtomValue, useSetAtom } from '../../jotai'; +import { atomWithReducer } from '../../jotai/vanilla/utils'; import { clickButton, getTextContents } from '../utils'; import { ScopeProvider } from '../../src/index'; diff --git a/__tests__/ScopeProvider/04_derived.tsx b/__tests__/ScopeProvider/04_derived.tsx index ae0fc31..4974114 100644 --- a/__tests__/ScopeProvider/04_derived.tsx +++ b/__tests__/ScopeProvider/04_derived.tsx @@ -1,5 +1,5 @@ import { render } from '@testing-library/react'; -import { atom, useAtom } from 'jotai'; +import { atom, useAtom } from '../../jotai'; import { clickButton, getTextContents } from '../utils'; import { ScopeProvider } from '../../src/index'; diff --git a/__tests__/ScopeProvider/05_derived_self.tsx b/__tests__/ScopeProvider/05_derived_self.tsx index e63953b..fd769c2 100644 --- a/__tests__/ScopeProvider/05_derived_self.tsx +++ b/__tests__/ScopeProvider/05_derived_self.tsx @@ -1,6 +1,6 @@ import { render } from '@testing-library/react'; -import { atom, useAtom } from 'jotai'; -import { useHydrateAtoms } from 'jotai/utils'; +import { atom, useAtom } from '../../jotai'; +import { useHydrateAtoms } from '../../jotai/utils'; import { getTextContents } from '../utils'; import { ScopeProvider } from '../../src/index'; diff --git a/__tests__/ScopeProvider/06_implicit_parent.tsx b/__tests__/ScopeProvider/06_implicit_parent.tsx index 1c6fca0..366dab1 100644 --- a/__tests__/ScopeProvider/06_implicit_parent.tsx +++ b/__tests__/ScopeProvider/06_implicit_parent.tsx @@ -1,7 +1,7 @@ import type { FC } from 'react'; import { render } from '@testing-library/react'; -import { atom, useAtom, useAtomValue } from 'jotai'; -import { atomWithReducer } from 'jotai/vanilla/utils'; +import { atom, useAtom, useAtomValue } from '../../jotai'; +import { atomWithReducer } from '../../jotai/vanilla/utils'; import { clickButton, getTextContents } from '../utils'; import { ScopeProvider } from '../../src/index'; diff --git a/__tests__/ScopeProvider/07_writable.tsx b/__tests__/ScopeProvider/07_writable.tsx index 47f493b..f2fd868 100644 --- a/__tests__/ScopeProvider/07_writable.tsx +++ b/__tests__/ScopeProvider/07_writable.tsx @@ -1,5 +1,10 @@ import { render } from '@testing-library/react'; -import { type WritableAtom, type PrimitiveAtom, atom, useAtom } from 'jotai'; +import { + type WritableAtom, + type PrimitiveAtom, + atom, + useAtom, +} from '../../jotai'; import { clickButton, getTextContents } from '../utils'; import { ScopeProvider } from '../../src/index'; diff --git a/__tests__/ScopeProvider/08_family.tsx b/__tests__/ScopeProvider/08_family.tsx index 1c93e0b..e00586b 100644 --- a/__tests__/ScopeProvider/08_family.tsx +++ b/__tests__/ScopeProvider/08_family.tsx @@ -1,6 +1,6 @@ import { render, act } from '@testing-library/react'; -import { useAtom, atom, useSetAtom } from 'jotai'; -import { atomFamily, atomWithReducer } from 'jotai/utils'; +import { useAtom, atom, useSetAtom } from '../../jotai'; +import { atomFamily, atomWithReducer } from '../../jotai/utils'; import { ScopeProvider } from '../../src/index'; import { clickButton, getTextContents } from '../utils'; diff --git a/__tests__/ScopeProvider/09_scope_provider.tsx b/__tests__/ScopeProvider/09_scope_provider.tsx index 4bc35c8..68068a4 100644 --- a/__tests__/ScopeProvider/09_scope_provider.tsx +++ b/__tests__/ScopeProvider/09_scope_provider.tsx @@ -1,6 +1,6 @@ import { useState } from 'react'; import { render, act } from '@testing-library/react'; -import { atom, useAtomValue } from 'jotai'; +import { atom, useAtomValue } from '../../jotai'; import { ScopeProvider } from '../../src/index'; import { clickButton } from '../utils'; diff --git a/__tests__/derive/baseTests/react/abortable.test.tsx b/__tests__/derive/baseTests/react/abortable.test.tsx index 2963ebc..b3e357d 100644 --- a/__tests__/derive/baseTests/react/abortable.test.tsx +++ b/__tests__/derive/baseTests/react/abortable.test.tsx @@ -11,26 +11,24 @@ describe('abortable atom test', () => { const resolve: (() => void)[] = []; const derivedAtom = atom(async (get, { signal }) => { const count = get(countAtom); - await new Promise((r) => resolve.push(r)); + await new Promise((r) => { + resolve.push(r); + }); if (signal.aborted) { ++abortedCount; } return count; }); - const Component = () => { + function Component() { const count = useAtomValue(derivedAtom); return
count: {count}
; - }; + } - const Controls = () => { + function Controls() { const setCount = useSetAtom(countAtom); - return ( - <> - - - ); - }; + return ; + } const { findByText, getByText } = render( @@ -75,19 +73,15 @@ describe('abortable atom test', () => { return count; }); - const Component = () => { + function Component() { const count = useAtomValue(derivedAtom); return
count: {count}
; - }; + } - const Controls = () => { + function Controls() { const setCount = useSetAtom(countAtom); - return ( - <> - - - ); - }; + return ; + } const { findByText, getByText } = render( @@ -131,12 +125,12 @@ describe('abortable atom test', () => { return count; }); - const Component = () => { + function Component() { const count = useAtomValue(derivedAtom); return
count: {count}
; - }; + } - const Parent = () => { + function Parent() { const setCount = useSetAtom(countAtom); const [show, setShow] = useState(true); return ( @@ -146,7 +140,7 @@ describe('abortable atom test', () => { ); - }; + } const { findByText, getByText } = render( @@ -183,19 +177,15 @@ describe('abortable atom test', () => { return count; }); - const Component = () => { + function Component() { const count = useAtomValue(derivedAtom); return
count: {count}
; - }; + } - const Controls = () => { + function Controls() { const setCount = useSetAtom(countAtom); - return ( - <> - - - ); - }; + return ; + } const { findByText, getByText } = render( diff --git a/__tests__/derive/baseTests/react/async.test.tsx b/__tests__/derive/baseTests/react/async.test.tsx index ca65f4a..c4939f1 100644 --- a/__tests__/derive/baseTests/react/async.test.tsx +++ b/__tests__/derive/baseTests/react/async.test.tsx @@ -24,7 +24,7 @@ it('does not show async stale result', async () => { const committed: number[] = []; let resolve1 = () => {}; - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const onClick = async () => { setCount((c) => c + 1); @@ -37,15 +37,15 @@ it('does not show async stale result', async () => { ); - }; + } - const DelayedCounter = () => { + function DelayedCounter() { const [delayedCount] = useAtom(asyncCountAtom); useEffect(() => { committed.push(delayedCount); }); return
delayedCount: {delayedCount}
; - }; + } const { getByText, findByText } = render( <> @@ -90,18 +90,18 @@ it('does not show async stale result on derived atom', async () => { }); const derivedAtom = atom((get) => get(asyncAlwaysNullAtom)); - const DisplayAsyncValue = () => { + function DisplayAsyncValue() { const [asyncValue] = useAtom(asyncAlwaysNullAtom); return
async value: {JSON.stringify(asyncValue)}
; - }; + } - const DisplayDerivedValue = () => { + function DisplayDerivedValue() { const [derivedValue] = useAtom(derivedAtom); return
derived value: {JSON.stringify(derivedValue)}
; - }; + } - const Test = () => { + function Test() { const [count, setCount] = useAtom(countAtom); return (
@@ -115,7 +115,7 @@ it('does not show async stale result on derived atom', async () => {
); - }; + } const { getByText, queryByText } = render( @@ -165,7 +165,7 @@ it('works with async get with extra deps', async () => { return get(countAtom); }); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -173,12 +173,12 @@ it('works with async get with extra deps', async () => { ); - }; + } - const DelayedCounter = () => { + function DelayedCounter() { const [delayedCount] = useAtom(asyncCountAtom); return
delayedCount: {delayedCount}
; - }; + } const { getByText, findByText } = render( @@ -214,10 +214,10 @@ it('reuses promises on initial read', async () => { return 'ready'; }); - const Child = () => { + function Child() { const [str] = useAtom(asyncAtom); return
{str}
; - }; + } const { findByText, findAllByText } = render( @@ -245,17 +245,15 @@ it('uses multiple async atoms at once', async () => { return 'ready2'; }); - const Component = () => { + function Component() { const [some] = useAtom(someAtom); const [some2] = useAtom(someAtom2); return ( - <> -
- {some} {some2} -
- +
+ {some} {some2} +
); - }; + } const { getByText, findByText } = render( @@ -281,7 +279,7 @@ it('uses async atom in the middle of dependency chain', async () => { }); const delayedCountAtom = atom((get) => get(asyncCountAtom)); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [delayedCount] = useAtom(delayedCountAtom); return ( @@ -292,7 +290,7 @@ it('uses async atom in the middle of dependency chain', async () => { ); - }; + } const { getByText, findByText } = render( @@ -320,15 +318,15 @@ it('updates an async atom in child useEffect on remount without setTimeout', asy async (get, set) => set(countAtom, get(countAtom) + 1), ); - const Counter = () => { + function Counter() { const [count, incCount] = useAtom(asyncCountAtom); useEffect(() => { incCount(); }, [incCount]); return
count: {count}
; - }; + } - const Parent = () => { + function Parent() { const [toggle, setToggle] = useAtom(toggleAtom); return ( <> @@ -336,14 +334,12 @@ it('updates an async atom in child useEffect on remount without setTimeout', asy {toggle ? :
no child
} ); - }; + } const { getByText, findByText } = render( - <> - - - - , + + + , ); await findByText('count: 0'); @@ -371,15 +367,15 @@ it('updates an async atom in child useEffect on remount', async () => { }, ); - const Counter = () => { + function Counter() { const [count, incCount] = useAtom(asyncCountAtom); useEffect(() => { incCount(); }, [incCount]); return
count: {count}
; - }; + } - const Parent = () => { + function Parent() { const [toggle, setToggle] = useAtom(toggleAtom); return ( <> @@ -387,14 +383,12 @@ it('updates an async atom in child useEffect on remount', async () => { {toggle ? :
no child
} ); - }; + } const { getByText, findByText } = render( - <> - - - - , + + + , ); await findByText('loading'); @@ -429,12 +423,12 @@ it('async get and useEffect on parent', async () => { return 'resolved'; }); - const AsyncComponent = () => { + function AsyncComponent() { const [text] = useAtom(asyncAtom); return
text: {text}
; - }; + } - const Parent = () => { + function Parent() { const [count, setCount] = useAtom(countAtom); useEffect(() => { setCount((c) => c + 1); @@ -446,14 +440,12 @@ it('async get and useEffect on parent', async () => { ); - }; + } const { getByText, findByText } = render( - <> - - - - , + + + , ); await findByText('loading'); @@ -472,12 +464,12 @@ it('async get with another dep and useEffect on parent', async () => { return count; }); - const AsyncComponent = () => { + function AsyncComponent() { const [count] = useAtom(asyncAtom); return
async: {count}
; - }; + } - const Parent = () => { + function Parent() { const [count, setCount] = useAtom(countAtom); useEffect(() => { setCount((c) => c + 1); @@ -489,14 +481,12 @@ it('async get with another dep and useEffect on parent', async () => { ); - }; + } const { getByText, findByText } = render( - <> - - - - , + + + , ); await findByText('loading'); @@ -524,12 +514,12 @@ it('set promise atom value on write (#304)', async () => { ); }); - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); return
count: {count * 1}
; - }; + } - const Parent = () => { + function Parent() { const [, dispatch] = useAtom(asyncAtom); return ( <> @@ -537,7 +527,7 @@ it('set promise atom value on write (#304)', async () => { ); - }; + } const { getByText, findByText } = render( @@ -567,7 +557,7 @@ it('uses async atom double chain (#306)', async () => { return get(asyncCountAtom); }); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [delayedCount] = useAtom(delayedCountAtom); return ( @@ -578,7 +568,7 @@ it('uses async atom double chain (#306)', async () => { ); - }; + } const { getByText, findByText } = render( @@ -609,10 +599,10 @@ it('uses an async atom that depends on another async atom', async () => { return 2; }); - const Counter = () => { + function Counter() { const [num] = useAtom(asyncAtom); return
num: {num}
; - }; + } const { findByText } = render( @@ -643,7 +633,7 @@ it('a derived atom from a newly created async atom (#351)', async () => { }; const derivedAtom = atom((get) => get(getAsyncAtom(get(countAtom)))); - const Counter = () => { + function Counter() { const [, setCount] = useAtom(countAtom); const [derived] = useAtom(derivedAtom); return ( @@ -654,14 +644,12 @@ it('a derived atom from a newly created async atom (#351)', async () => { ); - }; + } const { getByText, findByText } = render( - <> - - - - , + + + , ); await findByText('loading'); @@ -694,7 +682,7 @@ it('Handles synchronously invoked async set (#375)', async () => { fetch(); }); - const ListDocuments = () => { + function ListDocuments() { const [loading] = useAtom(loadingAtom); const [document] = useAtom(documentAtom); const [, loadDocument] = useAtom(loadDocumentAtom); @@ -709,7 +697,7 @@ it('Handles synchronously invoked async set (#375)', async () => { {!loading &&
{document}
} ); - }; + } const { findByText } = render( @@ -730,7 +718,7 @@ it('async write self atom', async () => { set(countAtom, -1); }); - const Counter = () => { + function Counter() { const [count, inc] = useAtom(countAtom); return ( <> @@ -738,7 +726,7 @@ it('async write self atom', async () => { ); - }; + } const { getByText, findByText } = render( @@ -759,7 +747,7 @@ it('non suspense async write self atom with setTimeout (#389)', async () => { setTimeout(() => set(countAtom, -1)); }); - const Counter = () => { + function Counter() { const [count, inc] = useAtom(countAtom); return ( <> @@ -767,7 +755,7 @@ it('non suspense async write self atom with setTimeout (#389)', async () => { ); - }; + } const { getByText, findByText } = render( @@ -789,15 +777,15 @@ it('should override promise as atom value (#430)', async () => { set(countAtom, Promise.resolve(arg)); }); - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); return
count: {count * 1}
; - }; + } - const Control = () => { + function Control() { const [, setCount] = useAtom(setCountAtom); return ; - }; + } const { getByText, findByText } = render( @@ -828,15 +816,15 @@ it('combine two promise atom values (#442)', async () => { init(); }; - const Counter = () => { + function Counter() { const [count] = useAtom(derivedAtom); return
count: {count}
; - }; + } - const Control = () => { + function Control() { useAtom(initAtom); return null; - }; + } const { findByText } = render( @@ -862,15 +850,15 @@ it('set two promise atoms at once', async () => { set(count2Atom, Promise.resolve(2)); }); - const Counter = () => { + function Counter() { const [count] = useAtom(derivedAtom); return
count: {count}
; - }; + } - const Control = () => { + function Control() { const [, setCounts] = useAtom(setCountsAtom); return ; - }; + } const { getByText, findByText } = render( @@ -902,15 +890,15 @@ it('async write chain', async () => { set(countAtom, 3); }); - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); return
count: {count}
; - }; + } - const Control = () => { + function Control() { const [, invoke] = useAtom(controlAtom); return ; - }; + } const { getByText, findByText } = render( @@ -943,12 +931,12 @@ it('async atom double chain without setTimeout (#751)', async () => { const derivedAsyncAtom = atom(async (get) => get(asyncAtom)); const anotherAsyncAtom = atom(async (get) => get(derivedAsyncAtom)); - const AsyncComponent = () => { + function AsyncComponent() { const [text] = useAtom(anotherAsyncAtom); return
async: {text}
; - }; + } - const Parent = () => { + function Parent() { // Use useAtom to reproduce the issue const [, setEnabled] = useAtom(enabledAtom); return ( @@ -965,7 +953,7 @@ it('async atom double chain without setTimeout (#751)', async () => { ); - }; + } const { getByText, findByText } = render( @@ -1002,12 +990,12 @@ it('async atom double chain with setTimeout', async () => { return get(derivedAsyncAtom); }); - const AsyncComponent = () => { + function AsyncComponent() { const [text] = useAtom(anotherAsyncAtom); return
async: {text}
; - }; + } - const Parent = () => { + function Parent() { // Use useAtom to reproduce the issue const [, setEnabled] = useAtom(enabledAtom); return ( @@ -1024,7 +1012,7 @@ it('async atom double chain with setTimeout', async () => { ); - }; + } const { getByText, findByText } = render( @@ -1070,12 +1058,12 @@ it('update unmounted async atom with intermediate atom', async () => { return get(tmpAtom); }); - const DerivedCounter = () => { + function DerivedCounter() { const [derived] = useAtom(derivedAtom); return
derived: {derived}
; - }; + } - const Control = () => { + function Control() { const [, setEnabled] = useAtom(enabledAtom); const [, setCount] = useAtom(countAtom); return ( @@ -1084,7 +1072,7 @@ it('update unmounted async atom with intermediate atom', async () => { ); - }; + } const { getByText, findByText } = render( @@ -1129,7 +1117,7 @@ it('multiple derived atoms with dependency chaining and async write (#813)', asy const itemAName = atom((get) => get(itemA)?.name); const itemBName = atom((get) => get(itemB)?.name); - const App = () => { + function App() { const [aName] = useAtom(itemAName); const [bName] = useAtom(itemBName); return ( @@ -1138,7 +1126,7 @@ it('multiple derived atoms with dependency chaining and async write (#813)', asy
bName: {bName}
); - }; + } const { getByText } = render( diff --git a/__tests__/derive/baseTests/react/async2.test.tsx b/__tests__/derive/baseTests/react/async2.test.tsx index 78e65ed..13e587b 100644 --- a/__tests__/derive/baseTests/react/async2.test.tsx +++ b/__tests__/derive/baseTests/react/async2.test.tsx @@ -1,9 +1,9 @@ import { StrictMode, Suspense } from 'react'; import { fireEvent, render, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; +import assert from 'minimalistic-assert'; import { useAtom, useAtomValue, useSetAtom } from '../../../../jotai/react'; import { atom } from '../../../../jotai/vanilla'; -import assert from 'minimalistic-assert'; describe('useAtom delay option test', () => { it('suspend for Promise.resolve without delay option', async () => { @@ -16,19 +16,15 @@ describe('useAtom delay option test', () => { return Promise.resolve(count); }); - const Component = () => { + function Component() { const count = useAtomValue(asyncAtom); return
count: {count}
; - }; + } - const Controls = () => { + function Controls() { const setCount = useSetAtom(countAtom); - return ( - <> - - - ); - }; + return ; + } const { getByText, findByText } = render( @@ -57,19 +53,15 @@ describe('useAtom delay option test', () => { return Promise.resolve(count); }); - const Component = () => { + function Component() { const count = useAtomValue(asyncAtom, { delay: 0 }); return
count: {count}
; - }; + } - const Controls = () => { + function Controls() { const setCount = useSetAtom(countAtom); - return ( - <> - - - ); - }; + return ; + } const { getByText, findByText } = render( @@ -108,26 +100,22 @@ describe('atom read function setSelf option test', () => { promiseCache.set(promise, v); setSelf(); }); - return 'pending' + count; + return `pending${count}`; }, (_get, set) => { set(refreshAtom, (c) => c + 1); }, ); - const Component = () => { + function Component() { const text = useAtomValue(derivedAtom); return
text: {text}
; - }; + } - const Controls = () => { + function Controls() { const setCount = useSetAtom(countAtom); - return ( - <> - - - ); - }; + return ; + } const { getByText, findByText } = render( @@ -232,19 +220,15 @@ describe('infinite pending', () => { return count; }); - const Component = () => { + function Component() { const count = useAtomValue(asyncAtom); return
count: {count}
; - }; + } - const Controls = () => { + function Controls() { const setCount = useSetAtom(countAtom); - return ( - <> - - - ); - }; + return ; + } const { getByText, findByText } = render( @@ -277,7 +261,7 @@ describe('write to async atom twice', () => { return get(asyncAtom); }); - const Component = () => { + function Component() { const count = useAtomValue(asyncAtom); const write = useSetAtom(writer); return ( @@ -286,7 +270,7 @@ describe('write to async atom twice', () => { ); - }; + } const { findByText, getByText } = render( @@ -310,7 +294,7 @@ describe('write to async atom twice', () => { return get(asyncAtom); }); - const Component = () => { + function Component() { const count = useAtomValue(asyncAtom); const write = useSetAtom(writer); return ( @@ -319,7 +303,7 @@ describe('write to async atom twice', () => { ); - }; + } const { findByText, getByText } = render( @@ -343,7 +327,7 @@ describe('write to async atom twice', () => { return get(asyncAtom); }); - const Component = () => { + function Component() { const count = useAtomValue(asyncAtom); const write = useSetAtom(writer); return ( @@ -352,7 +336,7 @@ describe('write to async atom twice', () => { ); - }; + } const { findByText, getByText } = render( diff --git a/__tests__/derive/baseTests/react/basic.test.tsx b/__tests__/derive/baseTests/react/basic.test.tsx index 25dcd9c..8aad332 100644 --- a/__tests__/derive/baseTests/react/basic.test.tsx +++ b/__tests__/derive/baseTests/react/basic.test.tsx @@ -35,7 +35,7 @@ const useCommitCount = () => { it('uses a primitive atom', async () => { const countAtom = atom(0); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -43,7 +43,7 @@ it('uses a primitive atom', async () => { ); - }; + } const { getByText, findByText } = render( @@ -61,7 +61,7 @@ it('uses a read-only derived atom', async () => { const countAtom = atom(0); const doubledCountAtom = atom((get) => get(countAtom) * 2); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [doubledCount] = useAtom(doubledCountAtom); return ( @@ -71,7 +71,7 @@ it('uses a read-only derived atom', async () => { ); - }; + } const { getByText } = render( @@ -97,7 +97,7 @@ it('uses a read-write derived atom', async () => { (get, set, update: number) => set(countAtom, get(countAtom) + update), ); - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); const [doubledCount, increaseCount] = useAtom(doubledCountAtom); return ( @@ -107,7 +107,7 @@ it('uses a read-write derived atom', async () => { ); - }; + } const { getByText } = render( @@ -132,16 +132,16 @@ it('uses a write-only derived atom', async () => { set(countAtom, get(countAtom) + 1), ); - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); return (
commits: {useCommitCount()}, count: {count}
); - }; + } - const Control = () => { + function Control() { const [, increment] = useAtom(incrementCountAtom); return ( <> @@ -149,7 +149,7 @@ it('uses a write-only derived atom', async () => { ); - }; + } const { getByText } = render( <> @@ -176,7 +176,7 @@ it('only re-renders if value has changed', async () => { const productAtom = atom((get) => get(count1Atom) * get(count2Atom)); type Props = { countAtom: typeof count1Atom; name: string }; - const Counter = ({ countAtom, name }: Props) => { + function Counter({ countAtom, name }: Props) { const [count, setCount] = useAtom(countAtom); return ( <> @@ -186,18 +186,16 @@ it('only re-renders if value has changed', async () => { ); - }; + } - const Product = () => { + function Product() { const [product] = useAtom(productAtom); return ( - <> -
- commits: {useCommitCount()}, product: {product} -
- +
+ commits: {useCommitCount()}, product: {product} +
); - }; + } const { getByText } = render( <> @@ -246,10 +244,10 @@ it('re-renders a time delayed derived atom with the same initial value (#947)', return 1; }); - const App = () => { + function App() { const [value] = useAtom(cAtom); return <>{value}; - }; + } const { findByText } = render( @@ -268,7 +266,7 @@ it('works with async get', async () => { return get(countAtom); }); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [delayedCount] = useAtom(asyncCountAtom); return ( @@ -280,14 +278,12 @@ it('works with async get', async () => { ); - }; + } const { getByText, findByText } = render( - <> - - - - , + + + , ); await findByText('loading'); @@ -311,7 +307,7 @@ it('works with async get without setTimeout', async () => { return get(countAtom); }); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [delayedCount] = useAtom(asyncCountAtom); return ( @@ -322,7 +318,7 @@ it('works with async get without setTimeout', async () => { ); - }; + } const { getByText, findByText } = render( @@ -356,7 +352,7 @@ it('uses atoms with tree dependencies', async () => { }, ); - const Counter = () => { + function Counter() { const [count] = useAtom(leftAtom); const [, setCount] = useAtom(rightAtom); return ( @@ -367,13 +363,9 @@ it('uses atoms with tree dependencies', async () => { ); - }; + } - const { getByText, findByText } = render( - <> - - , - ); + const { getByText, findByText } = render(); await findByText('commits: 1, count: 0'); @@ -397,7 +389,7 @@ it('runs update only once in StrictMode', async () => { }, ); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(derivedAtom); return ( <> @@ -405,7 +397,7 @@ it('runs update only once in StrictMode', async () => { ); - }; + } const { getByText, findByText } = render( @@ -432,7 +424,7 @@ it('uses an async write-only atom', async () => { }, ); - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); const [, setCount] = useAtom(asyncCountAtom); return ( @@ -443,13 +435,9 @@ it('uses an async write-only atom', async () => { ); - }; + } - const { getByText, findByText } = render( - <> - - , - ); + const { getByText, findByText } = render(); await findByText('commits: 1, count: 0'); @@ -465,7 +453,7 @@ it('uses a writable atom without read function', async () => { set(countAtom, get(countAtom) + 10 * v); }); - const Counter = () => { + function Counter() { const [count, addCount10Times] = useAtom(countAtom); return ( <> @@ -473,7 +461,7 @@ it('uses a writable atom without read function', async () => { ); - }; + } const { getByText, findByText } = render( @@ -491,19 +479,15 @@ it('uses a writable atom without read function', async () => { it('can write an atom value on useEffect', async () => { const countAtom = atom(0); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); useEffect(() => { setCount((c) => c + 1); }, [setCount]); return
count: {count}
; - }; + } - const { findByText } = render( - <> - - , - ); + const { findByText } = render(); await findByText('count: 1'); }); @@ -511,18 +495,18 @@ it('can write an atom value on useEffect', async () => { it('can write an atom value on useEffect in children', async () => { const countAtom = atom(0); - const Child = ({ + function Child({ setCount, }: { setCount: (f: (c: number) => number) => void; - }) => { + }) { useEffect(() => { setCount((c) => c + 1); }, [setCount]); return null; - }; + } - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return (
@@ -531,13 +515,9 @@ it('can write an atom value on useEffect in children', async () => {
); - }; + } - const { findByText } = render( - <> - - , - ); + const { findByText } = render(); await findByText('count: 2'); }); @@ -552,7 +532,7 @@ it('only invoke read function on use atom', async () => { expect(readCount).toBe(0); // do not invoke on atom() - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [doubledCount] = useAtom(doubledCountAtom); return ( @@ -564,13 +544,9 @@ it('only invoke read function on use atom', async () => { ); - }; + } - const { getByText, findByText } = render( - <> - - , - ); + const { getByText, findByText } = render(); await findByText('commits: 1, count: 0, readCount: 1, doubled: 0'); @@ -593,7 +569,7 @@ it('uses a read-write derived atom with two primitive atoms', async () => { set(countBAtom, get(countBAtom) + 1); }); - const Counter = () => { + function Counter() { const [countA, setCountA] = useAtom(countAAtom); const [countB, setCountB] = useAtom(countBAtom); const [sum, reset] = useAtom(sumAtom); @@ -609,7 +585,7 @@ it('uses a read-write derived atom with two primitive atoms', async () => { ); - }; + } const { getByText, findByText } = render( @@ -637,7 +613,7 @@ it('updates a derived atom in useEffect with two primitive atoms', async () => { const countBAtom = atom(1); const sumAtom = atom((get) => get(countAAtom) + get(countBAtom)); - const Counter = () => { + function Counter() { const [countA, setCountA] = useAtom(countAAtom); const [countB, setCountB] = useAtom(countBAtom); const [sum] = useAtom(sumAtom); @@ -652,13 +628,9 @@ it('updates a derived atom in useEffect with two primitive atoms', async () => { ); - }; + } - const { getByText, findByText } = render( - <> - - , - ); + const { getByText, findByText } = render(); await findByText('countA: 1, countB: 1, sum: 2'); @@ -670,15 +642,15 @@ it('updates two atoms in child useEffect', async () => { const countAAtom = atom(0); const countBAtom = atom(10); - const Child = () => { + function Child() { const [countB, setCountB] = useAtom(countBAtom); useEffect(() => { setCountB((c) => c + 1); }, [setCountB]); return
countB: {countB}
; - }; + } - const Counter = () => { + function Counter() { const [countA, setCountA] = useAtom(countAAtom); useEffect(() => { setCountA((c) => c + 1); @@ -689,13 +661,9 @@ it('updates two atoms in child useEffect', async () => { {countA > 0 && } ); - }; + } - const { getByText } = render( - <> - - , - ); + const { getByText } = render(); await waitFor(() => { getByText('countA: 1'); @@ -707,7 +675,7 @@ it('set atom right after useEffect (#208)', async () => { const countAtom = atom(0); const effectFn = jest.fn(); - const Child = () => { + function Child() { const [count, setCount] = useAtom(countAtom); const [, setState] = useState(null); // rAF does not repro, so schedule update intentionally in render @@ -721,16 +689,16 @@ it('set atom right after useEffect (#208)', async () => { setState(null); // this is important to repro (set something stable) }, [count, setState]); return
count: {count}
; - }; + } - const Parent = () => { + function Parent() { const [, setCount] = useAtom(countAtom); useEffect(() => { setCount(1); // requestAnimationFrame(() => setCount(2)) }, [setCount]); return ; - }; + } const { findByText } = render( @@ -746,7 +714,7 @@ it('changes atom from parent (#273, #275)', async () => { const atomA = atom({ id: 'a' }); const atomB = atom({ id: 'b' }); - const Item = ({ id }: { id: string }) => { + function Item({ id }: { id: string }) { const a = useMemo(() => (id === 'a' ? atomA : atomB), [id]); const [atomValue] = useAtom(a); return ( @@ -754,9 +722,9 @@ it('changes atom from parent (#273, #275)', async () => { commits: {useCommitCount()}, id: {atomValue.id} ); - }; + } - const App = () => { + function App() { const [id, setId] = useState('a'); return (
@@ -765,13 +733,9 @@ it('changes atom from parent (#273, #275)', async () => {
); - }; + } - const { getByText, findByText } = render( - <> - - , - ); + const { getByText, findByText } = render(); await findByText('commits: 1, id: a'); @@ -790,7 +754,7 @@ it('should be able to use a double derived atom twice and useEffect (#373)', asy const doubleAtom = atom((get) => get(countAtom) * 2); const fourfoldAtom = atom((get) => get(doubleAtom) * 2); - const App = () => { + function App() { const [count, setCount] = useAtom(countAtom); const [fourfold] = useAtom(fourfoldAtom); const [fourfold2] = useAtom(fourfoldAtom); @@ -805,7 +769,7 @@ it('should be able to use a double derived atom twice and useEffect (#373)', asy ); - }; + } const { getByText, findByText } = render( @@ -823,7 +787,7 @@ it('write self atom (undocumented usage)', async () => { set(countAtom, get(countAtom) + 1); }); - const Counter = () => { + function Counter() { const [count, inc] = useAtom(countAtom); return ( <> @@ -831,7 +795,7 @@ it('write self atom (undocumented usage)', async () => { ); - }; + } const { getByText, findByText } = render( @@ -859,14 +823,10 @@ it('async chain for multiple sync and async atoms (#443)', async () => { ); const countAtom = atom((get) => get(sumAtom)); - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); - return ( - <> -
count: {count}
- - ); - }; + return
count: {count}
; + } const { findByText } = render( @@ -885,7 +845,7 @@ it('sync re-renders with useState re-renders (#827)', async () => { const atom2 = atom('atom2'); const atoms = [atom0, atom1, atom2]; - const App = () => { + function App() { const [currentAtomIndex, setCurrentAtomIndex] = useState(0); const rotateAtoms = () => { setCurrentAtomIndex((prev) => (prev + 1) % atoms.length); @@ -901,12 +861,8 @@ it('sync re-renders with useState re-renders (#827)', async () => { ); - }; - const { findByText, getByText } = render( - <> - - , - ); + } + const { findByText, getByText } = render(); await findByText('commits: 1'); await userEvent.click(getByText('rotate')); @@ -925,14 +881,14 @@ it('chained derive atom with onMount and useEffect (#897)', async () => { count: get(derivedAtom), })); - const Counter = () => { + function Counter() { const [, setCount] = useAtom(countAtom); const [{ count }] = useAtom(derivedObjectAtom); useEffect(() => { setCount(1); }, [setCount]); return
count: {count}
; - }; + } const { findByText } = render( @@ -956,11 +912,11 @@ it('onMount is not called when atom value is accessed from writeGetter in derive get(aAtom); }); - const App = () => { + function App() { const [, action] = useAtom(bAtom); useEffect(() => action(), [action]); return null; - }; + } render( @@ -976,7 +932,7 @@ it('useAtom returns consistent value with input with changing atoms (#1235)', as const countAtom = atom(0); const valueAtoms = [atom(0), atom(1)]; - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [value] = useAtom(valueAtoms[count] as PrimitiveAtom); if (count !== value) { @@ -988,7 +944,7 @@ it('useAtom returns consistent value with input with changing atoms (#1235)', as ); - }; + } const { getByText, findByText } = render( diff --git a/__tests__/derive/baseTests/react/dependency.test.tsx b/__tests__/derive/baseTests/react/dependency.test.tsx index 476010d..ac987f6 100644 --- a/__tests__/derive/baseTests/react/dependency.test.tsx +++ b/__tests__/derive/baseTests/react/dependency.test.tsx @@ -18,7 +18,7 @@ it('works with 2 level dependencies', async () => { const doubledAtom = atom((get) => get(countAtom) * 2); const tripledAtom = atom((get) => get(doubledAtom) * 3); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [doubledCount] = useAtom(doubledAtom); const [tripledCount] = useAtom(tripledAtom); @@ -31,13 +31,9 @@ it('works with 2 level dependencies', async () => { ); - }; + } - const { getByText, findByText } = render( - <> - - , - ); + const { getByText, findByText } = render(); await findByText('commits: 1, count: 1, doubled: 2, tripled: 6'); @@ -53,7 +49,7 @@ it('works a primitive atom and a dependent async atom', async () => { return get(countAtom) * 2; }); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [doubledCount] = useAtom(doubledAtom); return ( @@ -64,7 +60,7 @@ it('works a primitive atom and a dependent async atom', async () => { ); - }; + } const { getByText, findByText } = render( @@ -94,7 +90,7 @@ it('should keep an atom value even if unmounted', async () => { const derivedFn = jest.fn((get: Getter) => get(countAtom)); const derivedAtom = atom(derivedFn); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -102,14 +98,14 @@ it('should keep an atom value even if unmounted', async () => { ); - }; + } - const DerivedCounter = () => { + function DerivedCounter() { const [derived] = useAtom(derivedAtom); return
derived: {derived}
; - }; + } - const Parent = () => { + function Parent() { const [show, setShow] = useState(true); return (
@@ -124,7 +120,7 @@ it('should keep an atom value even if unmounted', async () => { )}
); - }; + } const { getByText } = render( @@ -164,7 +160,7 @@ it('should keep a dependent atom value even if unmounted', async () => { const derivedFn = jest.fn((get: Getter) => get(countAtom)); const derivedAtom = atom(derivedFn); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -172,14 +168,14 @@ it('should keep a dependent atom value even if unmounted', async () => { ); - }; + } - const DerivedCounter = () => { + function DerivedCounter() { const [derived] = useAtom(derivedAtom); return
derived: {derived}
; - }; + } - const Parent = () => { + function Parent() { const [showDerived, setShowDerived] = useState(true); return (
@@ -187,7 +183,7 @@ it('should keep a dependent atom value even if unmounted', async () => { {showDerived ? : }
); - }; + } const { getByText, findByText } = render( @@ -216,7 +212,7 @@ it('should bail out updating if not changed', async () => { const derivedFn = jest.fn((get: Getter) => get(countAtom)); const derivedAtom = atom(derivedFn); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -224,12 +220,12 @@ it('should bail out updating if not changed', async () => { ); - }; + } - const DerivedCounter = () => { + function DerivedCounter() { const [derived] = useAtom(derivedAtom); return
derived: {derived}
; - }; + } const { getByText } = render( @@ -261,7 +257,7 @@ it('should bail out updating if not changed, 2 level', async () => { const getAnotherCountFn = jest.fn((get: Getter) => get(objAtom).anotherCount); const anotherCountAtom = atom(getAnotherCountFn); - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); const [, setData] = useAtom(dataAtom); return ( @@ -276,12 +272,12 @@ it('should bail out updating if not changed, 2 level', async () => { ); - }; + } - const DerivedCounter = () => { + function DerivedCounter() { const [anotherCount] = useAtom(anotherCountAtom); return
anotherCount: {anotherCount}
; - }; + } const { getByText } = render( @@ -317,7 +313,7 @@ it('derived atom to update base atom in callback', async () => { }, ); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [doubledCount, dispatch] = useAtom(doubledAtom); return ( @@ -330,13 +326,9 @@ it('derived atom to update base atom in callback', async () => { ); - }; + } - const { getByText, findByText } = render( - <> - - , - ); + const { getByText, findByText } = render(); await findByText('commits: 1, count: 1, doubled: 2'); @@ -351,7 +343,7 @@ it('can read sync derived atom in write without initializing', async () => { set(countAtom, get(doubledAtom) / 2 + num); }); - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); const [, add] = useAtom(addAtom); return ( @@ -360,7 +352,7 @@ it('can read sync derived atom in write without initializing', async () => { ); - }; + } const { getByText, findByText } = render( @@ -381,7 +373,7 @@ it('can remount atoms with dependency (#490)', async () => { const countAtom = atom(0); const derivedAtom = atom((get) => get(countAtom)); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -389,14 +381,14 @@ it('can remount atoms with dependency (#490)', async () => { ); - }; + } - const DerivedCounter = () => { + function DerivedCounter() { const [derived] = useAtom(derivedAtom); return
derived: {derived}
; - }; + } - const Parent = () => { + function Parent() { const [showChildren, setShowChildren] = useState(true); return (
@@ -411,7 +403,7 @@ it('can remount atoms with dependency (#490)', async () => { )}
); - }; + } const { getByText } = render( @@ -468,7 +460,7 @@ it('can remount atoms with intermediate atom', async () => { return get(resultAtom); }); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -476,14 +468,14 @@ it('can remount atoms with intermediate atom', async () => { ); - }; + } - const DerivedCounter = () => { + function DerivedCounter() { const [derived] = useAtom(derivedAtom); return
derived: {derived}
; - }; + } - const Parent = () => { + function Parent() { const [showChildren, setShowChildren] = useState(true); return (
@@ -492,7 +484,7 @@ it('can remount atoms with intermediate atom', async () => { {showChildren ? :
hidden
}
); - }; + } const { getByText } = render( @@ -549,7 +541,7 @@ it('can update dependents with useEffect (#512)', async () => { return count * 2; }); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -557,14 +549,14 @@ it('can update dependents with useEffect (#512)', async () => { ); - }; + } - const DerivedCounter = () => { + function DerivedCounter() { const [derived] = useAtom(derivedAtom); return
derived: {derived}
; - }; + } - const Parent = () => { + function Parent() { const [, setEnabled] = useAtom(enabledAtom); useEffect(() => { setEnabled(true); @@ -575,7 +567,7 @@ it('can update dependents with useEffect (#512)', async () => { ); - }; + } const { getByText } = render( @@ -610,12 +602,12 @@ it('update unmounted atom with intermediate atom', async () => { return get(tmpAtom); }); - const DerivedCounter = () => { + function DerivedCounter() { const [derived] = useAtom(derivedAtom); return
derived: {derived}
; - }; + } - const Control = () => { + function Control() { const [, setEnabled] = useAtom(enabledAtom); const [, setCount] = useAtom(countAtom); return ( @@ -624,7 +616,7 @@ it('update unmounted atom with intermediate atom', async () => { ); - }; + } const { getByText, findByText } = render( @@ -657,19 +649,19 @@ it('Should bail for derived sync chains (#877)', async () => { return get(syncAtom); }); - const Input = () => { + function Input() { const [result] = useAtom(derivedAtom); return
{result}
; - }; + } - const ForceValue = () => { + function ForceValue() { const setText = useAtom(textAtom)[1]; return (
); - }; + } const { getByText, findByText } = render( @@ -701,19 +693,19 @@ it('Should bail for derived async chains (#877)', async () => { return get(asyncAtom); }); - const Input = () => { + function Input() { const [result] = useAtom(derivedAtom); return
{result}
; - }; + } - const ForceValue = () => { + function ForceValue() { const setText = useAtom(textAtom)[1]; return (
); - }; + } const { getByText, findByText } = render( @@ -744,7 +736,7 @@ it('update correctly with async updates (#1250)', async () => { return count; }); - const App = () => { + function App() { const setCount = useSetAtom(countAtom); const alsoCount = useAtomValue(alsoCountAtom); const countIsGreaterThanOne = useAtomValue(countIsGreaterThanOneAtom); @@ -759,7 +751,7 @@ it('update correctly with async updates (#1250)', async () => {
countIsGreaterThanOne: {countIsGreaterThanOne.toString()}
); - }; + } const { getByText } = render( @@ -792,19 +784,15 @@ describe('glitch free', () => { }); const derived3Atom = atom(computeValue); - const App = () => { + function App() { const value = useAtomValue(derived3Atom); return
value: {value}
; - }; + } - const Control = () => { + function Control() { const setCount = useSetAtom(baseAtom); - return ( - <> - - - ); - }; + return ; + } const { getByText, findByText } = render( @@ -833,19 +821,15 @@ describe('glitch free', () => { }); const derived3Atom = atom(computeValue); - const App = () => { + function App() { const value = useAtomValue(derived3Atom); return
value: {value}
; - }; + } - const Control = () => { + function Control() { const setCount = useSetAtom(baseAtom); - return ( - <> - - - ); - }; + return ; + } const { getByText, findByText } = render( @@ -876,19 +860,15 @@ describe('glitch free', () => { }); const derived4Atom = atom(computeValue); - const App = () => { + function App() { const value = useAtomValue(derived4Atom); return
value: {value}
; - }; + } - const Control = () => { + function Control() { const setCount = useSetAtom(baseAtom); - return ( - <> - - - ); - }; + return ; + } const { getByText, findByText } = render( @@ -912,21 +892,20 @@ it('should not call read function for unmounted atoms in StrictMode (#2076)', as | (((get: Getter) => number) & { mockClear: () => void }) | undefined; - const Component = () => { + function Component() { const memoizedAtomRef = useRef | null>(null); if (!memoizedAtomRef.current) { const derivedFn = jest.fn((get: Getter) => get(countAtom)); if (!firstDerivedFn) { - // eslint-disable-next-line react-compiler/react-compiler firstDerivedFn = derivedFn; } memoizedAtomRef.current = atom(derivedFn); } useAtomValue(memoizedAtomRef.current); return null; - }; + } - const Main = () => { + function Main() { const [show, setShow] = useState(true); const setCount = useSetAtom(countAtom); return ( @@ -943,7 +922,7 @@ it('should not call read function for unmounted atoms in StrictMode (#2076)', as {show && } ); - }; + } const { getByText } = render( @@ -973,7 +952,7 @@ it('works with unused hook (#2554)', async () => { set(isBarAtom, true); }); - const App = () => { + function App() { const activate = useSetAtom(activateAction); useAtomValue(isActive1Atom); const isRunning = useAtomValue(isActive2Atom); @@ -983,7 +962,7 @@ it('works with unused hook (#2554)', async () => { {isRunning ? 'running' : 'not running'} ); - }; + } const { getByText, findByText } = render( @@ -1008,11 +987,11 @@ it('works with async dependencies (#2565)', async () => { return base + count; }); - const Count = () => { + function Count() { const count = useAtomValue(totalCountAtom); return

count: {count}

; - }; - const App = () => { + } + function App() { const up = useSetAtom(countUpAction); return (
@@ -1022,7 +1001,7 @@ it('works with async dependencies (#2565)', async () => {
); - }; + } const { getByText, findByText } = render( diff --git a/__tests__/derive/baseTests/react/error.test.tsx b/__tests__/derive/baseTests/react/error.test.tsx index 33d9575..6db7e82 100644 --- a/__tests__/derive/baseTests/react/error.test.tsx +++ b/__tests__/derive/baseTests/react/error.test.tsx @@ -12,10 +12,12 @@ import userEvent from '@testing-library/user-event'; import { useAtom } from '../../../../jotai/react'; import { atom } from '../../../../jotai/vanilla'; +// eslint-disable-next-line no-console const consoleError = console.error; const errorMessages: string[] = []; beforeEach(() => { errorMessages.splice(0); + // eslint-disable-next-line no-console console.error = jest.fn((err: string) => { const match = /^(.*?)(\n|$)/.exec(err); if (match?.[1]) { @@ -24,6 +26,7 @@ beforeEach(() => { }); }); afterEach(() => { + // eslint-disable-next-line no-console console.error = consoleError; }); @@ -35,9 +38,11 @@ class ErrorBoundary extends Component< super(props); this.state = { hasError: false }; } + static getDerivedStateFromError(error: Error) { return { hasError: true, error }; } + render() { return this.state.hasError ? (
@@ -57,14 +62,10 @@ it('can throw an initial error in read function', async () => { throw new Error(); }); - const Counter = () => { + function Counter() { useAtom(errorAtom); - return ( - <> -
no error
- - ); - }; + return
no error
; + } const { findByText } = render( @@ -86,7 +87,7 @@ it('can throw an error in read function', async () => { throw new Error(); }); - const Counter = () => { + function Counter() { const [, setCount] = useAtom(countAtom); const [count] = useAtom(errorAtom); return ( @@ -96,7 +97,7 @@ it('can throw an error in read function', async () => { ); - }; + } const { getByText, findByText } = render( @@ -118,14 +119,10 @@ it('can throw an initial chained error in read function', async () => { }); const derivedAtom = atom((get) => get(errorAtom)); - const Counter = () => { + function Counter() { useAtom(derivedAtom); - return ( - <> -
no error
- - ); - }; + return
no error
; + } const { findByText } = render( @@ -148,7 +145,7 @@ it('can throw a chained error in read function', async () => { }); const derivedAtom = atom((get) => get(errorAtom)); - const Counter = () => { + function Counter() { const [, setCount] = useAtom(countAtom); const [count] = useAtom(derivedAtom); return ( @@ -158,7 +155,7 @@ it('can throw a chained error in read function', async () => { ); - }; + } const { getByText, findByText } = render( @@ -179,14 +176,10 @@ it('can throw an initial error in async read function', async () => { throw new Error(); }); - const Counter = () => { + function Counter() { useAtom(errorAtom); - return ( - <> -
no error
- - ); - }; + return
no error
; + } const { findByText } = render( @@ -210,7 +203,7 @@ it('can throw an error in async read function', async () => { throw new Error(); }); - const Counter = () => { + function Counter() { const [, setCount] = useAtom(countAtom); const [count] = useAtom(errorAtom); return ( @@ -220,7 +213,7 @@ it('can throw an error in async read function', async () => { ); - }; + } const { getByText, findByText } = render( @@ -247,12 +240,13 @@ it('can throw an error in write function', async () => { }, ); - const Counter = () => { + function Counter() { const [count, dispatch] = useAtom(errorAtom); const onClick = () => { try { dispatch(); } catch (e) { + // eslint-disable-next-line no-console console.error(e); } }; @@ -263,7 +257,7 @@ it('can throw an error in write function', async () => { ); - }; + } const { getByText, findByText } = render( @@ -287,12 +281,13 @@ it('can throw an error in async write function', async () => { }, ); - const Counter = () => { + function Counter() { const [count, dispatch] = useAtom(errorAtom); const onClick = async () => { try { await dispatch(); } catch (e) { + // eslint-disable-next-line no-console console.error(e); } }; @@ -303,7 +298,7 @@ it('can throw an error in async write function', async () => { ); - }; + } const { getByText, findByText } = render( @@ -337,12 +332,13 @@ it('can throw a chained error in write function', async () => { }, ); - const Counter = () => { + function Counter() { const [count, dispatch] = useAtom(chainedAtom); const onClick = () => { try { dispatch(); } catch (e) { + // eslint-disable-next-line no-console console.error(e); } }; @@ -353,7 +349,7 @@ it('can throw a chained error in write function', async () => { ); - }; + } const { getByText, findByText } = render( @@ -371,7 +367,7 @@ it('can throw a chained error in write function', async () => { it('throws an error while updating in effect', async () => { const countAtom = atom(0); - const Counter = () => { + function Counter() { const [, setCount] = useAtom(countAtom); useEffect(() => { try { @@ -379,15 +375,12 @@ it('throws an error while updating in effect', async () => { throw new Error('err_updating_in_effect'); }); } catch (e) { + // eslint-disable-next-line no-console console.error(e); } }, [setCount]); - return ( - <> -
no error
- - ); - }; + return
no error
; + } const { findByText } = render( @@ -406,7 +399,7 @@ describe('throws an error while updating in effect cleanup', () => { let doubleSetCount = false; - const Counter = () => { + function Counter() { const [, setCount] = useAtom(countAtom); useEffect(() => { return () => { @@ -418,14 +411,10 @@ describe('throws an error while updating in effect cleanup', () => { }); }; }, [setCount]); - return ( - <> -
no error
- - ); - }; + return
no error
; + } - const Main = () => { + function Main() { const [hide, setHide] = useState(false); return ( <> @@ -433,15 +422,13 @@ describe('throws an error while updating in effect cleanup', () => { {!hide && } ); - }; + } it('[DEV-ONLY] single setCount', async () => { const { getByText, findByText } = render( - <> - -
- - , + +
+ , ); await findByText('no error'); @@ -463,11 +450,9 @@ describe('throws an error while updating in effect cleanup', () => { doubleSetCount = true; const { getByText, findByText } = render( - <> - -
- - , + +
+ , ); await findByText('no error'); @@ -490,10 +475,10 @@ describe('error recovery', () => { const createCounter = () => { const counterAtom = atom(0); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(counterAtom); return ; - }; + } return { Counter, counterAtom }; }; @@ -511,9 +496,9 @@ describe('error recovery', () => { return value; }); - const Display = () => { + function Display() { return
Value: {useAtom(syncAtom)[0]}
; - }; + } const { getByText, findByText } = render( @@ -543,9 +528,9 @@ describe('error recovery', () => { return value; }); - const Display = () => { + function Display() { return
Value: {useAtom(asyncAtom)[0]}
; - }; + } const { getByText, findByText } = render( diff --git a/__tests__/derive/baseTests/react/items.test.tsx b/__tests__/derive/baseTests/react/items.test.tsx index a43968f..6dbc309 100644 --- a/__tests__/derive/baseTests/react/items.test.tsx +++ b/__tests__/derive/baseTests/react/items.test.tsx @@ -13,13 +13,13 @@ it('remove an item, then add another', async () => { let itemIndex = 0; const itemsAtom = atom[]>([]); - const ListItem = ({ + function ListItem({ itemAtom, remove, }: { itemAtom: PrimitiveAtom; remove: () => void; - }) => { + }) { const [item, setItem] = useAtom(itemAtom); const toggle = () => setItem((prev) => ({ ...prev, checked: !prev.checked })); @@ -32,9 +32,9 @@ it('remove an item, then add another', async () => { ); - }; + } - const List = () => { + function List() { const [items, setItems] = useAtom(itemsAtom); const addItem = () => { setItems((prev) => [ @@ -59,7 +59,7 @@ it('remove an item, then add another', async () => { ); - }; + } const { getByText, findByText } = render( @@ -118,13 +118,13 @@ it('add an item with filtered list', async () => { return items.filter((atom) => !get(atom).checked); }); - const ListItem = ({ + function ListItem({ itemAtom, remove, }: { itemAtom: PrimitiveAtom; remove: () => void; - }) => { + }) { const [item, setItem] = useAtom(itemAtom); const toggle = () => setItem((prev) => ({ ...prev, checked: !prev.checked })); @@ -137,9 +137,9 @@ it('add an item with filtered list', async () => { ); - }; + } - const Filter = () => { + function Filter() { const [filter, setFilter] = useAtom(filterAtom); return ( <> @@ -149,13 +149,13 @@ it('add an item with filtered list', async () => { ); - }; + } - const FilteredList = ({ + function FilteredList({ removeItem, }: { removeItem: (itemAtom: PrimitiveAtom) => void; - }) => { + }) { const [items] = useAtom(filteredAtom); return (
    @@ -168,9 +168,9 @@ it('add an item with filtered list', async () => { ))}
); - }; + } - const List = () => { + function List() { const [, setItems] = useAtom(setItemsAtom); const addItem = () => { setItems((prev) => [ @@ -188,7 +188,7 @@ it('add an item with filtered list', async () => { ); - }; + } const { getByText, findByText } = render( diff --git a/__tests__/derive/baseTests/react/onmount.test.tsx b/__tests__/derive/baseTests/react/onmount.test.tsx index 22fa74d..87f152e 100644 --- a/__tests__/derive/baseTests/react/onmount.test.tsx +++ b/__tests__/derive/baseTests/react/onmount.test.tsx @@ -9,7 +9,7 @@ it('one atom, one effect', async () => { const onMountFn = jest.fn(() => {}); countAtom.onMount = onMountFn; - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -17,13 +17,9 @@ it('one atom, one effect', async () => { ); - }; + } - const { getByText, findByText } = render( - <> - - , - ); + const { getByText, findByText } = render(); await findByText('count: 1'); expect(onMountFn).toHaveBeenCalledTimes(1); @@ -41,7 +37,7 @@ it('two atoms, one each', async () => { countAtom.onMount = onMountFn; countAtom2.onMount = onMountFn2; - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const [count2, setCount2] = useAtom(countAtom2); return ( @@ -58,13 +54,9 @@ it('two atoms, one each', async () => { ); - }; + } - const { getByText } = render( - <> - - , - ); + const { getByText } = render(); await waitFor(() => { getByText('count: 1'); @@ -89,20 +81,12 @@ it('one derived atom, one onMount', async () => { const onMountFn = jest.fn(() => {}); countAtom.onMount = onMountFn; - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom2); - return ( - <> -
count: {count}
- - ); - }; + return
count: {count}
; + } - const { findByText } = render( - <> - - , - ); + const { findByText } = render(); await findByText('count: 1'); expect(onMountFn).toHaveBeenCalledTimes(1); @@ -115,16 +99,12 @@ it('mount/unmount test', async () => { const onMountFn = jest.fn(() => onUnMountFn); countAtom.onMount = onMountFn; - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); - return ( - <> -
count: {count}
- - ); - }; + return
count: {count}
; + } - const Display = () => { + function Display() { const [display, setDisplay] = useState(true); return ( <> @@ -132,13 +112,9 @@ it('mount/unmount test', async () => { ); - }; + } - const { getByText } = render( - <> - - , - ); + const { getByText } = render(); expect(onMountFn).toHaveBeenCalledTimes(1); expect(onUnMountFn).toHaveBeenCalledTimes(0); @@ -166,16 +142,12 @@ it('one derived atom, one onMount for the derived one, and one for the regular a const derivedOnMountFn = jest.fn(() => derivedOnUnMountFn); derivedAtom.onMount = derivedOnMountFn; - const Counter = () => { + function Counter() { const [count] = useAtom(derivedAtom); - return ( - <> -
count: {count}
- - ); - }; + return
count: {count}
; + } - const Display = () => { + function Display() { const [display, setDisplay] = useState(true); return ( <> @@ -183,13 +155,9 @@ it('one derived atom, one onMount for the derived one, and one for the regular a ); - }; + } - const { getByText } = render( - <> - - , - ); + const { getByText } = render(); expect(derivedOnMountFn).toHaveBeenCalledTimes(1); expect(derivedOnUnMountFn).toHaveBeenCalledTimes(0); expect(onMountFn).toHaveBeenCalledTimes(1); @@ -231,15 +199,11 @@ it('mount/unMount order', async () => { }); derivedAtom.onMount = derivedOnMountFn; - const Counter2 = () => { + function Counter2() { const [count] = useAtom(derivedAtom); - return ( - <> -
count: {count}
- - ); - }; - const Counter = () => { + return
count: {count}
; + } + function Counter() { const [count] = useAtom(countAtom); const [display, setDisplay] = useState(false); return ( @@ -249,9 +213,9 @@ it('mount/unMount order', async () => { {display ? : null} ); - }; + } - const Display = () => { + function Display() { const [display, setDisplay] = useState(false); return ( <> @@ -259,7 +223,7 @@ it('mount/unMount order', async () => { ); - }; + } const { getByText } = render( @@ -303,16 +267,12 @@ it('mount/unmount test with async atom', async () => { const onMountFn = jest.fn(() => onUnMountFn); countAtom.onMount = onMountFn; - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); - return ( - <> -
count: {count}
- - ); - }; + return
count: {count}
; + } - const Display = () => { + function Display() { const [display, setDisplay] = useState(true); return ( <> @@ -320,14 +280,12 @@ it('mount/unmount test with async atom', async () => { ); - }; + } const { getByText, findByText } = render( - <> - - - - , + + + , ); await findByText('loading'); @@ -361,16 +319,12 @@ it('subscription usage test', async () => { return () => store.listeners.delete(callback); }; - const Counter = () => { + function Counter() { const [count] = useAtom(countAtom); - return ( - <> -
count: {count}
- - ); - }; + return
count: {count}
; + } - const Display = () => { + function Display() { const [display, setDisplay] = useState(true); return ( <> @@ -378,7 +332,7 @@ it('subscription usage test', async () => { ); - }; + } const { getByText, findByText } = render( @@ -437,7 +391,7 @@ it('subscription in base atom test', async () => { }, ); - const Counter = () => { + function Counter() { const [count, add] = useAtom(derivedAtom); return ( <> @@ -445,7 +399,7 @@ it('subscription in base atom test', async () => { ); - }; + } const { getByText, findByText } = render( @@ -491,7 +445,7 @@ it('create atom with onMount in async get', async () => { }, ); - const Counter = () => { + function Counter() { const [count, add] = useAtom(derivedAtom); return ( <> @@ -499,7 +453,7 @@ it('create atom with onMount in async get', async () => { ); - }; + } const { getByText, findByText } = render( diff --git a/__tests__/derive/baseTests/react/optimization.test.tsx b/__tests__/derive/baseTests/react/optimization.test.tsx index 2eefdac..b849802 100644 --- a/__tests__/derive/baseTests/react/optimization.test.tsx +++ b/__tests__/derive/baseTests/react/optimization.test.tsx @@ -11,7 +11,7 @@ it('only relevant render function called (#156)', async () => { let renderCount1 = 0; let renderCount2 = 0; - const Counter1 = () => { + function Counter1() { const [count, setCount] = useAtom(count1Atom); ++renderCount1; return ( @@ -20,9 +20,9 @@ it('only relevant render function called (#156)', async () => { ); - }; + } - const Counter2 = () => { + function Counter2() { const [count, setCount] = useAtom(count2Atom); ++renderCount2; return ( @@ -31,7 +31,7 @@ it('only relevant render function called (#156)', async () => { ); - }; + } const { getByText } = render( <> @@ -74,7 +74,7 @@ it('only render once using atoms with write-only atom', async () => { let renderCount = 0; - const Counter = () => { + function Counter() { const [count1] = useAtom(count1Atom); const [count2] = useAtom(count2Atom); ++renderCount; @@ -83,12 +83,12 @@ it('only render once using atoms with write-only atom', async () => { count1: {count1}, count2: {count2}
); - }; + } - const Control = () => { + function Control() { const [, increment] = useAtom(incrementAtom); return ; - }; + } const { getByText, findByText } = render( <> @@ -116,7 +116,7 @@ it('useless re-renders with static atoms (#355)', async () => { let renderCount = 0; - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); useAtom(unrelatedAtom); ++renderCount; @@ -127,13 +127,9 @@ it('useless re-renders with static atoms (#355)', async () => { ); - }; + } - const { getByText, findByText } = render( - <> - - , - ); + const { getByText, findByText } = render(); await findByText('count: 0'); const renderCountAfterMount = renderCount; @@ -152,7 +148,7 @@ it('does not re-render if value is the same (#1158)', async () => { let renderCount = 0; - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); ++renderCount; return ( @@ -162,13 +158,9 @@ it('does not re-render if value is the same (#1158)', async () => { ); - }; + } - const { getByText, findByText } = render( - <> - - , - ); + const { getByText, findByText } = render(); await findByText('count: 0'); const renderCountAfterMount = renderCount; @@ -198,28 +190,28 @@ it('no extra rerenders after commit with derived atoms (#1213)', async () => { let renderCount1 = 0; let renderCount1AfterCommit = 0; - const Counter1 = () => { + function Counter1() { const [count1] = useAtom(count1Atom); ++renderCount1; useEffect(() => { renderCount1AfterCommit = renderCount1; }); return
count1: {count1}
; - }; + } let renderCount2 = 0; let renderCount2AfterCommit = 0; - const Counter2 = () => { + function Counter2() { const [count2] = useAtom(count2Atom); ++renderCount2; useEffect(() => { renderCount2AfterCommit = renderCount2; }); return
count2: {count2}
; - }; + } - const Control = () => { + function Control() { const [, setValue] = useAtom(baseAtom); const inc1 = () => { setValue((prev) => ({ ...prev, count1: prev.count1 + 1 })); @@ -233,7 +225,7 @@ it('no extra rerenders after commit with derived atoms (#1213)', async () => { ); - }; + } const { getByText } = render( <> diff --git a/__tests__/derive/baseTests/react/provider.test.tsx b/__tests__/derive/baseTests/react/provider.test.tsx index 1903738..90ba0c9 100644 --- a/__tests__/derive/baseTests/react/provider.test.tsx +++ b/__tests__/derive/baseTests/react/provider.test.tsx @@ -8,7 +8,7 @@ it('uses initial values from provider', async () => { const countAtom = atom(1); const petAtom = atom('cat'); - const Display = () => { + function Display() { const [count] = useAtom(countAtom); const [pet] = useAtom(petAtom); @@ -18,7 +18,7 @@ it('uses initial values from provider', async () => {

pet: {pet}

); - }; + } const store = createStore(); store.set(countAtom, 0); @@ -42,7 +42,7 @@ it('only uses initial value from provider for specific atom', async () => { const countAtom = atom(1); const petAtom = atom('cat'); - const Display = () => { + function Display() { const [count] = useAtom(countAtom); const [pet] = useAtom(petAtom); @@ -52,7 +52,7 @@ it('only uses initial value from provider for specific atom', async () => {

pet: {pet}

); - }; + } const store = createStore(); store.set(petAtom, 'dog'); diff --git a/__tests__/derive/baseTests/react/useAtomValue.test.tsx b/__tests__/derive/baseTests/react/useAtomValue.test.tsx index c1f3324..441a806 100644 --- a/__tests__/derive/baseTests/react/useAtomValue.test.tsx +++ b/__tests__/derive/baseTests/react/useAtomValue.test.tsx @@ -7,7 +7,7 @@ import { atom } from '../../../../jotai/vanilla'; it('useAtomValue basic test', async () => { const countAtom = atom(0); - const Counter = () => { + function Counter() { const count = useAtomValue(countAtom); const setCount = useSetAtom(countAtom); @@ -17,7 +17,7 @@ it('useAtomValue basic test', async () => { ); - }; + } const { findByText, getByText } = render( diff --git a/__tests__/derive/baseTests/react/useSetAtom.test.tsx b/__tests__/derive/baseTests/react/useSetAtom.test.tsx index b036aad..e0294b3 100644 --- a/__tests__/derive/baseTests/react/useSetAtom.test.tsx +++ b/__tests__/derive/baseTests/react/useSetAtom.test.tsx @@ -16,7 +16,7 @@ const useCommitCount = () => { it('useSetAtom does not trigger rerender in component', async () => { const countAtom = atom(0); - const Displayer = () => { + function Displayer() { const count = useAtomValue(countAtom); const commits = useCommitCount(); return ( @@ -24,9 +24,9 @@ it('useSetAtom does not trigger rerender in component', async () => { count: {count}, commits: {commits} ); - }; + } - const Updater = () => { + function Updater() { const setCount = useSetAtom(countAtom); const commits = useCommitCount(); return ( @@ -37,22 +37,18 @@ it('useSetAtom does not trigger rerender in component', async () => {
updater commits: {commits}
); - }; + } - const Parent = () => { + function Parent() { return ( <> ); - }; + } - const { getByText } = render( - <> - - , - ); + const { getByText } = render(); await waitFor(() => { getByText('count: 0, commits: 1'); @@ -81,28 +77,28 @@ it('useSetAtom with write without an argument', async () => { set(countAtom, get(countAtom) + 1), ); - const Button = ({ cb, children }: PropsWithChildren<{ cb: () => void }>) => ( - - ); + function Button({ cb, children }: PropsWithChildren<{ cb: () => void }>) { + return ; + } - const Displayer = () => { + function Displayer() { const count = useAtomValue(countAtom); return
count: {count}
; - }; + } - const Updater = () => { + function Updater() { const setCount = useSetAtom(incrementCountAtom); return ; - }; + } - const Parent = () => { + function Parent() { return ( <> ); - }; + } const { getByText } = render( diff --git a/__tests__/derive/baseTests/react/utils/useAtomCallback.test.tsx b/__tests__/derive/baseTests/react/utils/useAtomCallback.test.tsx index aec9449..c7a4317 100644 --- a/__tests__/derive/baseTests/react/utils/useAtomCallback.test.tsx +++ b/__tests__/derive/baseTests/react/utils/useAtomCallback.test.tsx @@ -8,7 +8,7 @@ import { atom } from '../../../../../jotai/vanilla'; it('useAtomCallback with get', async () => { const countAtom = atom(0); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -16,9 +16,9 @@ it('useAtomCallback with get', async () => { ); - }; + } - const Monitor = () => { + function Monitor() { const [count, setCount] = useState(0); const readCount = useAtomCallback( useCallback((get) => { @@ -35,12 +35,8 @@ it('useAtomCallback with get', async () => { clearInterval(timer); }; }, [readCount]); - return ( - <> -
state count: {count}
- - ); - }; + return
state count: {count}
; + } const { findByText, getByText } = render( @@ -60,7 +56,7 @@ it('useAtomCallback with get', async () => { it('useAtomCallback with set and update', async () => { const countAtom = atom(0); const changeableAtom = atom(0); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -68,9 +64,9 @@ it('useAtomCallback with set and update', async () => { ); - }; + } - const Monitor = () => { + function Monitor() { const [changeableCount] = useAtom(changeableAtom); const changeCount = useAtomCallback( useCallback((get, set) => { @@ -87,12 +83,8 @@ it('useAtomCallback with set and update', async () => { clearInterval(timer); }; }, [changeCount]); - return ( - <> -
changeable count: {changeableCount}
- - ); - }; + return
changeable count: {changeableCount}
; + } const { findByText, getByText } = render( @@ -112,7 +104,7 @@ it('useAtomCallback with set and update', async () => { it('useAtomCallback with set and update and arg', async () => { const countAtom = atom(0); - const App = () => { + function App() { const [count] = useAtom(countAtom); const setCount = useAtomCallback( useCallback((_get, set, arg: number) => { @@ -127,7 +119,7 @@ it('useAtomCallback with set and update and arg', async () => { ); - }; + } const { findByText, getByText } = render( @@ -145,7 +137,7 @@ it('useAtomCallback with set and update and arg', async () => { it('useAtomCallback with sync atom (#1100)', async () => { const countAtom = atom(0); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); const readCount = useAtomCallback(useCallback((get) => get(countAtom), [])); useEffect(() => { @@ -160,7 +152,7 @@ it('useAtomCallback with sync atom (#1100)', async () => { ); - }; + } const { findByText, getByText } = render( diff --git a/__tests__/derive/baseTests/react/utils/useHydrateAtoms.test.tsx b/__tests__/derive/baseTests/react/utils/useHydrateAtoms.test.tsx index 43fd584..6efb766 100644 --- a/__tests__/derive/baseTests/react/utils/useHydrateAtoms.test.tsx +++ b/__tests__/derive/baseTests/react/utils/useHydrateAtoms.test.tsx @@ -9,13 +9,13 @@ it('useHydrateAtoms should only hydrate on first render', async () => { const countAtom = atom(0); const statusAtom = atom('fulfilled'); - const Counter = ({ + function Counter({ initialCount, initialStatus, }: { initialCount: number; initialStatus: string; - }) => { + }) { useHydrateAtoms([ [countAtom, initialCount], [statusAtom, initialStatus], @@ -39,7 +39,7 @@ it('useHydrateAtoms should only hydrate on first render', async () => { ); - }; + } const { findByText, getByText, rerender } = render( @@ -66,13 +66,13 @@ it('useHydrateAtoms should only hydrate on first render using a Map', async () = const countAtom = atom(0); const activeAtom = atom(true); - const Counter = ({ + function Counter({ initialActive = false, initialCount, }: { initialActive?: boolean; initialCount: number; - }) => { + }) { useHydrateAtoms( new Map< typeof activeAtom | typeof countAtom, @@ -92,7 +92,7 @@ it('useHydrateAtoms should only hydrate on first render using a Map', async () = ); - }; + } const { findByText, getByText, rerender } = render( @@ -106,7 +106,7 @@ it('useHydrateAtoms should only hydrate on first render using a Map', async () = rerender( - + , ); await findByText('count: 43'); @@ -116,7 +116,7 @@ it('useHydrateAtoms should only hydrate on first render using a Map', async () = it('useHydrateAtoms should not trigger unnecessary re-renders', async () => { const countAtom = atom(0); - const Counter = ({ initialCount }: { initialCount: number }) => { + function Counter({ initialCount }: { initialCount: number }) { useHydrateAtoms([[countAtom, initialCount]]); const [countValue, setCount] = useAtom(countAtom); const commitCount = useRef(1); @@ -130,13 +130,9 @@ it('useHydrateAtoms should not trigger unnecessary re-renders', async () => { ); - }; + } - const { findByText, getByText } = render( - <> - - , - ); + const { findByText, getByText } = render(); await findByText('count: 42'); await findByText('commits: 1'); @@ -149,7 +145,7 @@ it('useHydrateAtoms should work with derived atoms', async () => { const countAtom = atom(0); const doubleAtom = atom((get) => get(countAtom) * 2); - const Counter = ({ initialCount }: { initialCount: number }) => { + function Counter({ initialCount }: { initialCount: number }) { useHydrateAtoms([[countAtom, initialCount]]); const [countValue, setCount] = useAtom(countAtom); const [doubleCount] = useAtom(doubleAtom); @@ -160,7 +156,7 @@ it('useHydrateAtoms should work with derived atoms', async () => { ); - }; + } const { findByText, getByText } = render( @@ -178,7 +174,7 @@ it('useHydrateAtoms should work with derived atoms', async () => { it('useHydrateAtoms can only restore an atom once', async () => { const countAtom = atom(0); - const Counter = ({ initialCount }: { initialCount: number }) => { + function Counter({ initialCount }: { initialCount: number }) { useHydrateAtoms([[countAtom, initialCount]]); const [countValue, setCount] = useAtom(countAtom); @@ -188,8 +184,8 @@ it('useHydrateAtoms can only restore an atom once', async () => { ); - }; - const Counter2 = ({ count }: { count: number }) => { + } + function Counter2({ count }: { count: number }) { useHydrateAtoms([[countAtom, count]]); const [countValue, setCount] = useAtom(countAtom); @@ -199,7 +195,7 @@ it('useHydrateAtoms can only restore an atom once', async () => { ); - }; + } const { findByText, getByText, rerender } = render( @@ -226,17 +222,13 @@ it('useHydrateAtoms should respect onMount', async () => { const onMountFn = jest.fn(() => {}); countAtom.onMount = onMountFn; - const Counter = ({ initialCount }: { initialCount: number }) => { + function Counter({ initialCount }: { initialCount: number }) { useHydrateAtoms([[countAtom, initialCount]]); const [countValue] = useAtom(countAtom); return
count: {countValue}
; - }; - const { findByText } = render( - <> - - , - ); + } + const { findByText } = render(); await findByText('count: 42'); expect(onMountFn).toHaveBeenCalledTimes(1); @@ -246,7 +238,7 @@ it('passing dangerouslyForceHydrate to useHydrateAtoms will re-hydrated atoms', const countAtom = atom(0); const statusAtom = atom('fulfilled'); - const Counter = ({ + function Counter({ initialCount, initialStatus, dangerouslyForceHydrate = false, @@ -254,7 +246,7 @@ it('passing dangerouslyForceHydrate to useHydrateAtoms will re-hydrated atoms', initialCount: number; initialStatus: string; dangerouslyForceHydrate?: boolean; - }) => { + }) { useHydrateAtoms( [ [countAtom, initialCount], @@ -283,7 +275,7 @@ it('passing dangerouslyForceHydrate to useHydrateAtoms will re-hydrated atoms', ); - }; + } const { findByText, getByText, rerender } = render( @@ -310,7 +302,7 @@ it('passing dangerouslyForceHydrate to useHydrateAtoms will re-hydrated atoms', , ); diff --git a/__tests__/derive/baseTests/react/utils/useReducerAtom.test.tsx b/__tests__/derive/baseTests/react/utils/useReducerAtom.test.tsx index 886c722..c8593f5 100644 --- a/__tests__/derive/baseTests/react/utils/useReducerAtom.test.tsx +++ b/__tests__/derive/baseTests/react/utils/useReducerAtom.test.tsx @@ -6,10 +6,13 @@ import { atom } from '../../../../../jotai/vanilla'; let savedConsoleWarn: any; beforeEach(() => { + // eslint-disable-next-line no-console savedConsoleWarn = console.warn; + // eslint-disable-next-line no-console console.warn = jest.fn(); }); afterEach(() => { + // eslint-disable-next-line no-console console.warn = savedConsoleWarn; }); @@ -17,7 +20,7 @@ it('useReducerAtom with no action argument', async () => { const countAtom = atom(0); const reducer = (state: number) => state + 2; - const Parent = () => { + function Parent() { const [count, dispatch] = useReducerAtom(countAtom, reducer); return ( <> @@ -25,7 +28,7 @@ it('useReducerAtom with no action argument', async () => { ); - }; + } const { findByText, getByText } = render( @@ -51,11 +54,12 @@ it('useReducerAtom with optional action argument', async () => { case 'DECREASE': return state - 1; case undefined: + default: return state; } }; - const Parent = () => { + function Parent() { const [count, dispatch] = useReducerAtom(countAtom, reducer); return ( <> @@ -65,7 +69,7 @@ it('useReducerAtom with optional action argument', async () => { ); - }; + } const { findByText, getByText } = render( @@ -92,11 +96,12 @@ it('useReducerAtom with non-optional action argument', async () => { case 'INCREASE': return state + 1; case 'DECREASE': + default: return state - 1; } }; - const Parent = () => { + function Parent() { const [count, dispatch] = useReducerAtom(countAtom, reducer); return ( <> @@ -105,7 +110,7 @@ it('useReducerAtom with non-optional action argument', async () => { ); - }; + } const { findByText, getByText } = render( diff --git a/__tests__/derive/baseTests/react/utils/useResetAtom.test.tsx b/__tests__/derive/baseTests/react/utils/useResetAtom.test.tsx index e58315d..7f8c679 100644 --- a/__tests__/derive/baseTests/react/utils/useResetAtom.test.tsx +++ b/__tests__/derive/baseTests/react/utils/useResetAtom.test.tsx @@ -13,7 +13,7 @@ import { it('atomWithReset resets to its first value', async () => { const countAtom = atomWithReset(0); - const Parent = () => { + function Parent() { const [count, setValue] = useAtom(countAtom); const resetAtom = useResetAtom(countAtom); return ( @@ -26,7 +26,7 @@ it('atomWithReset resets to its first value', async () => { ); - }; + } const { findByText, getByText } = render( @@ -60,7 +60,7 @@ it('atomWithReset resets to its first value', async () => { it('atomWithReset reset based on previous value', async () => { const countAtom = atomWithReset(0); - const Parent = () => { + function Parent() { const [count, setValue] = useAtom(countAtom); return ( <> @@ -76,7 +76,7 @@ it('atomWithReset reset based on previous value', async () => { ); - }; + } const { findByText, getByText } = render( @@ -105,7 +105,7 @@ it('atomWithReset through read-write atom', async () => { set(primitiveAtom, newValue as never), ); - const Parent = () => { + function Parent() { const [count, setValue] = useAtom(countAtom); const resetAtom = useResetAtom(countAtom); return ( @@ -115,7 +115,7 @@ it('atomWithReset through read-write atom', async () => { ); - }; + } const { findByText, getByText } = render( @@ -146,7 +146,7 @@ it('useResetAtom with custom atom', async () => { const countAtom = atomWithReducer(0, reducer); - const Parent = () => { + function Parent() { const [count, dispatch] = useAtom(countAtom); const resetAtom = useResetAtom(countAtom); return ( @@ -156,7 +156,7 @@ it('useResetAtom with custom atom', async () => { ); - }; + } const { findByText, getByText } = render( diff --git a/__tests__/derive/baseTests/react/vanilla-utils/atomFamily.test.tsx b/__tests__/derive/baseTests/react/vanilla-utils/atomFamily.test.tsx index d5d202b..08dd969 100644 --- a/__tests__/derive/baseTests/react/vanilla-utils/atomFamily.test.tsx +++ b/__tests__/derive/baseTests/react/vanilla-utils/atomFamily.test.tsx @@ -12,13 +12,13 @@ import { atomFamily } from '../../../../../jotai/vanilla/utils'; it('new atomFamily impl', async () => { const myFamily = atomFamily((param: string) => atom(param)); - const Displayer = ({ index }: { index: string }) => { + function Displayer({ index }: { index: string }) { const [count] = useAtom(myFamily(index)); return
count: {count}
; - }; + } const { findByText } = render( - + , ); @@ -66,7 +66,7 @@ it('removed atom creates a new reference', async () => { it('primitive atomFamily initialized with props', async () => { const myFamily = atomFamily((param: number) => atom(param)); - const Displayer = ({ index }: { index: number }) => { + function Displayer({ index }: { index: number }) { const [count, setCount] = useAtom(myFamily(index)); return (
@@ -74,9 +74,9 @@ it('primitive atomFamily initialized with props', async () => {
); - }; + } - const Parent = () => { + function Parent() { const [index, setIndex] = useState(1); return ( @@ -85,7 +85,7 @@ it('primitive atomFamily initialized with props', async () => { ); - }; + } const { findByText, getByText } = render( @@ -132,13 +132,13 @@ it('derived atomFamily functionality as usual', async () => { ), ); - const Displayer = ({ + function Displayer({ index, countAtom, }: { index: number; countAtom: WritableAtom], void>; - }) => { + }) { const [count, setCount] = useAtom(countAtom); return (
@@ -148,11 +148,11 @@ it('derived atomFamily functionality as usual', async () => {
); - }; + } const indicesAtom = atom((get) => [...new Array(get(arrayAtom).length)]); - const Parent = () => { + function Parent() { const [indices] = useAtom(indicesAtom); return ( @@ -162,7 +162,7 @@ it('derived atomFamily functionality as usual', async () => { ))} ); - }; + } const { getByText } = render( @@ -229,7 +229,7 @@ it('a derived atom from an async atomFamily (#351)', async () => { ); const derivedAtom = atom((get) => get(getAsyncAtom(get(countAtom)))); - const Counter = () => { + function Counter() { const setCount = useSetAtom(countAtom); const [derived] = useAtom(derivedAtom); return ( @@ -238,7 +238,7 @@ it('a derived atom from an async atomFamily (#351)', async () => { ); - }; + } const { getByText, findByText } = render( diff --git a/__tests__/derive/baseTests/react/vanilla-utils/atomWithDefault.test.tsx b/__tests__/derive/baseTests/react/vanilla-utils/atomWithDefault.test.tsx index 13fe1bc..2df24d3 100644 --- a/__tests__/derive/baseTests/react/vanilla-utils/atomWithDefault.test.tsx +++ b/__tests__/derive/baseTests/react/vanilla-utils/atomWithDefault.test.tsx @@ -9,7 +9,7 @@ it('simple sync get default', async () => { const count1Atom = atom(1); const count2Atom = atomWithDefault((get) => get(count1Atom) * 2); - const Counter = () => { + function Counter() { const [count1, setCount1] = useAtom(count1Atom); const [count2, setCount2] = useAtom(count2Atom); return ( @@ -21,7 +21,7 @@ it('simple sync get default', async () => { ); - }; + } const { findByText, getByText } = render( @@ -49,7 +49,7 @@ it('simple async get default', async () => { return get(count1Atom) * 2; }); - const Counter = () => { + function Counter() { const [count1, setCount1] = useAtom(count1Atom); const [count2, setCount2] = useAtom(count2Atom); return ( @@ -63,7 +63,7 @@ it('simple async get default', async () => { ); - }; + } const { findByText, getByText } = render( @@ -95,7 +95,7 @@ it('refresh sync atoms to default values', async () => { const count1Atom = atom(1); const count2Atom = atomWithDefault((get) => get(count1Atom) * 2); - const Counter = () => { + function Counter() { const [count1, setCount1] = useAtom(count1Atom); const [count2, setCount2] = useAtom(count2Atom); return ( @@ -108,7 +108,7 @@ it('refresh sync atoms to default values', async () => { ); - }; + } const { findByText, getByText } = render( @@ -142,7 +142,7 @@ it('refresh async atoms to default values', async () => { return get(count1Atom) * 2; }); - const Counter = () => { + function Counter() { const [count1, setCount1] = useAtom(count1Atom); const [count2, setCount2] = useAtom(count2Atom); return ( @@ -157,7 +157,7 @@ it('refresh async atoms to default values', async () => { ); - }; + } const { findByText, getByText } = render( diff --git a/__tests__/derive/baseTests/react/vanilla-utils/atomWithReducer.test.tsx b/__tests__/derive/baseTests/react/vanilla-utils/atomWithReducer.test.tsx index b40c49b..6b0dd65 100644 --- a/__tests__/derive/baseTests/react/vanilla-utils/atomWithReducer.test.tsx +++ b/__tests__/derive/baseTests/react/vanilla-utils/atomWithReducer.test.tsx @@ -12,12 +12,13 @@ it('atomWithReducer with optional action argument', async () => { case 'DECREASE': return state - 1; case undefined: + default: return state; } }; const countAtom = atomWithReducer(0, reducer); - const Parent = () => { + function Parent() { const [count, dispatch] = useAtom(countAtom); return ( <> @@ -27,7 +28,7 @@ it('atomWithReducer with optional action argument', async () => { ); - }; + } const { findByText, getByText } = render( @@ -53,12 +54,13 @@ it('atomWithReducer with non-optional action argument', async () => { case 'INCREASE': return state + 1; case 'DECREASE': + default: return state - 1; } }; const countAtom = atomWithReducer(0, reducer); - const Parent = () => { + function Parent() { const [count, dispatch] = useAtom(countAtom); return ( <> @@ -67,7 +69,7 @@ it('atomWithReducer with non-optional action argument', async () => { ); - }; + } const { findByText, getByText } = render( diff --git a/__tests__/derive/baseTests/react/vanilla-utils/atomWithRefresh.test.tsx b/__tests__/derive/baseTests/react/vanilla-utils/atomWithRefresh.test.tsx index 7e73d49..edee15f 100644 --- a/__tests__/derive/baseTests/react/vanilla-utils/atomWithRefresh.test.tsx +++ b/__tests__/derive/baseTests/react/vanilla-utils/atomWithRefresh.test.tsx @@ -8,7 +8,7 @@ it('sync counter', async () => { let counter = 0; const countAtom = atomWithRefresh(() => ++counter); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -16,7 +16,7 @@ it('sync counter', async () => { ); - }; + } const { findByText, getByText } = render( @@ -43,7 +43,7 @@ it('async counter', async () => { return ++counter; }); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -51,7 +51,7 @@ it('async counter', async () => { ); - }; + } const { findByText, getByText } = render( @@ -86,7 +86,7 @@ it('writable counter', async () => { }, ); - const Counter = () => { + function Counter() { const [count, setCount] = useAtom(countAtom); return ( <> @@ -95,7 +95,7 @@ it('writable counter', async () => { ); - }; + } const { findByText, getByText } = render( diff --git a/__tests__/derive/baseTests/react/vanilla-utils/freezeAtom.test.tsx b/__tests__/derive/baseTests/react/vanilla-utils/freezeAtom.test.tsx index 5a2ed1a..96de5c7 100644 --- a/__tests__/derive/baseTests/react/vanilla-utils/freezeAtom.test.tsx +++ b/__tests__/derive/baseTests/react/vanilla-utils/freezeAtom.test.tsx @@ -13,7 +13,7 @@ it('freezeAtom basic test', async () => { set(objAtom, { deep: {} }); }); - const Component = () => { + function Component() { const [obj, setObj] = useAtom(freezeAtom(objAtom)); return ( <> @@ -23,7 +23,7 @@ it('freezeAtom basic test', async () => { ); - }; + } const { getByText, findByText } = render( @@ -40,10 +40,13 @@ it('freezeAtom basic test', async () => { describe('freezeAtomCreator', () => { let savedConsoleWarn: any; beforeEach(() => { + // eslint-disable-next-line no-console savedConsoleWarn = console.warn; + // eslint-disable-next-line no-console console.warn = jest.fn(); }); afterEach(() => { + // eslint-disable-next-line no-console console.warn = savedConsoleWarn; }); @@ -53,7 +56,7 @@ describe('freezeAtomCreator', () => { set(objAtom, { deep: {} }); }); - const Component = () => { + function Component() { const [obj, setObj] = useAtom(objAtom); return ( <> @@ -63,7 +66,7 @@ describe('freezeAtomCreator', () => { ); - }; + } const { getByText, findByText } = render( diff --git a/__tests__/derive/baseTests/react/vanilla-utils/loadable.test.tsx b/__tests__/derive/baseTests/react/vanilla-utils/loadable.test.tsx index 1901d56..cc25a46 100644 --- a/__tests__/derive/baseTests/react/vanilla-utils/loadable.test.tsx +++ b/__tests__/derive/baseTests/react/vanilla-utils/loadable.test.tsx @@ -65,16 +65,12 @@ it('loadable goes back to loading after re-fetch', async () => { return new Promise((r) => (resolve = r)); }); - const Refresh = () => { + function Refresh() { const setRefresh = useSetAtom(refreshAtom); return ( - <> - - + ); - }; + } const { findByText, getByText } = render( @@ -104,16 +100,12 @@ it('loadable can recover from error', async () => { }); }); - const Refresh = () => { + function Refresh() { const setRefresh = useSetAtom(refreshAtom); return ( - <> - - + ); - }; + } const { findByText, getByText } = render( @@ -155,11 +147,11 @@ it('loadable can use resolved promises synchronously', async () => { const asyncAtom = atom(Promise.resolve(5)); const effectCallback = jest.fn(); - const ResolveAtomComponent = () => { + function ResolveAtomComponent() { useAtomValue(asyncAtom); return
Ready
; - }; + } const { findByText, rerender } = render( @@ -198,14 +190,10 @@ it('loadable of a derived async atom does not trigger infinite loop (#1114)', as return new Promise((r) => (resolve = r)); }); - const Trigger = () => { + function Trigger() { const trigger = useSetAtom(baseAtom); - return ( - <> - - - ); - }; + return ; + } const { findByText, getByText } = render( @@ -280,10 +268,10 @@ type LoadableComponentProps = { effectCallback?: (loadableValue: any) => void; }; -const LoadableComponent = ({ +function LoadableComponent({ asyncAtom, effectCallback, -}: LoadableComponentProps) => { +}: LoadableComponentProps) { const value = useAtomValue(loadable(asyncAtom)); useEffect(() => { @@ -301,7 +289,7 @@ const LoadableComponent = ({ } // this is to ensure correct typing - const data: number | string = value.data; + const { data } = value; return <>Data: {data}; -}; +} diff --git a/__tests__/derive/baseTests/react/vanilla-utils/selectAtom.test.tsx b/__tests__/derive/baseTests/react/vanilla-utils/selectAtom.test.tsx index 95eeb42..c832dba 100644 --- a/__tests__/derive/baseTests/react/vanilla-utils/selectAtom.test.tsx +++ b/__tests__/derive/baseTests/react/vanilla-utils/selectAtom.test.tsx @@ -17,29 +17,23 @@ it('selectAtom works as expected', async () => { const bigAtom = atom({ a: 0, b: 'othervalue' }); const littleAtom = selectAtom(bigAtom, (v) => v.a); - const Parent = () => { + function Parent() { const setValue = useSetAtom(bigAtom); return ( - <> - - + ); - }; + } - const Selector = () => { + function Selector() { const a = useAtomValue(littleAtom); - return ( - <> -
a: {a}
- - ); - }; + return
a: {a}
; + } const { findByText, getByText } = render( @@ -66,7 +60,7 @@ it('do not update unless equality function says value has changed', async () => (left, right) => JSON.stringify(left) === JSON.stringify(right), ); - const Parent = () => { + function Parent() { const setValue = useSetAtom(bigAtom); return ( <> @@ -82,9 +76,9 @@ it('do not update unless equality function says value has changed', async () => ); - }; + } - const Selector = () => { + function Selector() { const value = useAtomValue(littleAtom); const commits = useCommitCount(); return ( @@ -93,7 +87,7 @@ it('do not update unless equality function says value has changed', async () =>
commits: {commits}
); - }; + } const { findByText, getByText } = render( <> diff --git a/__tests__/derive/baseTests/react/vanilla-utils/splitAtom.test.tsx b/__tests__/derive/baseTests/react/vanilla-utils/splitAtom.test.tsx index 5d3add1..ff0ee55 100644 --- a/__tests__/derive/baseTests/react/vanilla-utils/splitAtom.test.tsx +++ b/__tests__/derive/baseTests/react/vanilla-utils/splitAtom.test.tsx @@ -22,28 +22,19 @@ it('no unnecessary updates when updating atoms', async () => { { task: 'get dragon food', checked: false }, ]); - const TaskList = ({ listAtom }: { listAtom: typeof todosAtom }) => { - const [atoms, dispatch] = useAtom(splitAtom(listAtom)); + function TaskList({ listAtom }: { listAtom: typeof todosAtom }) { + const [atoms] = useAtom(splitAtom(listAtom)); return ( <> TaskListUpdates: {useCommitCount()} {atoms.map((anAtom) => ( - dispatch({ type: 'remove', atom: anAtom })} - itemAtom={anAtom} - /> + ))} ); - }; + } - const TaskItem = ({ - itemAtom, - }: { - itemAtom: PrimitiveAtom; - onRemove: () => void; - }) => { + function TaskItem({ itemAtom }: { itemAtom: PrimitiveAtom }) { const [value, onChange] = useAtom(itemAtom); const toggle = () => onChange((value) => ({ ...value, checked: !value.checked })); @@ -58,13 +49,9 @@ it('no unnecessary updates when updating atoms', async () => { /> ); - }; + } - const { getByTestId, getByText } = render( - <> - - , - ); + const { getByTestId, getByText } = render(); await waitFor(() => { getByText('TaskListUpdates: 1'); @@ -108,7 +95,7 @@ it('removing atoms', async () => { { task: 'help nana', checked: false }, ]); - const TaskList = ({ listAtom }: { listAtom: typeof todosAtom }) => { + function TaskList({ listAtom }: { listAtom: typeof todosAtom }) { const [atoms, dispatch] = useAtom(splitAtom(listAtom)); return ( <> @@ -121,15 +108,15 @@ it('removing atoms', async () => { ))} ); - }; + } - const TaskItem = ({ + function TaskItem({ itemAtom, onRemove, }: { itemAtom: PrimitiveAtom; onRemove: () => void; - }) => { + }) { const [value] = useAtom(itemAtom); return (
  • @@ -139,7 +126,7 @@ it('removing atoms', async () => {
  • ); - }; + } const { getByTestId, queryByText } = render( @@ -185,7 +172,7 @@ it('inserting atoms', async () => { { task: 'help nana' }, ]); - const TaskList = ({ listAtom }: { listAtom: typeof todosAtom }) => { + function TaskList({ listAtom }: { listAtom: typeof todosAtom }) { const [atoms, dispatch] = useAtom(splitAtom(listAtom)); return ( <> @@ -217,29 +204,29 @@ it('inserting atoms', async () => { ); - }; + } let taskCount = 1; - const TaskItem = ({ + function TaskItem({ itemAtom, onInsert, }: { itemAtom: PrimitiveAtom; onInsert: (newValue: TodoItem) => void; - }) => { + }) { const [value] = useAtom(itemAtom); return (
  • {value.task}
  • ); - }; + } const { getByTestId, queryByTestId } = render( @@ -282,7 +269,7 @@ it('moving atoms', async () => { { task: 'help nana' }, ]); - const TaskList = ({ listAtom }: { listAtom: typeof todosAtom }) => { + function TaskList({ listAtom }: { listAtom: typeof todosAtom }) { const [atoms, dispatch] = useAtom(splitAtom(listAtom)); return (
      @@ -322,9 +309,9 @@ it('moving atoms', async () => { ))}
    ); - }; + } - const TaskItem = ({ + function TaskItem({ itemAtom, onMoveLeft, onMoveRight, @@ -332,7 +319,7 @@ it('moving atoms', async () => { itemAtom: PrimitiveAtom; onMoveLeft: () => void; onMoveRight: () => void; - }) => { + }) { const [value] = useAtom(itemAtom); return (
  • @@ -345,7 +332,7 @@ it('moving atoms', async () => {
  • ); - }; + } const { getByTestId, queryByTestId } = render( @@ -394,7 +381,7 @@ it('read-only array atom', async () => { { task: 'get dragon food', checked: false }, ]); - const TaskList = ({ listAtom }: { listAtom: typeof todosAtom }) => { + function TaskList({ listAtom }: { listAtom: typeof todosAtom }) { const [atoms] = useAtom(splitAtom(listAtom)); return ( <> @@ -403,9 +390,9 @@ it('read-only array atom', async () => { ))} ); - }; + } - const TaskItem = ({ itemAtom }: { itemAtom: Atom }) => { + function TaskItem({ itemAtom }: { itemAtom: Atom }) { const [value] = useAtom(itemAtom); return (
  • @@ -417,7 +404,7 @@ it('read-only array atom', async () => { />
  • ); - }; + } const { getByTestId } = render( @@ -454,20 +441,20 @@ it('no error with cached atoms (fix 510)', async () => { type NumItemProps = { atom: Atom }; - const NumItem = ({ atom }: NumItemProps) => { + function NumItem({ atom }: NumItemProps) { const [readOnlyItem] = useAtom(atom); if (typeof readOnlyItem !== 'number') { throw new Error('expecting a number'); } return <>{readOnlyItem}; - }; + } function Filter() { const [, setFilter] = useAtom(filterAtom); return ; } - const Filtered = () => { + function Filtered() { const [todos] = useAtom(filteredAtomsAtom); const cachedAtoms = useCachedAtoms(todos); @@ -478,7 +465,7 @@ it('no error with cached atoms (fix 510)', async () => { ))} ); - }; + } const { getByText } = render( diff --git a/__tests__/derive/baseTests/vanilla/dependency.test.tsx b/__tests__/derive/baseTests/vanilla/dependency.test.tsx index c6066ce..5d8a29a 100644 --- a/__tests__/derive/baseTests/vanilla/dependency.test.tsx +++ b/__tests__/derive/baseTests/vanilla/dependency.test.tsx @@ -162,18 +162,16 @@ it('should not provide stale values to conditional dependents', () => { const hasFilter = get(hasFilterAtom); if (hasFilter) { return []; - } else { - return data; } + return data; }); const stageAtom = atom((get) => { const hasFilter = get(hasFilterAtom); if (hasFilter) { const filtered = get(filteredAtom); return filtered.length === 0 ? 'is-empty' : 'has-data'; - } else { - return 'no-filter'; } + return 'no-filter'; }); const store = createStore(); diff --git a/__tests__/derive/baseTests/vanilla/store.test.tsx b/__tests__/derive/baseTests/vanilla/store.test.tsx index d7f9b2f..cad8643 100644 --- a/__tests__/derive/baseTests/vanilla/store.test.tsx +++ b/__tests__/derive/baseTests/vanilla/store.test.tsx @@ -1,8 +1,8 @@ import { waitFor } from '@testing-library/dom'; +import assert from 'minimalistic-assert'; import { atom } from '../../../../jotai/vanilla'; import type { Getter } from '../../../../jotai/vanilla'; import { createStore } from '../../derivedStore'; -import assert from 'minimalistic-assert'; it('should not fire on subscribe', async () => { const store = createStore(); diff --git a/__tests__/derive/baseTests/vanilla/unstable_derive.test.tsx b/__tests__/derive/baseTests/vanilla/unstable_derive.test.tsx index 02414ee..db4c1a7 100644 --- a/__tests__/derive/baseTests/vanilla/unstable_derive.test.tsx +++ b/__tests__/derive/baseTests/vanilla/unstable_derive.test.tsx @@ -9,7 +9,7 @@ describe('unstable_derive for scoping atoms', () => { */ it('primitive atom', async () => { const a = atom('a'); - a.onMount = (setSelf) => setSelf((v) => v + ':mounted'); + a.onMount = (setSelf) => setSelf((v) => `${v}:mounted`); const scopedAtoms = new Set>([a]); const store = createStore(); @@ -38,7 +38,7 @@ describe('unstable_derive for scoping atoms', () => { expect(store.get(a)).toBe('a'); expect(derivedStore.get(a)).toBe('a:mounted'); - derivedStore.set(a, (v) => v + ':updated'); + derivedStore.set(a, (v) => `${v}:updated`); await new Promise((resolve) => setTimeout(resolve)); expect(store.get(a)).toBe('a'); expect(derivedStore.get(a)).toBe('a:mounted:updated');