Skip to content

Commit e2e5401

Browse files
authored
test(query-core/queryObserver): add test for 'getOptimisticResult' updating 'currentResult' with changed data (#10102)
1 parent 14a3ce5 commit e2e5401

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

packages/query-core/src/__tests__/queryObserver.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,32 @@ describe('queryObserver', () => {
15101510
expect(result.isEnabled).toBe(true)
15111511
})
15121512

1513+
test('should update currentResult when getOptimisticResult is called with changed data', () => {
1514+
const key = queryKey()
1515+
1516+
const observer = new QueryObserver(queryClient, {
1517+
queryKey: key,
1518+
queryFn: () => 'data',
1519+
})
1520+
1521+
const defaultedOptions = queryClient.defaultQueryOptions({
1522+
queryKey: key,
1523+
queryFn: () => 'data',
1524+
})
1525+
1526+
// First render: no data yet
1527+
const initialResult = observer.getOptimisticResult(defaultedOptions)
1528+
expect(initialResult.data).toBeUndefined()
1529+
1530+
// Another component sets data (e.g., dependent query resolved)
1531+
queryClient.setQueryData(key, 'updated')
1532+
1533+
// Re-render: getOptimisticResult should pick up the new data and update currentResult
1534+
const updatedResult = observer.getOptimisticResult(defaultedOptions)
1535+
expect(updatedResult.data).toBe('updated')
1536+
expect(observer.getCurrentResult().data).toBe('updated')
1537+
})
1538+
15131539
describe('StrictMode behavior', () => {
15141540
it('should deduplicate calls to queryFn', async () => {
15151541
const key = queryKey()

0 commit comments

Comments
 (0)