Skip to content

Commit 1ab906a

Browse files
committed
add tests
1 parent edaaadc commit 1ab906a

File tree

2 files changed

+109
-11
lines changed

2 files changed

+109
-11
lines changed

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,29 @@ function createStore({
7272
hasShardingError = () => false,
7373
hasShardKey = () => false,
7474
failsOnShardingRequest = () => false,
75+
failsToFetchClusterDetails = () => false,
76+
failsToFetchDeploymentStatus = () => false,
77+
failsToFetchShardKey = () => false,
7578
authenticatedFetchStub,
7679
}:
7780
| {
7881
isNamespaceManaged?: () => boolean;
7982
hasShardingError?: () => boolean;
8083
hasShardKey?: () => boolean | AtlasShardKey;
8184
failsOnShardingRequest?: () => boolean;
85+
failsToFetchClusterDetails?: () => boolean;
86+
failsToFetchDeploymentStatus?: () => boolean;
87+
failsToFetchShardKey?: () => boolean;
8288
authenticatedFetchStub?: never;
8389
}
8490
| {
8591
isNamespaceManaged?: never;
8692
hasShardingError?: never;
8793
hasShardKey?: () => boolean | ShardKey;
8894
failsOnShardingRequest?: never;
95+
failsToFetchClusterDetails?: never;
96+
failsToFetchDeploymentStatus?: never;
97+
failsToFetchShardKey?: () => boolean;
8998
authenticatedFetchStub?: () => void;
9099
} = {}): GlobalWritesStore {
91100
const atlasService = {
@@ -95,6 +104,9 @@ function createStore({
95104
}
96105

97106
if (uri.includes('/clusters/')) {
107+
if (failsToFetchClusterDetails()) {
108+
return Promise.reject(new Error('Failed to fetch cluster details'));
109+
}
98110
return createAuthFetchResponse({
99111
...clusterDetails,
100112
geoSharding: {
@@ -105,6 +117,9 @@ function createStore({
105117
}
106118

107119
if (uri.includes('/deploymentStatus/')) {
120+
if (failsToFetchDeploymentStatus()) {
121+
return Promise.reject(new Error('Failed to fetch deployment status'));
122+
}
108123
return createAuthFetchResponse({
109124
automationStatus: {
110125
processes: hasShardingError() ? [failedShardingProcess] : [],
@@ -120,6 +135,10 @@ function createStore({
120135
}),
121136
automationAgentAwait: (_meta: unknown, type: string) => {
122137
if (type === 'getShardKey') {
138+
if (failsToFetchShardKey()) {
139+
return Promise.reject(new Error('Failed to fetch shardKey'));
140+
}
141+
123142
const shardKey = hasShardKey();
124143
return {
125144
response:
@@ -178,6 +197,35 @@ describe('GlobalWritesStore Store', function () {
178197
});
179198

180199
context('scenarios', function () {
200+
context('initial load fail', function () {
201+
it('fails to fetch cluster details', async function () {
202+
const store = createStore({
203+
failsToFetchClusterDetails: () => true,
204+
});
205+
await waitFor(() => {
206+
expect(store.getState().status).to.equal('LOADING_ERROR');
207+
});
208+
});
209+
210+
it('fails to fetch shard key', async function () {
211+
const store = createStore({
212+
failsToFetchShardKey: () => true,
213+
});
214+
await waitFor(() => {
215+
expect(store.getState().status).to.equal('LOADING_ERROR');
216+
});
217+
});
218+
219+
it('fails to fetch deployment status', async function () {
220+
const store = createStore({
221+
failsToFetchDeploymentStatus: () => true,
222+
});
223+
await waitFor(() => {
224+
expect(store.getState().status).to.equal('LOADING_ERROR');
225+
});
226+
});
227+
});
228+
181229
it('not managed -> sharding -> valid shard key', async function () {
182230
let mockShardKey = false;
183231
let mockManagedNamespace = false;
@@ -280,6 +328,52 @@ describe('GlobalWritesStore Store', function () {
280328
});
281329
});
282330

331+
context('pulling fail', function () {
332+
it('sharding -> error (failed to fetch shard key)', async function () {
333+
let mockFailure = false;
334+
// initial state === sharding
335+
clock = sinon.useFakeTimers({
336+
shouldAdvanceTime: true,
337+
});
338+
const store = createStore({
339+
isNamespaceManaged: () => true,
340+
failsToFetchShardKey: Sinon.fake(() => mockFailure),
341+
});
342+
await waitFor(() => {
343+
expect(store.getState().status).to.equal('SHARDING');
344+
});
345+
346+
// sharding ends with a request failure
347+
mockFailure = true;
348+
clock.tick(POLLING_INTERVAL);
349+
await waitFor(() => {
350+
expect(store.getState().status).to.equal('LOADING_ERROR');
351+
});
352+
});
353+
354+
it('sharding -> error (failed to fetch deployment status)', async function () {
355+
let mockFailure = false;
356+
// initial state === sharding
357+
clock = sinon.useFakeTimers({
358+
shouldAdvanceTime: true,
359+
});
360+
const store = createStore({
361+
isNamespaceManaged: () => true,
362+
failsToFetchDeploymentStatus: Sinon.fake(() => mockFailure),
363+
});
364+
await waitFor(() => {
365+
expect(store.getState().status).to.equal('SHARDING');
366+
});
367+
368+
// sharding ends with a request failure
369+
mockFailure = true;
370+
clock.tick(POLLING_INTERVAL);
371+
await waitFor(() => {
372+
expect(store.getState().status).to.equal('LOADING_ERROR');
373+
});
374+
});
375+
});
376+
283377
it('sharding -> cancelling request -> not managed', async function () {
284378
let mockManagedNamespace = true;
285379
confirmationStub.resolves(true);

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -670,11 +670,13 @@ export const fetchClusterShardingData =
670670
'Error fetching cluster sharding data',
671671
(error as Error).message
672672
);
673-
handleLoadingError({
674-
error: error as Error,
675-
id: `global-writes-fetch-shard-info-error-${connectionInfoRef.current.id}-${namespace}`,
676-
description: 'Failed to fetch sharding information',
677-
});
673+
dispatch(
674+
handleLoadingError({
675+
error: error as Error,
676+
id: `global-writes-fetch-shard-info-error-${connectionInfoRef.current.id}-${namespace}`,
677+
description: 'Failed to fetch sharding information',
678+
})
679+
);
678680
}
679681
};
680682

@@ -932,14 +934,16 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
932934
logger.log.error(
933935
logger.mongoLogId(1_001_000_333),
934936
'AtlasFetchError',
935-
'Error fetching shard key',
937+
'Error fetching shard key / deployment status',
936938
(error as Error).message
937939
);
938-
handleLoadingError({
939-
error: error as Error,
940-
id: `global-writes-fetch-shard-key-error-${connectionInfoRef.current.id}-${namespace}`,
941-
description: 'Failed to fetch shard key',
942-
});
940+
dispatch(
941+
handleLoadingError({
942+
error: error as Error,
943+
id: `global-writes-fetch-shard-key-error-${connectionInfoRef.current.id}-${namespace}`,
944+
description: 'Failed to fetch shard key / deployment status',
945+
})
946+
);
943947
}
944948
};
945949
};

0 commit comments

Comments
 (0)