Skip to content

Commit 3015e57

Browse files
authored
fix(compass-schema): fix geoLayers callbacks COMPASS-9540 (#7099)
1 parent 75c1c9b commit 3015e57

File tree

2 files changed

+81
-11
lines changed

2 files changed

+81
-11
lines changed

packages/compass-schema/src/stores/schema-analysis-reducer.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,16 @@ const getInitialState = (): SchemaAnalysisState => ({
153153

154154
export const geoLayerAdded = (
155155
field: string,
156-
layer: Layer
157-
): SchemaThunkAction<ReturnType<typeof generateGeoQuery>> => {
156+
layer: Layer,
157+
onAdded: (geoQuery: ReturnType<typeof generateGeoQuery>) => void
158+
): SchemaThunkAction<void> => {
158159
return (dispatch, getState, { geoLayersRef }) => {
159160
geoLayersRef.current = addLayer(
160161
field,
161162
layer as Circle | Polygon,
162163
geoLayersRef.current
163164
);
164-
return generateGeoQuery(geoLayersRef.current);
165+
onAdded(generateGeoQuery(geoLayersRef.current));
165166
};
166167
};
167168

@@ -173,24 +174,30 @@ export const analysisErrorDismissed =
173174

174175
export const geoLayersEdited = (
175176
field: string,
176-
layers: LayerGroup
177-
): SchemaThunkAction<ReturnType<typeof generateGeoQuery>> => {
177+
layers: LayerGroup,
178+
onEdited: (geoQuery: ReturnType<typeof generateGeoQuery>) => void
179+
): SchemaThunkAction<void> => {
178180
return (dispatch, getState, { geoLayersRef }) => {
179181
layers.eachLayer((layer) => {
180-
dispatch(geoLayerAdded(field, layer));
182+
dispatch(
183+
geoLayerAdded(field, layer, () => {
184+
// noop, we will call `onEdited` when we're done with updates
185+
})
186+
);
181187
});
182-
return generateGeoQuery(geoLayersRef.current);
188+
onEdited(generateGeoQuery(geoLayersRef.current));
183189
};
184190
};
185191

186192
export const geoLayersDeleted = (
187-
layers: LayerGroup
188-
): SchemaThunkAction<ReturnType<typeof generateGeoQuery>> => {
193+
layers: LayerGroup,
194+
onDeleted: (geoQuery: ReturnType<typeof generateGeoQuery>) => void
195+
): SchemaThunkAction<void> => {
189196
return (dispatch, getState, { geoLayersRef }) => {
190197
layers.eachLayer((layer) => {
191198
delete geoLayersRef.current[(layer as any)._leaflet_id];
192199
});
193-
return generateGeoQuery(geoLayersRef.current);
200+
onDeleted(generateGeoQuery(geoLayersRef.current));
194201
};
195202
};
196203

packages/compass-schema/src/stores/store.spec.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
1111
import { createNoopLogger } from '@mongodb-js/compass-logging/provider';
1212
import type { FieldStoreService } from '@mongodb-js/compass-field-store';
1313
import { createNoopTrack } from '@mongodb-js/compass-telemetry/provider';
14-
import { startAnalysis, stopAnalysis } from './schema-analysis-reducer';
14+
import {
15+
geoLayerAdded,
16+
geoLayersDeleted,
17+
geoLayersEdited,
18+
startAnalysis,
19+
stopAnalysis,
20+
} from './schema-analysis-reducer';
1521
import Sinon from 'sinon';
1622
import {
1723
changeExportSchemaFormat,
1824
openExportSchema,
1925
} from './schema-export-reducer';
26+
import { Circle, LayerGroup, Polygon } from 'leaflet';
2027

2128
const dummyLogger = createNoopLogger('TEST');
2229
const dummyTrack = createNoopTrack();
@@ -154,6 +161,62 @@ describe('Schema Store', function () {
154161
expect(store.getState().schemaAnalysis.analysisState).to.equal('initial');
155162
});
156163

164+
describe('geoLayers', function () {
165+
it('geoLayerAdded, geoLayersEdited, geoLayersDeleted: calls the onChange callback', function () {
166+
const layer = new Circle([1, 2], {
167+
radius: 1000,
168+
});
169+
const onChangeSpy = sandbox.spy();
170+
store.dispatch(geoLayerAdded('coordinates', layer, onChangeSpy));
171+
expect(onChangeSpy).to.have.been.calledOnceWith({
172+
coordinates: {
173+
$geoWithin: {
174+
$centerSphere: Sinon.match.array,
175+
},
176+
},
177+
});
178+
});
179+
180+
it('geoLayersEdited: calls the onChange callback', function () {
181+
const layersGroup = new LayerGroup();
182+
layersGroup.addLayer(
183+
new Polygon([
184+
[1, 2],
185+
[3, 4],
186+
[5, 6],
187+
])
188+
);
189+
const onChangeSpy = sandbox.spy();
190+
store.dispatch(
191+
geoLayersEdited('coordinates', layersGroup, onChangeSpy)
192+
);
193+
expect(onChangeSpy).to.have.been.calledOnceWith({
194+
coordinates: {
195+
$geoWithin: {
196+
$geometry: {
197+
type: 'Polygon',
198+
coordinates: Sinon.match.array,
199+
},
200+
},
201+
},
202+
});
203+
});
204+
205+
it('geoLayersDeleted: calls the onChange callback', function () {
206+
const layersGroup = new LayerGroup();
207+
layersGroup.addLayer(
208+
new Polygon([
209+
[1, 2],
210+
[3, 4],
211+
[5, 6],
212+
])
213+
);
214+
const onChangeSpy = sandbox.spy();
215+
store.dispatch(geoLayersDeleted(layersGroup, onChangeSpy));
216+
expect(onChangeSpy).to.have.been.calledOnceWith({ $or: [] });
217+
});
218+
});
219+
157220
describe('schema export', function () {
158221
describe('with an analyzed schema', function () {
159222
beforeEach(async function () {

0 commit comments

Comments
 (0)