Skip to content

Commit 6f65be6

Browse files
committed
..
1 parent 6300679 commit 6f65be6

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed
Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
11
import React from 'react';
22
import { expect } from 'chai';
3-
import { screen } from '@mongodb-js/testing-library-compass';
3+
import { screen, userEvent } from '@mongodb-js/testing-library-compass';
44
import { ShardingState } from './sharding';
55
import { renderWithStore } from '../../../tests/create-store';
6+
import Sinon from 'sinon';
67

78
function renderWithProps(
89
props?: Partial<React.ComponentProps<typeof ShardingState>>
910
) {
10-
return renderWithStore(<ShardingState {...props} />);
11+
return renderWithStore(
12+
<ShardingState
13+
onCancelSharding={() => {}}
14+
isCancellingSharding={false}
15+
{...props}
16+
/>
17+
);
1118
}
1219

1320
describe('Sharding', function () {
1421
it('renders the info banner', async function () {
1522
await renderWithProps();
1623
expect(screen.getByRole('alert')).to.exist;
1724
});
25+
26+
it('sharding request can be cancelled', async function () {
27+
const onCancelSharding = Sinon.spy();
28+
await renderWithProps({
29+
onCancelSharding,
30+
});
31+
const btn = screen.getByRole('button', { name: 'Cancel Request' });
32+
expect(btn).to.be.visible;
33+
34+
userEvent.click(btn);
35+
36+
expect(onCancelSharding).to.have.been.calledOnce;
37+
});
38+
39+
it('when cancelling is in progress, it cannot be triggered again', async function () {
40+
const onCancelSharding = Sinon.spy();
41+
await renderWithProps({
42+
isCancellingSharding: true,
43+
onCancelSharding,
44+
});
45+
const btn = screen.getByTestId('cancel-sharding-btn');
46+
expect(btn.getAttribute('aria-disabled')).to.equal('true');
47+
48+
userEvent.click(btn);
49+
50+
expect(onCancelSharding).not.to.have.been.calledOnce;
51+
});
1852
});

packages/compass-global-writes/src/components/states/sharding.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
css,
88
Link,
99
spacing,
10-
SpinLoader,
1110
} from '@mongodb-js/compass-components';
1211
import { connect } from 'react-redux';
1312
import {
@@ -50,13 +49,9 @@ export function ShardingState({
5049
>
5150
You can read more about Global Writes in our documentation.
5251
<Button
52+
data-testid="cancel-sharding-btn"
5353
onClick={onCancelSharding}
54-
disabled={isCancellingSharding}
55-
leftGlyph={
56-
isCancellingSharding ? (
57-
<SpinLoader title="Cancelling Request" />
58-
) : undefined
59-
}
54+
isLoading={isCancellingSharding}
6055
>
6156
Cancel Request
6257
</Button>
@@ -67,7 +62,7 @@ export function ShardingState({
6762

6863
export default connect(
6964
(state: RootState) => ({
70-
isCancellingSharding: state.status === ShardingStatuses.CANCEL_SHARDING,
65+
isCancellingSharding: state.status === ShardingStatuses.CANCELLING_SHARDING,
7166
}),
7267
{
7368
onCancelSharding: cancelSharding,

packages/compass-global-writes/src/services/atlas-global-writes-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export type ClusterDetailsApiResponse = {
4242
replicationSpecList: ReplicationItem[];
4343
};
4444

45-
type AutomationAgentProcess = {
45+
export type AutomationAgentProcess = {
4646
statusType: string;
4747
workingOnShort: string;
4848
errorText: string;

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum GlobalWritesActionTypes {
3737
CancellingShardingErrored = 'global-writes/CancellingShardingErrored',
3838

3939
NextPollingTimeoutSet = 'global-writes/NextPollingTimeoutSet',
40+
NextPollingTimeoutCleared = 'global-writes/NextPollingTimeoutCleared',
4041

4142
UnmanagingNamespaceStarted = 'global-writes/UnmanagingNamespaceStarted',
4243
UnmanagingNamespaceFinished = 'global-writes/UnmanagingNamespaceFinished',
@@ -94,6 +95,10 @@ type NextPollingTimeoutSetAction = {
9495
timeout: NodeJS.Timeout;
9596
};
9697

98+
type NextPollingTimeoutClearedAction = {
99+
type: GlobalWritesActionTypes.NextPollingTimeoutCleared;
100+
};
101+
97102
type UnmanagingNamespaceStartedAction = {
98103
type: GlobalWritesActionTypes.UnmanagingNamespaceStarted;
99104
};
@@ -263,12 +268,15 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
263268
(state.status === ShardingStatuses.NOT_READY ||
264269
state.status === ShardingStatuses.SHARDING)
265270
) {
271+
if (state.pollingTimeout) {
272+
throw new Error('Polling was not stopped');
273+
}
266274
return {
267275
...state,
268276
status: ShardingStatuses.SHARDING_ERROR,
269277
shardKey: undefined,
270278
shardingError: action.error,
271-
pollingTimeout: undefined,
279+
pollingTimeout: state.pollingTimeout,
272280
};
273281
}
274282

@@ -280,12 +288,15 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
280288
(state.status === ShardingStatuses.NOT_READY ||
281289
state.status === ShardingStatuses.SHARDING)
282290
) {
291+
if (state.pollingTimeout) {
292+
throw new Error('Polling was not stopped');
293+
}
283294
return {
284295
...state,
285296
status: getStatusFromShardKey(action.shardKey, state.managedNamespace),
286297
shardKey: action.shardKey,
287298
shardingError: undefined,
288-
pollingTimeout: undefined,
299+
pollingTimeout: state.pollingTimeout,
289300
};
290301
}
291302

@@ -341,17 +352,33 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
341352
};
342353
}
343354

355+
if (
356+
isAction<NextPollingTimeoutClearedAction>(
357+
action,
358+
GlobalWritesActionTypes.NextPollingTimeoutCleared
359+
) &&
360+
state.status === ShardingStatuses.SHARDING
361+
) {
362+
return {
363+
...state,
364+
pollingTimeout: undefined,
365+
};
366+
}
367+
344368
if (
345369
isAction<CancellingShardingStartedAction>(
346370
action,
347371
GlobalWritesActionTypes.CancellingShardingStarted
348372
) &&
349373
state.status === ShardingStatuses.SHARDING
350374
) {
375+
if (state.pollingTimeout) {
376+
throw new Error('Polling was not stopped');
377+
}
351378
return {
352379
...state,
353380
status: ShardingStatuses.CANCELLING_SHARDING,
354-
pollingTimeout: undefined,
381+
pollingTimeout: state.pollingTimeout,
355382
};
356383
}
357384

@@ -610,6 +637,7 @@ const startPollingForShardKey = (): GlobalWritesThunkAction<
610637
NextPollingTimeoutSetAction
611638
> => {
612639
return (dispatch) => {
640+
console.log('START POLLING');
613641
const timeout = setTimeout(() => {
614642
void dispatch(fetchNamespaceShardKey());
615643
}, POLLING_INTERVAL);
@@ -635,6 +663,7 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
635663
getState,
636664
{ atlasGlobalWritesService, logger, connectionInfoRef }
637665
) => {
666+
console.log('FETCHING KEY');
638667
const { namespace, status } = getState();
639668

640669
try {

0 commit comments

Comments
 (0)