Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions packages/query-core/src/__tests__/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,30 @@ describe('core/utils', () => {
const b = [{ a: null, c: 'c', d: [{ d: 'd ' }] }]
expect(partialMatchKey(a, b)).toEqual(false)
})

it('should treat object property set to undefined as missing property', () => {
const a = ['todos']
const b = ['todos', { filter: undefined }]
expect(partialMatchKey(a, b)).toEqual(true)
})

it('should treat empty object and object with undefined values as equivalent', () => {
const a = ['todos', {}]
const b = ['todos', { filter: undefined }]
expect(partialMatchKey(a, b)).toEqual(true)
})

it('should match queries with specific filter when using undefined filter', () => {
const a = ['todos', { filter: 'done' }]
const b = ['todos', { filter: undefined }]
expect(partialMatchKey(a, b)).toEqual(true)
})

it('should not match when filter has a non-undefined value that differs', () => {
const a = ['todos', { filter: 'active' }]
const b = ['todos', { filter: 'done' }]
expect(partialMatchKey(a, b)).toEqual(false)
})
})

describe('replaceEqualDeep', () => {
Expand Down
18 changes: 17 additions & 1 deletion packages/query-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,23 @@ export function partialMatchKey(a: any, b: any): boolean {
}

if (a && b && typeof a === 'object' && typeof b === 'object') {
return Object.keys(b).every((key) => partialMatchKey(a[key], b[key]))
return Object.keys(b).every((key) => {
if (b[key] === undefined) {
return true
}
if (
a[key] === undefined &&
b[key] &&
typeof b[key] === 'object' &&
Object.values(b[key]).every((v: any) => v === undefined)
) {
return true
}
if (a[key] === undefined) {
return false
}
return partialMatchKey(a[key], b[key])
})
}

return false
Expand Down