Skip to content

Commit 8c43268

Browse files
authored
fix(aggregations): hide add stage in toolbar COMPASS-6373 (#3907)
1 parent c0c5eac commit 8c43268

File tree

4 files changed

+103
-21
lines changed

4 files changed

+103
-21
lines changed

packages/compass-aggregations/src/components/pipeline-toolbar/pipeline-header/pipeline-stages.spec.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ describe('PipelineStages', function () {
3131
stages: [],
3232
showAddNewStage: true,
3333
});
34-
expect(
35-
within(container).findByText(
36-
'Your pipeline is currently empty. To get started select the first stage.'
37-
)
38-
).to.exist;
34+
const content = container.textContent?.trim().replace(/\u00a0/g, ' ');
35+
expect(content).to.equal(
36+
`Your pipeline is currently empty. To get started add the first stage.`
37+
);
3938
});
4039

4140
describe('add stage button', function () {

packages/compass-aggregations/src/components/pipeline-toolbar/pipeline-header/pipeline-stages.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type PipelineStagesProps = {
4646
onEditPipelineClick: (workspace: Workspace) => void;
4747
};
4848

49+
const nbsp = '\u00a0';
50+
4951
export const PipelineStages: React.FunctionComponent<PipelineStagesProps> = ({
5052
isResultsMode,
5153
stages,
@@ -60,8 +62,7 @@ export const PipelineStages: React.FunctionComponent<PipelineStagesProps> = ({
6062
Your pipeline is currently empty.
6163
{showAddNewStage && (
6264
<>
63-
{' '}
64-
To get started select the&nbsp;
65+
{nbsp}To get started add the{nbsp}
6566
<Link
6667
className={addStageStyles}
6768
as="button"
@@ -97,11 +98,12 @@ export const PipelineStages: React.FunctionComponent<PipelineStagesProps> = ({
9798
};
9899

99100
const mapState = (state: RootState) => {
100-
const stages = getPipelineStageOperatorsFromBuilderState(state);
101+
const stages = getPipelineStageOperatorsFromBuilderState(state, false);
101102
const isResultsMode = state.workspace === 'results';
103+
const isStageMode = state.pipelineBuilder.pipelineMode === 'builder-ui';
102104
return {
103-
stages,
104-
showAddNewStage: !isResultsMode && stages.length === 0,
105+
stages: stages.filter(Boolean) as string[],
106+
showAddNewStage: !isResultsMode && isStageMode && stages.length === 0,
105107
isResultsMode,
106108
};
107109
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { expect } from 'chai';
2+
import { applyMiddleware, createStore as createReduxStore } from 'redux';
3+
import type { DataService } from 'mongodb-data-service';
4+
import thunk from 'redux-thunk';
5+
6+
import reducer from '..';
7+
import { getPipelineStageOperatorsFromBuilderState } from './builder-helpers';
8+
import { PipelineBuilder } from './pipeline-builder';
9+
import { addStage, mapBuilderStageToStoreStage } from './stage-editor';
10+
import { changePipelineMode } from './pipeline-mode';
11+
import { PipelineStorage } from '../../utils/pipeline-storage';
12+
13+
function createStore(
14+
pipelineSource = `[{$match: {_id: 1}}, {$limit: 10}]`
15+
) {
16+
const pipelineBuilder = new PipelineBuilder({} as DataService, pipelineSource);
17+
return createReduxStore(
18+
reducer,
19+
{
20+
pipelineBuilder: {
21+
stageEditor: {
22+
stageIds: pipelineBuilder.stages.map(stage => stage.id),
23+
stages: pipelineBuilder.stages.map(mapBuilderStageToStoreStage)
24+
}
25+
}
26+
},
27+
applyMiddleware(
28+
thunk.withExtraArgument({
29+
pipelineBuilder,
30+
pipelineStorage: new PipelineStorage(),
31+
})
32+
)
33+
);
34+
}
35+
36+
describe('builder-helpers', function () {
37+
describe('getPipelineStageOperatorsFromBuilderState', function () {
38+
let store: ReturnType<typeof createStore>;
39+
beforeEach(function() {
40+
store = createStore();
41+
});
42+
describe('in stage editor mode', function() {
43+
it('should return filtered stage names', function () {
44+
store.dispatch(addStage());
45+
expect(
46+
getPipelineStageOperatorsFromBuilderState(store.getState())
47+
).to.deep.equal(['$match', '$limit']);
48+
});
49+
50+
it('should return unfiltered stage names', function () {
51+
store.dispatch(addStage());
52+
expect(
53+
getPipelineStageOperatorsFromBuilderState(store.getState(), false)
54+
).to.deep.equal(['$match', '$limit', null]);
55+
});
56+
})
57+
describe('in text editor mode', function() {
58+
it('should return filtered stage names', function() {
59+
store.dispatch(changePipelineMode('as-text'));
60+
store.dispatch(addStage());
61+
expect(
62+
getPipelineStageOperatorsFromBuilderState(store.getState())
63+
).to.deep.equal(['$match', '$limit']);
64+
});
65+
it('should return unfiltered stage names', function() {
66+
store.dispatch(addStage());
67+
expect(
68+
getPipelineStageOperatorsFromBuilderState(store.getState(), false)
69+
).to.deep.equal(['$match', '$limit', null]);
70+
});
71+
});
72+
});
73+
});

packages/compass-aggregations/src/modules/pipeline-builder/builder-helpers.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,27 @@ export function getPipelineStringFromBuilderState(
5353
}
5454

5555
export function getPipelineStageOperatorsFromBuilderState(
56-
state: RootState
57-
): string[] {
58-
if (state.pipelineBuilder.pipelineMode === 'builder-ui') {
59-
return state.pipelineBuilder.stageEditor.stages
56+
state: RootState,
57+
filterEmptyStageOperators?: true,
58+
): string[];
59+
export function getPipelineStageOperatorsFromBuilderState(
60+
state: RootState,
61+
filterEmptyStageOperators?: false
62+
): (string | null)[];
63+
export function getPipelineStageOperatorsFromBuilderState(
64+
state: RootState,
65+
filterEmptyStageOperators = true
66+
): (string | null)[] | string[] {
67+
const stages = state.pipelineBuilder.pipelineMode === 'builder-ui'
68+
? state.pipelineBuilder.stageEditor.stages
6069
.filter((stage) => !stage.disabled)
6170
.map((stage) => stage.stageOperator)
62-
.filter(Boolean) as string[];
63-
}
64-
return state.pipelineBuilder.textEditor.pipeline.pipeline
65-
.map((stage) => {
66-
return getStageOperator(stage);
67-
})
68-
.filter(Boolean) as string[];
71+
: state.pipelineBuilder.textEditor.pipeline.pipeline
72+
.map((stage) => {
73+
return getStageOperator(stage) ?? null;
74+
});
75+
76+
return filterEmptyStageOperators ? stages.filter(Boolean) : stages;
6977
}
7078

7179
export function getIsPipelineInvalidFromBuilderState(

0 commit comments

Comments
 (0)