Skip to content

Commit bc39611

Browse files
a10nikdai-shi
andauthored
fix(atomWithRefresh): non-writable refreshables throw an error if implicitly passed an argument (#2996)
* docs(atomWithRefresh): doesn't refresh with arguments passed * fix(#2995): error instead of noop when fetch-only refreshable atom's setter is called with args * Update tests/vanilla/utils/atomWithRefresh.test.ts * Update test-multiple-builds.yml * Update test-multiple-builds.yml * Update test-multiple-builds.yml --------- Co-authored-by: Daishi Kato <[email protected]>
1 parent df53eb2 commit bc39611

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

.github/workflows/test-multiple-builds.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ jobs:
3030
- name: Patch for DEV-ONLY
3131
if: ${{ matrix.env == 'development' }}
3232
run: |
33-
sed -i~ "s/\(it\|describe\)[.a-zA-Z]*('\[DEV-ONLY\]/\1('/" tests/*/*.tsx tests/*/*/*.tsx
34-
sed -i~ "s/\(it\|describe\)[.a-zA-Z]*('\[PRD-ONLY\]/\1.skip('/" tests/*/*.tsx tests/*/*/*.tsx
33+
sed -i~ "s/\(it\|describe\)[.a-zA-Z]*('\[DEV-ONLY\]/\1('/" tests/*/*.ts* tests/*/*/*.ts*
34+
sed -i~ "s/\(it\|describe\)[.a-zA-Z]*('\[PRD-ONLY\]/\1.skip('/" tests/*/*.ts* tests/*/*/*.ts*
3535
- name: Patch for PRD-ONLY
3636
if: ${{ matrix.env == 'production' }}
3737
run: |
38-
sed -i~ "s/\(it\|describe\)[.a-zA-Z]*('\PRD-ONLY\]/\1('/" tests/*/*.tsx tests/*/*/*.tsx
39-
sed -i~ "s/\(it\|describe\)[.a-zA-Z]*('\[DEV-ONLY\]/\1.skip('/" tests/*/*.tsx tests/*/*/*.tsx
38+
sed -i~ "s/\(it\|describe\)[.a-zA-Z]*('\PRD-ONLY\]/\1('/" tests/*/*.ts* tests/*/*/*.ts*
39+
sed -i~ "s/\(it\|describe\)[.a-zA-Z]*('\[DEV-ONLY\]/\1.skip('/" tests/*/*.ts* tests/*/*/*.ts*
4040
- name: Patch for CJS
4141
if: ${{ matrix.build == 'cjs' }}
4242
run: |

docs/utilities/resettable.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ const PostsList = () => {
198198
</ul>
199199
200200
{/* Clicking this button will re-fetch the posts */}
201-
<button type="button" onClick={refreshPosts}>
201+
<button type="button" onClick={() => refreshPosts()}>
202202
Refresh posts
203203
</button>
204204
</div>

src/vanilla/utils/atomWithRefresh.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export function atomWithRefresh<Value, Args extends unknown[], Result>(
3939
set(refreshAtom, (c) => c + 1)
4040
} else if (write) {
4141
return write(get, set, ...args)
42+
} else if (import.meta.env?.MODE !== 'production') {
43+
throw new Error('refresh must be called without arguments')
4244
}
4345
},
4446
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { createStore } from 'jotai/vanilla'
3+
import { atomWithRefresh } from 'jotai/vanilla/utils'
4+
5+
describe('atomWithRefresh', () => {
6+
it('[DEV-ONLY] throws when refresh is called with extra arguments', () => {
7+
const atom = atomWithRefresh(() => {})
8+
const store = createStore()
9+
const args = ['some arg'] as unknown as []
10+
expect(() => store.set(atom, ...args)).throws()
11+
})
12+
})

0 commit comments

Comments
 (0)