@@ -2,6 +2,8 @@ import { createApi } from '@reduxjs/toolkit/query'
2
2
import { delay } from 'msw'
3
3
import { setupApiStore } from './helpers'
4
4
import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
5
+ import { createListenerMiddleware } from '@reduxjs/toolkit'
6
+
5
7
6
8
const mockBaseQuery = vi
7
9
. fn ( )
@@ -125,18 +127,27 @@ describe('polling tests', () => {
125
127
126
128
it ( 'respects skipPollOnFocusLost' , async ( ) => {
127
129
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 , {
130
140
subscriptionOptions : { pollingInterval : 10 , skipPollOnFocusLost : true } ,
131
141
subscribe : true ,
132
142
} )
133
143
)
144
+ storeListenerRef . store . dispatch ( api . internalActions ?. onFocusLost ( ) )
134
145
135
- await delay ( 20 )
146
+ await delay ( 50 )
136
147
const callsWithSkip = mockBaseQuery . mock . calls . length
137
148
138
- storeRef . store . dispatch (
139
- getPosts . initiate ( 1 , {
149
+ storeListenerRef . store . dispatch (
150
+ getPosts . initiate ( 2 , {
140
151
subscriptionOptions : {
141
152
pollingInterval : 10 ,
142
153
skipPollOnFocusLost : false ,
@@ -145,17 +156,28 @@ describe('polling tests', () => {
145
156
} )
146
157
)
147
158
148
- await delay ( 30 )
159
+ storeListenerRef . store . dispatch ( api . internalActions ?. onFocus ( ) )
160
+
161
+ await delay ( 50 )
149
162
const callsWithoutSkip = mockBaseQuery . mock . calls . length
150
- console . log ( callsWithSkip , callsWithoutSkip )
151
163
152
164
expect ( callsWithSkip ) . toBe ( 1 )
153
165
expect ( callsWithoutSkip ) . toBeGreaterThan ( 2 )
166
+
167
+ storeListenerRef . store . dispatch ( api . util . resetApiState ( ) )
154
168
} )
155
169
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 , {
159
181
subscriptionOptions : {
160
182
pollingInterval : 10 ,
161
183
skipPollOnFocusLost : false ,
@@ -167,13 +189,15 @@ describe('polling tests', () => {
167
189
await delay ( 50 )
168
190
const callsWithSkip = mockBaseQuery . mock . calls . length
169
191
170
- storeRef . store . dispatch (
171
- getPosts . initiate ( 1 , {
192
+ storeListenerRef . store . dispatch (
193
+ getPosts . initiate ( 3 , {
172
194
subscriptionOptions : { pollingInterval : 15 , skipPollOnFocusLost : true } ,
173
195
subscribe : true ,
174
196
} )
175
197
)
176
198
199
+ storeListenerRef . store . dispatch ( api . internalActions ?. onFocusLost ( ) )
200
+
177
201
await delay ( 50 )
178
202
const callsWithoutSkip = mockBaseQuery . mock . calls . length
179
203
0 commit comments