Skip to content

Commit 829254b

Browse files
committed
feat(useQuery): onSuccess callback
options spread is wrong - `updatedAt` is actually `dataUpdatedAt`. Oddly we didn't have a test, so I added one
1 parent ec2c138 commit 829254b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/core/query.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ export class Query<
222222
this.dispatch({
223223
data,
224224
type: 'success',
225-
...options,
225+
dataUpdatedAt: options?.updatedAt,
226+
notifySuccess: options?.notifySuccess,
226227
})
227228

228229
return data

src/core/tests/queryClient.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { waitFor } from '@testing-library/react'
2+
import '@testing-library/jest-dom'
23
import React from 'react'
34

45
import {
@@ -254,6 +255,36 @@ describe('queryClient', () => {
254255
expect(onSuccess).toHaveBeenCalledTimes(1)
255256
expect(onSuccess).toHaveBeenCalledWith('data')
256257
})
258+
259+
test('should respect updatedAt', async () => {
260+
const key = queryKey()
261+
262+
function Page() {
263+
const state = useQuery(key, () => 'data')
264+
return (
265+
<div>
266+
<div>data: {state.data}</div>
267+
<div>dataUpdatedAt: {state.dataUpdatedAt}</div>
268+
<button
269+
onClick={() =>
270+
queryClient.setQueryData(key, 'newData', { updatedAt: 100 })
271+
}
272+
>
273+
setQueryData
274+
</button>
275+
</div>
276+
)
277+
}
278+
279+
const rendered = renderWithClient(queryClient, <Page />)
280+
281+
await waitFor(() => rendered.getByText('data: data'))
282+
rendered.getByRole('button', { name: /setQueryData/i }).click()
283+
await waitFor(() => rendered.getByText('data: newData'))
284+
await waitFor(() => {
285+
expect(rendered.getByText('dataUpdatedAt: 100')).toBeInTheDocument()
286+
})
287+
})
257288
})
258289

259290
describe('setQueriesData', () => {

0 commit comments

Comments
 (0)