Skip to content

Commit 89c7a67

Browse files
committed
Hack in a way to expose polling update counters in tests
# Conflicts: # packages/toolkit/src/query/core/buildMiddleware/index.ts # packages/toolkit/src/query/core/buildMiddleware/types.ts
1 parent 5ea73f7 commit 89c7a67

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

packages/toolkit/src/query/core/buildMiddleware/batchActions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ export const buildBatchedActionsHandler: InternalHandlerBuilder<
155155

156156
let actionShouldContinue = true
157157

158+
// HACK Sneak the test-only polling state back out
159+
if (
160+
process.env.NODE_ENV === 'test' &&
161+
typeof action.type === 'string' &&
162+
action.type === `${api.reducerPath}/getPolling`
163+
) {
164+
return [false, internalState.currentPolls] as any
165+
}
166+
158167
if (didMutate) {
159168
if (!updateSyncTimer) {
160169
// We only use the subscription state for the Redux DevTools at this point,

packages/toolkit/src/query/core/buildMiddleware/polling.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
2121
refetchQuery,
2222
internalState,
2323
}) => {
24-
const currentPolls: QueryStateMeta<{
25-
nextPollTimestamp: number
26-
timeout?: TimeoutId
27-
pollingInterval: number
28-
}> = {}
24+
const { currentPolls } = internalState
2925

3026
const { currentSubscriptions } = internalState
3127

@@ -120,6 +116,13 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
120116

121117
const { lowestPollingInterval } = findLowestPollingInterval(subscriptions)
122118

119+
// HACK add extra data to track how many times this has been called in tests
120+
if (process.env.NODE_ENV === 'test') {
121+
const updateCounters = ((currentPolls as any).pollUpdateCounters ??= {})
122+
updateCounters[queryCacheKey] ??= 0
123+
updateCounters[queryCacheKey]++
124+
}
125+
123126
if (!Number.isFinite(lowestPollingInterval)) {
124127
cleanupPollForKey(queryCacheKey)
125128
return

packages/toolkit/src/query/core/buildMiddleware/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export type TimeoutId = ReturnType<typeof setTimeout>
3939

4040
export interface InternalMiddlewareState {
4141
currentSubscriptions: SubscriptionInternalState
42+
currentPolls: QueryStateMeta<{
43+
nextPollTimestamp: number
44+
timeout?: TimeoutId
45+
pollingInterval: number
46+
}>
4247
runningQueries: Map<
4348
Dispatch,
4449
Record<

packages/toolkit/src/query/core/module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ export const coreModule = ({
621621

622622
const internalState: InternalMiddlewareState = {
623623
currentSubscriptions: new Map(),
624+
currentPolls: {},
624625
runningQueries: new Map(),
625626
runningMutations: new Map(),
626627
}

0 commit comments

Comments
 (0)