@@ -2,6 +2,8 @@ import { createApi } from '@reduxjs/toolkit/query'
22import { delay } from 'msw'
33import { setupApiStore } from './helpers'
44import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
5+ import { createListenerMiddleware } from '@reduxjs/toolkit'
6+
57
68const mockBaseQuery = vi
79 . fn ( )
@@ -125,18 +127,27 @@ describe('polling tests', () => {
125127
126128 it ( 'respects skipPollOnFocusLost' , async ( ) => {
127129 mockBaseQuery . mockClear ( )
128- storeRef . store . dispatch (
129- getPosts . initiate ( 1 , {
130+ const listenerMiddleware = createListenerMiddleware ( )
131+ const storeListenerRef = setupApiStore ( api , undefined , {
132+ middleware : {
133+ concat : [ listenerMiddleware . middleware ] ,
134+ } ,
135+ withoutTestLifecycles : true ,
136+ } )
137+
138+ storeListenerRef . store . dispatch (
139+ getPosts . initiate ( 2 , {
130140 subscriptionOptions : { pollingInterval : 10 , skipPollOnFocusLost : true } ,
131141 subscribe : true ,
132142 } )
133143 )
144+ storeListenerRef . store . dispatch ( api . internalActions ?. onFocusLost ( ) )
134145
135- await delay ( 20 )
146+ await delay ( 50 )
136147 const callsWithSkip = mockBaseQuery . mock . calls . length
137148
138- storeRef . store . dispatch (
139- getPosts . initiate ( 1 , {
149+ storeListenerRef . store . dispatch (
150+ getPosts . initiate ( 2 , {
140151 subscriptionOptions : {
141152 pollingInterval : 10 ,
142153 skipPollOnFocusLost : false ,
@@ -145,17 +156,28 @@ describe('polling tests', () => {
145156 } )
146157 )
147158
148- await delay ( 30 )
159+ storeListenerRef . store . dispatch ( api . internalActions ?. onFocus ( ) )
160+
161+ await delay ( 50 )
149162 const callsWithoutSkip = mockBaseQuery . mock . calls . length
150- console . log ( callsWithSkip , callsWithoutSkip )
151163
152164 expect ( callsWithSkip ) . toBe ( 1 )
153165 expect ( callsWithoutSkip ) . toBeGreaterThan ( 2 )
166+
167+ storeListenerRef . store . dispatch ( api . util . resetApiState ( ) )
154168 } )
155169
156- it ( 'replaces skipPollOnFocusLost with most recent mount' , async ( ) => {
157- storeRef . store . dispatch (
158- getPosts . initiate ( 1 , {
170+ it ( 'respects skipPollOnFocusLost if any subscription is true' , async ( ) => {
171+ const listenerMiddleware = createListenerMiddleware ( )
172+ const storeListenerRef = setupApiStore ( api , undefined , {
173+ middleware : {
174+ concat : [ listenerMiddleware . middleware ] ,
175+ } ,
176+ withoutTestLifecycles : true ,
177+ } )
178+
179+ storeListenerRef . store . dispatch (
180+ getPosts . initiate ( 3 , {
159181 subscriptionOptions : {
160182 pollingInterval : 10 ,
161183 skipPollOnFocusLost : false ,
@@ -167,13 +189,15 @@ describe('polling tests', () => {
167189 await delay ( 50 )
168190 const callsWithSkip = mockBaseQuery . mock . calls . length
169191
170- storeRef . store . dispatch (
171- getPosts . initiate ( 1 , {
192+ storeListenerRef . store . dispatch (
193+ getPosts . initiate ( 3 , {
172194 subscriptionOptions : { pollingInterval : 15 , skipPollOnFocusLost : true } ,
173195 subscribe : true ,
174196 } )
175197 )
176198
199+ storeListenerRef . store . dispatch ( api . internalActions ?. onFocusLost ( ) )
200+
177201 await delay ( 50 )
178202 const callsWithoutSkip = mockBaseQuery . mock . calls . length
179203
0 commit comments