@@ -173,3 +173,130 @@ describe('calling initiate should have resulting queryCacheKey match baseQuery q
173
173
)
174
174
} )
175
175
} )
176
+
177
+ describe ( 'getRunningQueryThunk with multiple stores' , ( ) => {
178
+ test ( 'should isolate running queries between different store instances using the same API' , async ( ) => {
179
+ // Create a shared API instance
180
+ const sharedApi = createApi ( {
181
+ baseQuery : fakeBaseQuery ( ) ,
182
+ endpoints : ( build ) => ( {
183
+ testQuery : build . query < string , string > ( {
184
+ async queryFn ( arg ) {
185
+ // Add delay to ensure queries are running when we check
186
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) )
187
+ return { data : `result-${ arg } ` }
188
+ } ,
189
+ } ) ,
190
+ } ) ,
191
+ } )
192
+
193
+ // Create two separate stores using the same API instance
194
+ const store1 = setupApiStore ( sharedApi , undefined , {
195
+ withoutTestLifecycles : true ,
196
+ } ) . store
197
+ const store2 = setupApiStore ( sharedApi , undefined , {
198
+ withoutTestLifecycles : true ,
199
+ } ) . store
200
+
201
+ // Start queries on both stores
202
+ const query1Promise = store1 . dispatch (
203
+ sharedApi . endpoints . testQuery . initiate ( 'arg1' ) ,
204
+ )
205
+ const query2Promise = store2 . dispatch (
206
+ sharedApi . endpoints . testQuery . initiate ( 'arg2' ) ,
207
+ )
208
+
209
+ // Verify that getRunningQueryThunk returns the correct query for each store
210
+ const runningQuery1 = store1 . dispatch (
211
+ sharedApi . util . getRunningQueryThunk ( 'testQuery' , 'arg1' ) ,
212
+ )
213
+ const runningQuery2 = store2 . dispatch (
214
+ sharedApi . util . getRunningQueryThunk ( 'testQuery' , 'arg2' ) ,
215
+ )
216
+
217
+ // Each store should only see its own running query
218
+ expect ( runningQuery1 ) . toBeDefined ( )
219
+ expect ( runningQuery2 ) . toBeDefined ( )
220
+ expect ( runningQuery1 ?. requestId ) . toBe ( query1Promise . requestId )
221
+ expect ( runningQuery2 ?. requestId ) . toBe ( query2Promise . requestId )
222
+
223
+ // Cross-store queries should not be visible
224
+ const crossQuery1 = store1 . dispatch (
225
+ sharedApi . util . getRunningQueryThunk ( 'testQuery' , 'arg2' ) ,
226
+ )
227
+ const crossQuery2 = store2 . dispatch (
228
+ sharedApi . util . getRunningQueryThunk ( 'testQuery' , 'arg1' ) ,
229
+ )
230
+
231
+ expect ( crossQuery1 ) . toBeUndefined ( )
232
+ expect ( crossQuery2 ) . toBeUndefined ( )
233
+
234
+ // Wait for queries to complete
235
+ await Promise . all ( [ query1Promise , query2Promise ] )
236
+
237
+ // After completion, getRunningQueryThunk should return undefined for both stores
238
+ const completedQuery1 = store1 . dispatch (
239
+ sharedApi . util . getRunningQueryThunk ( 'testQuery' , 'arg1' ) ,
240
+ )
241
+ const completedQuery2 = store2 . dispatch (
242
+ sharedApi . util . getRunningQueryThunk ( 'testQuery' , 'arg2' ) ,
243
+ )
244
+
245
+ expect ( completedQuery1 ) . toBeUndefined ( )
246
+ expect ( completedQuery2 ) . toBeUndefined ( )
247
+ } )
248
+
249
+ test ( 'should handle same query args on different stores independently' , async ( ) => {
250
+ // Create a shared API instance
251
+ const sharedApi = createApi ( {
252
+ baseQuery : fakeBaseQuery ( ) ,
253
+ endpoints : ( build ) => ( {
254
+ sameArgQuery : build . query < string , string > ( {
255
+ async queryFn ( arg ) {
256
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) )
257
+ return { data : `result-${ arg } -${ Math . random ( ) } ` }
258
+ } ,
259
+ } ) ,
260
+ } ) ,
261
+ } )
262
+
263
+ // Create two separate stores
264
+ const store1 = setupApiStore ( sharedApi , undefined , {
265
+ withoutTestLifecycles : true ,
266
+ } ) . store
267
+ const store2 = setupApiStore ( sharedApi , undefined , {
268
+ withoutTestLifecycles : true ,
269
+ } ) . store
270
+
271
+ // Start the same query on both stores
272
+ const sameArg = 'shared-arg'
273
+ const query1Promise = store1 . dispatch (
274
+ sharedApi . endpoints . sameArgQuery . initiate ( sameArg ) ,
275
+ )
276
+ const query2Promise = store2 . dispatch (
277
+ sharedApi . endpoints . sameArgQuery . initiate ( sameArg ) ,
278
+ )
279
+
280
+ // Both stores should see their own running query with the same cache key
281
+ const runningQuery1 = store1 . dispatch (
282
+ sharedApi . util . getRunningQueryThunk ( 'sameArgQuery' , sameArg ) ,
283
+ )
284
+ const runningQuery2 = store2 . dispatch (
285
+ sharedApi . util . getRunningQueryThunk ( 'sameArgQuery' , sameArg ) ,
286
+ )
287
+
288
+ expect ( runningQuery1 ) . toBeDefined ( )
289
+ expect ( runningQuery2 ) . toBeDefined ( )
290
+ expect ( runningQuery1 ?. requestId ) . toBe ( query1Promise . requestId )
291
+ expect ( runningQuery2 ?. requestId ) . toBe ( query2Promise . requestId )
292
+
293
+ // The request IDs should be different even though the cache key is the same
294
+ expect ( runningQuery1 ?. requestId ) . not . toBe ( runningQuery2 ?. requestId )
295
+
296
+ // But the cache keys should be the same
297
+ expect ( runningQuery1 ?. queryCacheKey ) . toBe ( runningQuery2 ?. queryCacheKey )
298
+
299
+ // Wait for completion
300
+ await Promise . all ( [ query1Promise , query2Promise ] )
301
+ } )
302
+ } )
0 commit comments