Skip to content

Commit e754b7a

Browse files
committed
remove TEST_POLLING_INTERVAL
1 parent 84a6d70 commit e754b7a

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

packages/compass-global-writes/src/store/index.spec.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { setupStore } from '../../tests/create-store';
44
import {
55
createShardKey,
66
type CreateShardKeyData,
7-
TEST_POLLING_INTERVAL,
87
unmanageNamespace,
98
cancelSharding,
109
setPluginTitleVisibility,
10+
POLLING_INTERVAL,
1111
} from './reducer';
1212
import sinon from 'sinon';
1313
import type {
@@ -152,6 +152,7 @@ function createStore({
152152

153153
describe('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
});

packages/compass-global-writes/src/store/reducer.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import {
66
} from '@mongodb-js/compass-components';
77
import type { ManagedNamespace } from '../services/atlas-global-writes-service';
88

9-
const POLLING_INTERVAL = 5000;
10-
export const TEST_POLLING_INTERVAL = 100;
9+
export const POLLING_INTERVAL = 5000;
1110

1211
export function isAction<A extends Action>(
1312
action: Action,
@@ -675,12 +674,9 @@ const pollForShardKey = (): GlobalWritesThunkAction<
675674
) {
676675
return;
677676
}
678-
const timeout = setTimeout(
679-
() => {
680-
void dispatch(fetchNamespaceShardKey());
681-
},
682-
process.env.NODE_ENV !== 'test' ? POLLING_INTERVAL : TEST_POLLING_INTERVAL
683-
);
677+
const timeout = setTimeout(() => {
678+
void dispatch(fetchNamespaceShardKey());
679+
}, POLLING_INTERVAL);
684680

685681
dispatch({
686682
type: GlobalWritesActionTypes.NextPollingTimeoutSet,

0 commit comments

Comments
 (0)