diff --git a/packages/query-composer/example/example.tsx b/packages/query-composer/example/example.tsx index 387de7f0..2a6253fc 100644 --- a/packages/query-composer/example/example.tsx +++ b/packages/query-composer/example/example.tsx @@ -59,7 +59,6 @@ const App = () => {
= ({ iconName: 'duplicate', iconColor: 'other', isEnabled: !isExpanded, - onClick: () => - queryModifiers.replaceWithDefinition(stagePath, fieldIndex), + onClick: () => { + const {stageIndex, parts: oldParts} = stagePath; + const parts = oldParts ? [...oldParts] : []; + parts.pop(); + const parentPath = {stageIndex, parts}; + queryModifiers.replaceWithDefinition(parentPath, fieldIndex); + }, }, { kind: 'one_click', diff --git a/packages/query-composer/src/components/QuerySummaryPanel/StateSummaryUI.tsx b/packages/query-composer/src/components/QuerySummaryPanel/StateSummaryUI.tsx index 7c5e787a..c568b2af 100644 --- a/packages/query-composer/src/components/QuerySummaryPanel/StateSummaryUI.tsx +++ b/packages/query-composer/src/components/QuerySummaryPanel/StateSummaryUI.tsx @@ -66,7 +66,7 @@ export const StageSummaryUI: React.FC = ({ }; window.addEventListener('keyup', handle); return () => window.removeEventListener('keyup', handle); - }); + }, [currentFieldOrdering, queryModifiers, selectedFieldIndex, stagePath]); return ( diff --git a/packages/query-composer/tests/core/query.spec.ts b/packages/query-composer/tests/core/query.spec.ts index f96d2c8e..13527785 100644 --- a/packages/query-composer/tests/core/query.spec.ts +++ b/packages/query-composer/tests/core/query.spec.ts @@ -182,6 +182,27 @@ run: names -> { group_by: name state +}`); + }); + + it('preserves order_by', () => { + qb.addOrderBy({stageIndex: 0}, 1); + expect(qb.getQueryStringForNotebook()).toEqual(`\ +run: names -> { + group_by: + name + state + aggregate: population + order_by: state +}`); + qb.reorderFields({stageIndex: 0}, [2, 0, 1]); + expect(qb.getQueryStringForNotebook()).toEqual(`\ +run: names -> { + aggregate: population + group_by: + name + state + order_by: state }`); }); }); @@ -477,6 +498,49 @@ run: names -> { }); }); + describe('expanding stages', () => { + beforeEach(() => { + qb.addField({stageIndex: 0}, 'by_state'); + }); + + it('auto expands when applying new properties', () => { + expect(qb.getQueryStringForNotebook()).toEqual(`\ +run: names -> { + nest: by_state +}`); + qb.addLimit({stageIndex: 0, parts: [{stageIndex: 0, fieldIndex: 0}]}, 13); + expect(qb.getQueryStringForNotebook()).toEqual(`\ +run: names -> { + nest: by_state is { + group_by: state + aggregate: births_per_100k + limit: 13 + } +}`); + }); + + it('explicitly expands a stage', () => { + expect(qb.getQueryStringForNotebook()).toEqual(`\ +run: names -> { + nest: by_state +}`); + qb.replaceWithDefinition( + { + stageIndex: 0, + parts: [], + }, + 0 + ); + expect(qb.getQueryStringForNotebook()).toEqual(`\ +run: names -> { + nest: by_state is { + group_by: state + aggregate: births_per_100k + } +}`); + }); + }); + describe('updateSource', () => { it('updates the source', () => { expect(qb.getSource()?.as).toEqual('names');