Skip to content

Commit 3d627ee

Browse files
committed
reducer tests
1 parent 5c094ec commit 3d627ee

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('GlobalWritesStore Store', function () {
199199
});
200200
});
201201

202-
it.only('not managed -> sharding -> sharding error', async function () {
202+
it('not managed -> sharding -> sharding error', async function () {
203203
let mockFailure = false;
204204
// initial state === unsharded
205205
const store = createStore({
@@ -343,15 +343,61 @@ describe('GlobalWritesStore Store', function () {
343343
expect(store.getState().status).to.equal('SHARD_KEY_CORRECT');
344344
});
345345

346-
it('sharding error', async function () {
346+
it('sharding error -> cancelling request -> not managed', async function () {
347+
// initial state === sharding error
348+
let mockManagedNamespace = true;
349+
let mockShardingError = true;
350+
clock = sinon.useFakeTimers({
351+
shouldAdvanceTime: true,
352+
});
353+
const store = createStore({
354+
isNamespaceManaged: Sinon.fake(() => mockManagedNamespace),
355+
hasShardingError: Sinon.fake(() => mockShardingError),
356+
});
357+
await waitFor(() => {
358+
expect(store.getState().status).to.equal('SHARDING_ERROR');
359+
expect(store.getState().managedNamespace).to.equal(managedNamespace);
360+
});
361+
362+
// user triggers a cancellation
363+
const promise = store.dispatch(cancelSharding());
364+
mockManagedNamespace = false;
365+
mockShardingError = false;
366+
await promise;
367+
expect(store.getState().status).to.equal('UNSHARDED');
368+
expect(confirmationStub).to.have.been.called;
369+
});
370+
371+
it('sharding error -> submitting form -> sharding -> sharded', async function () {
372+
// initial state === sharding error=
373+
let mockShardingError = true;
374+
let mockShardKey = false;
375+
clock = sinon.useFakeTimers({
376+
shouldAdvanceTime: true,
377+
});
347378
const store = createStore({
348379
isNamespaceManaged: () => true,
349-
hasShardingError: () => true,
380+
hasShardingError: Sinon.fake(() => mockShardingError),
381+
hasShardKey: Sinon.fake(() => mockShardKey),
350382
});
351383
await waitFor(() => {
352384
expect(store.getState().status).to.equal('SHARDING_ERROR');
353385
expect(store.getState().managedNamespace).to.equal(managedNamespace);
354386
});
387+
388+
// user submits the form
389+
const promise = store.dispatch(createShardKey(shardKeyData));
390+
mockShardingError = false;
391+
expect(store.getState().status).to.equal('SUBMITTING_FOR_SHARDING_ERROR');
392+
await promise;
393+
expect(store.getState().status).to.equal('SHARDING');
394+
395+
// the key is created
396+
mockShardKey = true;
397+
clock.tick(POLLING_INTERVAL);
398+
await waitFor(() => {
399+
expect(store.getState().status).to.equal('SHARD_KEY_CORRECT');
400+
});
355401
});
356402

357403
it('sends correct data to the server when creating a shard key', async function () {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
440440
GlobalWritesActionTypes.CancellingShardingFinished
441441
) &&
442442
(state.status === ShardingStatuses.CANCELLING_SHARDING ||
443-
state.status === ShardingStatuses.SHARDING_ERROR)
443+
state.status === ShardingStatuses.SHARDING_ERROR ||
444+
state.status === ShardingStatuses.CANCELLING_SHARDING_ERROR)
444445
// the error might come before the cancel request was processed
445446
) {
446447
return {

0 commit comments

Comments
 (0)