Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paula-stacho committed Nov 6, 2024
1 parent edaaadc commit 1ab906a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 11 deletions.
94 changes: 94 additions & 0 deletions packages/compass-global-writes/src/store/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,29 @@ function createStore({
hasShardingError = () => false,
hasShardKey = () => false,
failsOnShardingRequest = () => false,
failsToFetchClusterDetails = () => false,
failsToFetchDeploymentStatus = () => false,
failsToFetchShardKey = () => false,
authenticatedFetchStub,
}:
| {
isNamespaceManaged?: () => boolean;
hasShardingError?: () => boolean;
hasShardKey?: () => boolean | AtlasShardKey;
failsOnShardingRequest?: () => boolean;
failsToFetchClusterDetails?: () => boolean;
failsToFetchDeploymentStatus?: () => boolean;
failsToFetchShardKey?: () => boolean;
authenticatedFetchStub?: never;
}
| {
isNamespaceManaged?: never;
hasShardingError?: never;
hasShardKey?: () => boolean | ShardKey;
failsOnShardingRequest?: never;
failsToFetchClusterDetails?: never;
failsToFetchDeploymentStatus?: never;
failsToFetchShardKey?: () => boolean;
authenticatedFetchStub?: () => void;
} = {}): GlobalWritesStore {
const atlasService = {
Expand All @@ -95,6 +104,9 @@ function createStore({
}

if (uri.includes('/clusters/')) {
if (failsToFetchClusterDetails()) {
return Promise.reject(new Error('Failed to fetch cluster details'));
}
return createAuthFetchResponse({
...clusterDetails,
geoSharding: {
Expand All @@ -105,6 +117,9 @@ function createStore({
}

if (uri.includes('/deploymentStatus/')) {
if (failsToFetchDeploymentStatus()) {
return Promise.reject(new Error('Failed to fetch deployment status'));
}
return createAuthFetchResponse({
automationStatus: {
processes: hasShardingError() ? [failedShardingProcess] : [],
Expand All @@ -120,6 +135,10 @@ function createStore({
}),
automationAgentAwait: (_meta: unknown, type: string) => {
if (type === 'getShardKey') {
if (failsToFetchShardKey()) {
return Promise.reject(new Error('Failed to fetch shardKey'));
}

const shardKey = hasShardKey();
return {
response:
Expand Down Expand Up @@ -178,6 +197,35 @@ describe('GlobalWritesStore Store', function () {
});

context('scenarios', function () {
context('initial load fail', function () {
it('fails to fetch cluster details', async function () {
const store = createStore({
failsToFetchClusterDetails: () => true,
});
await waitFor(() => {
expect(store.getState().status).to.equal('LOADING_ERROR');
});
});

it('fails to fetch shard key', async function () {
const store = createStore({
failsToFetchShardKey: () => true,
});
await waitFor(() => {
expect(store.getState().status).to.equal('LOADING_ERROR');
});
});

it('fails to fetch deployment status', async function () {
const store = createStore({
failsToFetchDeploymentStatus: () => true,
});
await waitFor(() => {
expect(store.getState().status).to.equal('LOADING_ERROR');
});
});
});

it('not managed -> sharding -> valid shard key', async function () {
let mockShardKey = false;
let mockManagedNamespace = false;
Expand Down Expand Up @@ -280,6 +328,52 @@ describe('GlobalWritesStore Store', function () {
});
});

context('pulling fail', function () {
it('sharding -> error (failed to fetch shard key)', async function () {
let mockFailure = false;
// initial state === sharding
clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
});
const store = createStore({
isNamespaceManaged: () => true,
failsToFetchShardKey: Sinon.fake(() => mockFailure),
});
await waitFor(() => {
expect(store.getState().status).to.equal('SHARDING');
});

// sharding ends with a request failure
mockFailure = true;
clock.tick(POLLING_INTERVAL);
await waitFor(() => {
expect(store.getState().status).to.equal('LOADING_ERROR');
});
});

it('sharding -> error (failed to fetch deployment status)', async function () {
let mockFailure = false;
// initial state === sharding
clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
});
const store = createStore({
isNamespaceManaged: () => true,
failsToFetchDeploymentStatus: Sinon.fake(() => mockFailure),
});
await waitFor(() => {
expect(store.getState().status).to.equal('SHARDING');
});

// sharding ends with a request failure
mockFailure = true;
clock.tick(POLLING_INTERVAL);
await waitFor(() => {
expect(store.getState().status).to.equal('LOADING_ERROR');
});
});
});

it('sharding -> cancelling request -> not managed', async function () {
let mockManagedNamespace = true;
confirmationStub.resolves(true);
Expand Down
26 changes: 15 additions & 11 deletions packages/compass-global-writes/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,11 +670,13 @@ export const fetchClusterShardingData =
'Error fetching cluster sharding data',
(error as Error).message
);
handleLoadingError({
error: error as Error,
id: `global-writes-fetch-shard-info-error-${connectionInfoRef.current.id}-${namespace}`,
description: 'Failed to fetch sharding information',
});
dispatch(
handleLoadingError({
error: error as Error,
id: `global-writes-fetch-shard-info-error-${connectionInfoRef.current.id}-${namespace}`,
description: 'Failed to fetch sharding information',
})
);
}
};

Expand Down Expand Up @@ -932,14 +934,16 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
logger.log.error(
logger.mongoLogId(1_001_000_333),
'AtlasFetchError',
'Error fetching shard key',
'Error fetching shard key / deployment status',
(error as Error).message
);
handleLoadingError({
error: error as Error,
id: `global-writes-fetch-shard-key-error-${connectionInfoRef.current.id}-${namespace}`,
description: 'Failed to fetch shard key',
});
dispatch(
handleLoadingError({
error: error as Error,
id: `global-writes-fetch-shard-key-error-${connectionInfoRef.current.id}-${namespace}`,
description: 'Failed to fetch shard key / deployment status',
})
);
}
};
};
Expand Down

0 comments on commit 1ab906a

Please sign in to comment.