Skip to content

Commit

Permalink
reducer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paula-stacho committed Oct 23, 2024
1 parent b998ad2 commit 966e1e0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
52 changes: 49 additions & 3 deletions packages/compass-global-writes/src/store/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ describe('GlobalWritesStore Store', function () {
});
});

it.only('not managed -> sharding -> sharding error', async function () {
it('not managed -> sharding -> sharding error', async function () {
let mockFailure = false;
// initial state === unsharded
const store = createStore({
Expand Down Expand Up @@ -445,15 +445,61 @@ describe('GlobalWritesStore Store', function () {
});
});

it('sharding error', async function () {
it('sharding error -> cancelling request -> not managed', async function () {
// initial state === sharding error
let mockManagedNamespace = true;
let mockShardingError = true;
clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
});
const store = createStore({
isNamespaceManaged: Sinon.fake(() => mockManagedNamespace),
hasShardingError: Sinon.fake(() => mockShardingError),
});
await waitFor(() => {
expect(store.getState().status).to.equal('SHARDING_ERROR');
expect(store.getState().managedNamespace).to.equal(managedNamespace);
});

// user triggers a cancellation
const promise = store.dispatch(cancelSharding());
mockManagedNamespace = false;
mockShardingError = false;
await promise;
expect(store.getState().status).to.equal('UNSHARDED');
expect(confirmationStub).to.have.been.called;
});

it('sharding error -> submitting form -> sharding -> sharded', async function () {
// initial state === sharding error=
let mockShardingError = true;
let mockShardKey = false;
clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
});
const store = createStore({
isNamespaceManaged: () => true,
hasShardingError: () => true,
hasShardingError: Sinon.fake(() => mockShardingError),
hasShardKey: Sinon.fake(() => mockShardKey),
});
await waitFor(() => {
expect(store.getState().status).to.equal('SHARDING_ERROR');
expect(store.getState().managedNamespace).to.equal(managedNamespace);
});

// user submits the form
const promise = store.dispatch(createShardKey(shardKeyData));
mockShardingError = false;
expect(store.getState().status).to.equal('SUBMITTING_FOR_SHARDING_ERROR');
await promise;
expect(store.getState().status).to.equal('SHARDING');

// the key is created
mockShardKey = true;
clock.tick(POLLING_INTERVAL);
await waitFor(() => {
expect(store.getState().status).to.equal('SHARD_KEY_CORRECT');
});
});

it('sends correct data to the server when creating a shard key', async function () {
Expand Down
3 changes: 2 additions & 1 deletion packages/compass-global-writes/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
GlobalWritesActionTypes.CancellingShardingFinished
) &&
(state.status === ShardingStatuses.CANCELLING_SHARDING ||
state.status === ShardingStatuses.SHARDING_ERROR)
state.status === ShardingStatuses.SHARDING_ERROR ||
state.status === ShardingStatuses.CANCELLING_SHARDING_ERROR)
// the error might come before the cancel request was processed
) {
return {
Expand Down

0 comments on commit 966e1e0

Please sign in to comment.