@@ -4,10 +4,10 @@ import { setupStore } from '../../tests/create-store';
44import {
55 createShardKey ,
66 type CreateShardKeyData ,
7- TEST_POLLING_INTERVAL ,
87 unmanageNamespace ,
98 cancelSharding ,
109 setPluginTitleVisibility ,
10+ POLLING_INTERVAL ,
1111} from './reducer' ;
1212import sinon from 'sinon' ;
1313import type {
@@ -152,6 +152,7 @@ function createStore({
152152
153153describe ( 'GlobalWritesStore Store' , function ( ) {
154154 let confirmationStub : Sinon . SinonStub ;
155+ let clock : Sinon . SinonFakeTimers ;
155156
156157 beforeEach ( ( ) => {
157158 confirmationStub = sinon
@@ -161,6 +162,7 @@ describe('GlobalWritesStore Store', function () {
161162
162163 afterEach ( ( ) => {
163164 sinon . restore ( ) ;
165+ clock && clock . restore ( ) ;
164166 } ) ;
165167
166168 it ( 'sets the initial state' , function ( ) {
@@ -182,14 +184,17 @@ describe('GlobalWritesStore Store', function () {
182184 } ) ;
183185
184186 // user requests sharding
187+ clock = sinon . useFakeTimers ( {
188+ shouldAdvanceTime : true ,
189+ } ) ;
185190 const promise = store . dispatch ( createShardKey ( shardKeyData ) ) ;
186191 expect ( store . getState ( ) . status ) . to . equal ( 'SUBMITTING_FOR_SHARDING' ) ;
187192 await promise ;
188193 expect ( store . getState ( ) . status ) . to . equal ( 'SHARDING' ) ;
189194
190195 // sharding ends with a shardKey
191196 mockShardKey = true ;
192- await wait ( TEST_POLLING_INTERVAL ) ;
197+ clock . tick ( POLLING_INTERVAL ) ;
193198 await waitFor ( ( ) => {
194199 expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_CORRECT' ) ;
195200 } ) ;
@@ -207,14 +212,17 @@ describe('GlobalWritesStore Store', function () {
207212 } ) ;
208213
209214 // user requests sharding
215+ clock = sinon . useFakeTimers ( {
216+ shouldAdvanceTime : true ,
217+ } ) ;
210218 const promise = store . dispatch ( createShardKey ( shardKeyData ) ) ;
211219 expect ( store . getState ( ) . status ) . to . equal ( 'SUBMITTING_FOR_SHARDING' ) ;
212220 await promise ;
213221 expect ( store . getState ( ) . status ) . to . equal ( 'SHARDING' ) ;
214222
215223 // sharding ends with an error
216224 mockFailure = true ;
217- await wait ( TEST_POLLING_INTERVAL ) ;
225+ clock . tick ( POLLING_INTERVAL ) ;
218226 await waitFor ( ( ) => {
219227 expect ( store . getState ( ) . status ) . to . equal ( 'SHARDING_ERROR' ) ;
220228 } ) ;
@@ -240,6 +248,9 @@ describe('GlobalWritesStore Store', function () {
240248 it ( 'sharding -> valid shard key' , async function ( ) {
241249 let mockShardKey = false ;
242250 // initial state === sharding
251+ clock = sinon . useFakeTimers ( {
252+ shouldAdvanceTime : true ,
253+ } ) ;
243254 const store = createStore ( {
244255 isNamespaceManaged : ( ) => true ,
245256 hasShardKey : Sinon . fake ( ( ) => mockShardKey ) ,
@@ -251,7 +262,7 @@ describe('GlobalWritesStore Store', function () {
251262
252263 // sharding ends with a shardKey
253264 mockShardKey = true ;
254- await wait ( TEST_POLLING_INTERVAL ) ;
265+ clock . tick ( POLLING_INTERVAL ) ;
255266 await waitFor ( ( ) => {
256267 expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_CORRECT' ) ;
257268 } ) ;
@@ -283,6 +294,9 @@ describe('GlobalWritesStore Store', function () {
283294 let mockShardKey = false ;
284295 confirmationStub . resolves ( true ) ;
285296 // initial state === sharding
297+ clock = sinon . useFakeTimers ( {
298+ shouldAdvanceTime : true ,
299+ } ) ;
286300 const store = createStore ( {
287301 isNamespaceManaged : ( ) => true ,
288302 hasShardKey : Sinon . fake ( ( ) => mockShardKey ) ,
@@ -294,13 +308,14 @@ describe('GlobalWritesStore Store', function () {
294308 // user leaves the workspace
295309 store . dispatch ( setPluginTitleVisibility ( false ) ) ;
296310 mockShardKey = true ;
297- await wait ( TEST_POLLING_INTERVAL * 2 ) ;
311+ clock . tick ( POLLING_INTERVAL ) ;
312+ clock . tick ( POLLING_INTERVAL ) ;
298313 expect ( store . getState ( ) . pollingTimeout ) . to . be . undefined ;
299314 expect ( store . getState ( ) . status ) . to . equal ( 'SHARDING' ) ; // no update
300315
301316 // user comes back
302317 store . dispatch ( setPluginTitleVisibility ( true ) ) ;
303- await wait ( TEST_POLLING_INTERVAL ) ;
318+ await wait ( POLLING_INTERVAL ) ;
304319 await waitFor ( ( ) => {
305320 expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_CORRECT' ) ; // now there is an update
306321 } ) ;
0 commit comments