Skip to content

Commit 9560eff

Browse files
committed
feat(useQuery): run placeholderData through select
add a test for placeholderDataFunction
1 parent f167c6c commit 9560eff

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/react/tests/useQuery.test.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,6 +3475,57 @@ describe('useQuery', () => {
34753475
])
34763476
})
34773477

3478+
it('placeholder data function result should run through select', async () => {
3479+
const key1 = queryKey()
3480+
3481+
const states: UseQueryResult<string>[] = []
3482+
let placeholderFunctionRunCount = 0
3483+
3484+
function Page() {
3485+
const state = useQuery(key1, () => 1, {
3486+
placeholderData: () => {
3487+
placeholderFunctionRunCount++
3488+
return 23
3489+
},
3490+
select: data => String(data * 2),
3491+
})
3492+
3493+
states.push(state)
3494+
3495+
return (
3496+
<div>
3497+
<h2>Data: {state.data}</h2>
3498+
<div>Status: {state.status}</div>
3499+
</div>
3500+
)
3501+
}
3502+
3503+
const rendered = renderWithClient(queryClient, <Page />)
3504+
await waitFor(() => rendered.getByText('Data: 2'))
3505+
3506+
rendered.rerender(<Page />)
3507+
3508+
expect(states).toMatchObject([
3509+
{
3510+
isSuccess: true,
3511+
isPlaceholderData: true,
3512+
data: '46',
3513+
},
3514+
{
3515+
isSuccess: true,
3516+
isPlaceholderData: false,
3517+
data: '2',
3518+
},
3519+
{
3520+
isSuccess: true,
3521+
isPlaceholderData: false,
3522+
data: '2',
3523+
},
3524+
])
3525+
3526+
expect(placeholderFunctionRunCount).toEqual(1)
3527+
})
3528+
34783529
it('should cancel the query function when there are no more subscriptions', async () => {
34793530
const key = queryKey()
34803531
let cancelFn: jest.Mock = jest.fn()

0 commit comments

Comments
 (0)