| 1 |
import { createApi } from '@reduxjs/toolkit/query' |
| 2 |
import { delay } from 'msw' |
| 3 |
import { setupApiStore } from '../../tests/utils/helpers' |
| 4 |
import type { SubscriptionSelectors } from '../core/buildMiddleware/types' |
| 5 |
|
| 6 |
const mockBaseQuery = vi |
| 7 |
.fn() |
| 8 |
.mockImplementation((args: any) => ({ data: args })) |
| 9 |
|
| 10 |
const api = createApi({ |
| 11 |
baseQuery: mockBaseQuery, |
| 12 |
tagTypes: ['Posts'], |
| 13 |
endpoints: (build) => ({ |
| 14 |
getPosts: build.query<unknown, number>({ |
| 15 |
query(pageNumber) { |
| 16 |
return { url: 'posts', params: pageNumber } |
| 17 |
}, |
| 18 |
providesTags: ['Posts'], |
| 19 |
}), |
| 20 |
}), |
| 21 |
}) |
| 22 |
const { getPosts } = api.endpoints |
| 23 |
|
| 24 |
const storeRef = setupApiStore(api) |
| 25 |
|
| 26 |
let getSubscriptions: SubscriptionSelectors['getSubscriptions'] |
| 27 |
|
| 28 |
beforeEach(() => { |
| 29 |
;({ getSubscriptions } = storeRef.store.dispatch( |
| 30 |
api.internalActions.internal_getRTKQSubscriptions(), |
| 31 |
) as unknown as SubscriptionSelectors) |
| 32 |
}) |
| 33 |
|
| 34 |
const getSubscribersForQueryCacheKey = (queryCacheKey: string) => |
| 35 |
getSubscriptions()[queryCacheKey] || {} |
| 36 |
const createSubscriptionGetter = (queryCacheKey: string) => () => |
| 37 |
getSubscribersForQueryCacheKey(queryCacheKey) |
| 38 |
|
| 39 |
describe('polling tests', () => { |
| 40 |
it('clears intervals when seeing a resetApiState action', async () => { |
| 41 |
await storeRef.store.dispatch( |
| 42 |
getPosts.initiate(1, { |
| 43 |
subscriptionOptions: { pollingInterval: 10 }, |
| 44 |
subscribe: true, |
| 45 |
}), |
| 46 |
) |
| 47 |
|
| 48 |
expect(mockBaseQuery).toHaveBeenCalledOnce() |
| 49 |
|
| 50 |
storeRef.store.dispatch(api.util.resetApiState()) |
| 51 |
|
| 52 |
await delay(30) |
| 53 |
|
| 54 |
expect(mockBaseQuery).toHaveBeenCalledOnce() |
| 55 |
}) |
| 56 |
|
| 57 |
it('replaces polling interval when the subscription options are updated', async () => { |
| 58 |
const { requestId, queryCacheKey, ...subscription } = |
| 59 |
storeRef.store.dispatch( |
| 60 |
getPosts.initiate(1, { |
| 61 |
subscriptionOptions: { pollingInterval: 10 }, |
| 62 |
subscribe: true, |
| 63 |
}), |
| 64 |
) |
| 65 |
|
| 66 |
const getSubs = createSubscriptionGetter(queryCacheKey) |
| 67 |
|
| 68 |
await delay(1) |
| 69 |
expect(Object.keys(getSubs())).toHaveLength(1) |
| 70 |
expect(getSubs()[requestId].pollingInterval).toBe(10) |
| 71 |
|
| 72 |
subscription.updateSubscriptionOptions({ pollingInterval: 20 }) |
| 73 |
|
| 74 |
await delay(1) |
| 75 |
expect(Object.keys(getSubs())).toHaveLength(1) |
| 76 |
expect(getSubs()[requestId].pollingInterval).toBe(20) |
| 77 |
}) |
| 78 |
|
| 79 |
it(`doesn't replace the interval when removing a shared query instance with a poll `, async () => { |
| 80 |
const subscriptionOne = storeRef.store.dispatch( |
| 81 |
getPosts.initiate(1, { |
| 82 |
subscriptionOptions: { pollingInterval: 10 }, |
| 83 |
subscribe: true, |
| 84 |
}), |
| 85 |
) |
| 86 |
|
| 87 |
storeRef.store.dispatch( |
| 88 |
getPosts.initiate(1, { |
| 89 |
subscriptionOptions: { pollingInterval: 10 }, |
| 90 |
subscribe: true, |
| 91 |
}), |
| 92 |
) |
| 93 |
|
| 94 |
await delay(10) |
| 95 |
|
| 96 |
const getSubs = createSubscriptionGetter(subscriptionOne.queryCacheKey) |
| 97 |
|
| 98 |
expect(Object.keys(getSubs())).toHaveLength(2) |
| 99 |
|
| 100 |
subscriptionOne.unsubscribe() |
| 101 |
|
| 102 |
await delay(1) |
| 103 |
expect(Object.keys(getSubs())).toHaveLength(1) |
| 104 |
}) |
| 105 |
|
| 106 |
it('uses lowest specified interval when two components are mounted', async () => { |
| 107 |
storeRef.store.dispatch( |
| 108 |
getPosts.initiate(1, { |
| 109 |
subscriptionOptions: { pollingInterval: 30000 }, |
| 110 |
subscribe: true, |
| 111 |
}), |
| 112 |
) |
| 113 |
|
| 114 |
storeRef.store.dispatch( |
| 115 |
getPosts.initiate(1, { |
| 116 |
subscriptionOptions: { pollingInterval: 10 }, |
| 117 |
subscribe: true, |
| 118 |
}), |
| 119 |
) |
| 120 |
|
| 121 |
await delay(20) |
| 122 |
|
| 123 |
expect(mockBaseQuery.mock.calls.length).toBeGreaterThanOrEqual(2) |
| 124 |
}) |
| 125 |
|
| 126 |
it('respects skipPollingIfUnfocused', async () => { |
| 127 |
mockBaseQuery.mockClear() |
| 128 |
storeRef.store.dispatch( |
| 129 |
getPosts.initiate(2, { |
| 130 |
subscriptionOptions: { |
| 131 |
pollingInterval: 10, |
| 132 |
skipPollingIfUnfocused: true, |
| 133 |
}, |
| 134 |
subscribe: true, |
| 135 |
}), |
| 136 |
) |
| 137 |
storeRef.store.dispatch(api.internalActions?.onFocusLost()) |
| 138 |
|
| 139 |
await delay(50) |
| 140 |
const callsWithSkip = mockBaseQuery.mock.calls.length |
| 141 |
|
| 142 |
storeRef.store.dispatch( |
| 143 |
getPosts.initiate(2, { |
| 144 |
subscriptionOptions: { |
| 145 |
pollingInterval: 10, |
| 146 |
skipPollingIfUnfocused: false, |
| 147 |
}, |
| 148 |
subscribe: true, |
| 149 |
}), |
| 150 |
) |
| 151 |
|
| 152 |
storeRef.store.dispatch(api.internalActions?.onFocus()) |
| 153 |
|
| 154 |
await delay(50) |
| 155 |
const callsWithoutSkip = mockBaseQuery.mock.calls.length |
| 156 |
|
| 157 |
expect(callsWithSkip).toBe(1) |
| 158 |
expect(callsWithoutSkip).toBeGreaterThan(2) |
| 159 |
|
| 160 |
storeRef.store.dispatch(api.util.resetApiState()) |
| 161 |
}) |
| 162 |
|
| 163 |
it('respects skipPollingIfUnfocused if at least one subscription has it', async () => { |
| 164 |
storeRef.store.dispatch( |
| 165 |
getPosts.initiate(3, { |
| 166 |
subscriptionOptions: { |
| 167 |
pollingInterval: 10, |
| 168 |
skipPollingIfUnfocused: false, |
| 169 |
}, |
| 170 |
subscribe: true, |
| 171 |
}), |
| 172 |
) |
| 173 |
|
| 174 |
await delay(50) |
| 175 |
const callsWithoutSkip = mockBaseQuery.mock.calls.length |
| 176 |
|
| 177 |
storeRef.store.dispatch( |
| 178 |
getPosts.initiate(3, { |
| 179 |
subscriptionOptions: { |
| 180 |
pollingInterval: 15, |
| 181 |
skipPollingIfUnfocused: true, |
| 182 |
}, |
| 183 |
subscribe: true, |
| 184 |
}), |
| 185 |
) |
| 186 |
|
| 187 |
storeRef.store.dispatch( |
| 188 |
getPosts.initiate(3, { |
| 189 |
subscriptionOptions: { |
| 190 |
pollingInterval: 20, |
| 191 |
skipPollingIfUnfocused: false, |
| 192 |
}, |
| 193 |
subscribe: true, |
| 194 |
}), |
| 195 |
) |
| 196 |
|
| 197 |
storeRef.store.dispatch(api.internalActions?.onFocusLost()) |
| 198 |
|
| 199 |
await delay(50) |
| 200 |
const callsWithSkip = mockBaseQuery.mock.calls.length |
| 201 |
|
| 202 |
expect(callsWithoutSkip).toBeGreaterThan(2) |
| 203 |
expect(callsWithSkip).toBe(callsWithoutSkip + 1) |
| 204 |
}) |
| 205 |
|
| 206 |
it('replaces skipPollingIfUnfocused when the subscription options are updated', async () => { |
| 207 |
const { requestId, queryCacheKey, ...subscription } = |
| 208 |
storeRef.store.dispatch( |
| 209 |
getPosts.initiate(1, { |
| 210 |
subscriptionOptions: { |
| 211 |
pollingInterval: 10, |
| 212 |
skipPollingIfUnfocused: false, |
| 213 |
}, |
| 214 |
subscribe: true, |
| 215 |
}), |
| 216 |
) |
| 217 |
|
| 218 |
const getSubs = createSubscriptionGetter(queryCacheKey) |
| 219 |
|
| 220 |
await delay(1) |
| 221 |
expect(Object.keys(getSubs())).toHaveLength(1) |
| 222 |
expect(getSubs()[requestId].skipPollingIfUnfocused).toBe(false) |
| 223 |
|
| 224 |
subscription.updateSubscriptionOptions({ |
| 225 |
pollingInterval: 20, |
| 226 |
skipPollingIfUnfocused: true, |
| 227 |
}) |
| 228 |
|
| 229 |
await delay(1) |
| 230 |
expect(Object.keys(getSubs())).toHaveLength(1) |
| 231 |
expect(getSubs()[requestId].skipPollingIfUnfocused).toBe(true) |
| 232 |
}) |
| 233 |
}) |