Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions packages/solid-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,69 @@ describe('useQuery', () => {
expect(states[1]).toMatchObject({ data: 'test' })
})

it('should maintain referential equality when reconcile option is a string key', async () => {
const key = queryKey()
const states: Array<Array<{ id: string; done: boolean }>> = []

let count = 0

function Page() {
const state = useQuery(() => ({
queryKey: key,
queryFn: async () => {
await sleep(10)
count++
return [
{ id: '1', done: false },
{ id: '2', done: count > 1 },
]
},
reconcile: 'id',
}))

createEffect(() => {
if (state.data) {
states.push(state.data)
}
})

const { refetch } = state

return (
<div>
<button onClick={() => refetch()}>refetch</button>
<h2>Data: {JSON.stringify(state.data)}</h2>
</div>
)
}

const rendered = render(() => (
<QueryClientProvider client={queryClient}>
<Page />
</QueryClientProvider>
))

await vi.advanceTimersByTimeAsync(10)
expect(
rendered.getByText(
'Data: [{"id":"1","done":false},{"id":"2","done":false}]',
),
).toBeInTheDocument()
expect(states).toHaveLength(1)

fireEvent.click(rendered.getByRole('button', { name: /refetch/i }))
await vi.advanceTimersByTimeAsync(10)
expect(
rendered.getByText(
'Data: [{"id":"1","done":false},{"id":"2","done":true}]',
),
).toBeInTheDocument()

// reconcile by 'id' updates in-place, so the array reference stays the same
// and the effect is not triggered again
expect(states).toHaveLength(1)
})

it('should share equal data structures between query results', async () => {
const key = queryKey()
const result1 = [
Expand Down
Loading