Skip to content

Commit 38aa530

Browse files
committed
Work around setupApiStore trying to add lifecycle hooks inside tests
1 parent baae05e commit 38aa530

File tree

5 files changed

+70
-31
lines changed

5 files changed

+70
-31
lines changed

packages/toolkit/src/query/tests/createApi.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,14 @@ describe('endpoint definition typings', () => {
332332
})
333333
}
334334
let api = getNewApi()
335-
let storeRef = setupApiStore(api)
336335
beforeEach(() => {
337336
api = getNewApi()
338-
storeRef = setupApiStore(api)
339337
})
340338

341339
test('pre-modification behaviour', async () => {
340+
const storeRef = setupApiStore(api, undefined, {
341+
withoutTestLifecycles: true,
342+
})
342343
storeRef.store.dispatch(api.endpoints.query1.initiate('in1'))
343344
storeRef.store.dispatch(api.endpoints.query2.initiate('in2'))
344345
storeRef.store.dispatch(api.endpoints.mutation1.initiate('in1'))
@@ -397,6 +398,9 @@ describe('endpoint definition typings', () => {
397398
})
398399

399400
test('warn on wrong tagType', async () => {
401+
const storeRef = setupApiStore(api, undefined, {
402+
withoutTestLifecycles: true,
403+
})
400404
// only type-test this part
401405
if (2 > 1) {
402406
api.enhanceEndpoints({
@@ -455,6 +459,9 @@ describe('endpoint definition typings', () => {
455459
})
456460

457461
test('modify', () => {
462+
const storeRef = setupApiStore(api, undefined, {
463+
withoutTestLifecycles: true,
464+
})
458465
api.enhanceEndpoints({
459466
endpoints: {
460467
query1: {
@@ -751,7 +758,9 @@ test('providesTags and invalidatesTags can use baseQueryMeta', async () => {
751758
}),
752759
})
753760

754-
const storeRef = setupApiStore(api)
761+
const storeRef = setupApiStore(api, undefined, {
762+
withoutTestLifecycles: true,
763+
})
755764

756765
await storeRef.store.dispatch(api.endpoints.query.initiate())
757766
expect('request' in _meta! && 'response' in _meta!).toBe(true)

packages/toolkit/src/query/tests/helpers.tsx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ export function setupApiStore<
173173
util: { resetApiState(): any }
174174
},
175175
R extends Record<string, Reducer<any, any>> = Record<never, never>
176-
>(api: A, extraReducers?: R, withoutListeners?: boolean) {
176+
>(
177+
api: A,
178+
extraReducers?: R,
179+
options: { withoutListeners?: boolean; withoutTestLifecycles?: boolean } = {}
180+
) {
177181
const getStore = () =>
178182
configureStore({
179183
reducer: { api: api.reducer, ...extraReducers },
@@ -203,21 +207,23 @@ export function setupApiStore<
203207
}
204208
let cleanupListeners: () => void
205209

206-
beforeEach(() => {
207-
const store = getStore() as StoreType
208-
refObj.store = store
209-
refObj.wrapper = withProvider(store)
210-
if (!withoutListeners) {
211-
cleanupListeners = setupListeners(store.dispatch)
212-
}
213-
})
214-
afterEach(() => {
215-
cleanup()
216-
if (!withoutListeners) {
217-
cleanupListeners()
218-
}
219-
refObj.store.dispatch(api.util.resetApiState())
220-
})
210+
if (!options.withoutTestLifecycles) {
211+
beforeEach(() => {
212+
const store = getStore() as StoreType
213+
refObj.store = store
214+
refObj.wrapper = withProvider(store)
215+
if (!options.withoutListeners) {
216+
cleanupListeners = setupListeners(store.dispatch)
217+
}
218+
})
219+
afterEach(() => {
220+
cleanup()
221+
if (!options.withoutListeners) {
222+
cleanupListeners()
223+
}
224+
refObj.store.dispatch(api.util.resetApiState())
225+
})
226+
}
221227

222228
return refObj
223229
}

packages/toolkit/src/query/tests/invalidation.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ test.each(caseMatrix)(
100100
invalidatesTags,
101101
}),
102102
}),
103-
})
103+
}),
104+
undefined,
105+
{ withoutTestLifecycles: true }
104106
)
105107

106108
store.dispatch(providing.initiate())

packages/toolkit/src/query/tests/refetchingBehaviors.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ describe('refetchOnReconnect tests', () => {
366366
})
367367

368368
describe('customListenersHandler', () => {
369-
const storeRef = setupApiStore(defaultApi, undefined, true)
369+
const storeRef = setupApiStore(defaultApi, undefined, {
370+
withoutListeners: true,
371+
})
370372

371373
test('setupListeners accepts a custom callback and executes it', async () => {
372374
const consoleSpy = jest.spyOn(console, 'log')

packages/toolkit/src/query/tests/retry.test.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ describe('configuration', () => {
3333
}),
3434
})
3535

36-
const storeRef = setupApiStore(api)
36+
const storeRef = setupApiStore(api, undefined, {
37+
withoutTestLifecycles: true,
38+
})
3739
storeRef.store.dispatch(api.endpoints.q1.initiate({}))
3840

3941
await loopTimers(7)
@@ -58,7 +60,9 @@ describe('configuration', () => {
5860
}),
5961
})
6062

61-
const storeRef = setupApiStore(api)
63+
const storeRef = setupApiStore(api, undefined, {
64+
withoutTestLifecycles: true,
65+
})
6266
storeRef.store.dispatch(api.endpoints.q1.initiate({}))
6367

6468
await loopTimers(5)
@@ -87,7 +91,9 @@ describe('configuration', () => {
8791
}),
8892
})
8993

90-
const storeRef = setupApiStore(api)
94+
const storeRef = setupApiStore(api, undefined, {
95+
withoutTestLifecycles: true,
96+
})
9197

9298
storeRef.store.dispatch(api.endpoints.q1.initiate({}))
9399
await loopTimers(5)
@@ -123,7 +129,9 @@ describe('configuration', () => {
123129
}),
124130
})
125131

126-
const storeRef = setupApiStore(api)
132+
const storeRef = setupApiStore(api, undefined, {
133+
withoutTestLifecycles: true,
134+
})
127135
storeRef.store.dispatch(api.endpoints.q1.initiate({}))
128136

129137
await loopTimers(6)
@@ -148,7 +156,9 @@ describe('configuration', () => {
148156
}),
149157
})
150158

151-
const storeRef = setupApiStore(api)
159+
const storeRef = setupApiStore(api, undefined, {
160+
withoutTestLifecycles: true,
161+
})
152162

153163
storeRef.store.dispatch(api.endpoints.m1.initiate({}))
154164

@@ -177,7 +187,9 @@ describe('configuration', () => {
177187
}),
178188
})
179189

180-
const storeRef = setupApiStore(api)
190+
const storeRef = setupApiStore(api, undefined, {
191+
withoutTestLifecycles: true,
192+
})
181193

182194
storeRef.store.dispatch(api.endpoints.m1.initiate({}))
183195

@@ -202,7 +214,9 @@ describe('configuration', () => {
202214
}),
203215
})
204216

205-
const storeRef = setupApiStore(api)
217+
const storeRef = setupApiStore(api, undefined, {
218+
withoutTestLifecycles: true,
219+
})
206220

207221
storeRef.store.dispatch(api.endpoints.q1.initiate({}))
208222

@@ -232,7 +246,9 @@ describe('configuration', () => {
232246
}),
233247
})
234248

235-
const storeRef = setupApiStore(api)
249+
const storeRef = setupApiStore(api, undefined, {
250+
withoutTestLifecycles: true,
251+
})
236252

237253
const result = await storeRef.store.dispatch(api.endpoints.q1.initiate({}))
238254

@@ -277,7 +293,9 @@ describe('configuration', () => {
277293
}),
278294
})
279295

280-
const storeRef = setupApiStore(api)
296+
const storeRef = setupApiStore(api, undefined, {
297+
withoutTestLifecycles: true,
298+
})
281299

282300
storeRef.store.dispatch(api.endpoints.q1.initiate({}))
283301

@@ -312,7 +330,9 @@ describe('configuration', () => {
312330
}),
313331
})
314332

315-
const storeRef = setupApiStore(api)
333+
const storeRef = setupApiStore(api, undefined, {
334+
withoutTestLifecycles: true,
335+
})
316336
storeRef.store.dispatch(api.endpoints.q1.initiate({}))
317337

318338
await loopTimers()

0 commit comments

Comments
 (0)