Skip to content

Commit

Permalink
test(queryCache): add failing test for partially undefined key (TanSt…
Browse files Browse the repository at this point in the history
…ack#3741)

The current behavior when using functions such as invalidateQueries or setQueriesData
is unexpected when objects with undefined properties are involved.

A key containing an object with an undefined property is hashed without the undefined property,
yet the property is considered in the partialDeepEqual function.

This creates some confusing scenarios, as demonstrated in the discussion on TkDodo's blog
<TkDodo/blog-comments#71 (comment)> and in the
referenced issue

This commit includes a failing test to demonstrate the expected behavior
  • Loading branch information
maxandron committed Dec 9, 2022
1 parent 9b21609 commit e5a507c
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/query-core/src/tests/queryCache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ describe('queryCache', () => {
await queryClient.prefetchQuery(key2, () => 'data2')
expect(queryCache.findAll().length).toBe(2)
})

test('should return all the queries when key contains object with an undefined property', async () => {
const baseKey = queryKey()
await queryClient.prefetchQuery([...baseKey, { a: 1 }], () => 'data1')
expect(queryCache.findAll([...baseKey, { a: undefined }])).toHaveLength(1)
})
})

describe('QueryCacheConfig.onError', () => {
Expand Down

0 comments on commit e5a507c

Please sign in to comment.