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
2 changes: 1 addition & 1 deletion docs/community-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ others:
description: 'VS Code extension for TanStack Query (React Query): visualize query keys, cache invalidation/refetch flows, and file impact graph',
},
{
title: 'Tanstack Query Visualizer',
title: 'TanStack Query Visualizer',
url: 'https://tanstack-query-visualizer.sofi.coop/',
description: 'An interactive sandbox that visualizes the relationship between mutations and query keys.',
},
Expand Down
35 changes: 35 additions & 0 deletions packages/query-core/src/__tests__/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
hashQueryKeyByOptions,
isPlainArray,
isPlainObject,
isValidTimeout,
keepPreviousData,
matchMutation,
partialMatchKey,
Expand Down Expand Up @@ -166,6 +167,18 @@ describe('core/utils', () => {
const b = [{ a: null, c: 'c', d: [{ d: 'd ' }] }]
expect(partialMatchKey(a, b)).toEqual(false)
})

it('should treat undefined object properties as matching missing properties', () => {
const queryKeyWithUndefined = ['todos', { filters: undefined }]
const queryKeyWithoutProperty = ['todos', {}]

expect(
partialMatchKey(queryKeyWithoutProperty, queryKeyWithUndefined),
).toEqual(true)
expect(
partialMatchKey(queryKeyWithUndefined, queryKeyWithoutProperty),
).toEqual(true)
})
})

describe('replaceEqualDeep', () => {
Expand Down Expand Up @@ -559,6 +572,28 @@ describe('core/utils', () => {

expect(hashKey(nested1)).toEqual(hashKey(nested2))
})

it('should hash undefined object properties the same as missing properties', () => {
const withUndefined = ['todos', { filters: undefined }]
const withoutProperty = ['todos', {}]

expect(hashKey(withUndefined)).toEqual(hashKey(withoutProperty))
})
})

describe('isValidTimeout', () => {
it('should accept valid timeout values', () => {
expect(isValidTimeout(0)).toEqual(true)
expect(isValidTimeout(1_000)).toEqual(true)
})

it('should reject invalid timeout values', () => {
expect(isValidTimeout(-1)).toEqual(false)
expect(isValidTimeout(Number.NaN)).toEqual(false)
expect(isValidTimeout(Number.POSITIVE_INFINITY)).toEqual(false)
expect(isValidTimeout('1000')).toEqual(false)
expect(isValidTimeout(undefined)).toEqual(false)
})
})

describe('ensureQueryFn', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function functionalUpdate<TInput, TOutput>(
}

export function isValidTimeout(value: unknown): value is number {
return typeof value === 'number' && value >= 0 && value !== Infinity
return typeof value === 'number' && Number.isFinite(value) && value >= 0
}

export function timeUntilStale(updatedAt: number, staleTime?: number): number {
Expand Down