Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions packages/compass-schema/src/stores/schema-analysis-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,16 @@ const getInitialState = (): SchemaAnalysisState => ({

export const geoLayerAdded = (
field: string,
layer: Layer
): SchemaThunkAction<ReturnType<typeof generateGeoQuery>> => {
layer: Layer,
onAdded: (geoQuery: ReturnType<typeof generateGeoQuery>) => void
): SchemaThunkAction<void> => {
return (dispatch, getState, { geoLayersRef }) => {
geoLayersRef.current = addLayer(
field,
layer as Circle | Polygon,
geoLayersRef.current
);
return generateGeoQuery(geoLayersRef.current);
onAdded(generateGeoQuery(geoLayersRef.current));
};
};

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

export const geoLayersEdited = (
field: string,
layers: LayerGroup
): SchemaThunkAction<ReturnType<typeof generateGeoQuery>> => {
layers: LayerGroup,
onEdited: (geoQuery: ReturnType<typeof generateGeoQuery>) => void
): SchemaThunkAction<void> => {
return (dispatch, getState, { geoLayersRef }) => {
layers.eachLayer((layer) => {
dispatch(geoLayerAdded(field, layer));
dispatch(
geoLayerAdded(field, layer, () => {
// noop, we will call `onEdited` when we're done with updates
})
);
});
return generateGeoQuery(geoLayersRef.current);
onEdited(generateGeoQuery(geoLayersRef.current));
};
};

export const geoLayersDeleted = (
layers: LayerGroup
): SchemaThunkAction<ReturnType<typeof generateGeoQuery>> => {
layers: LayerGroup,
onDeleted: (geoQuery: ReturnType<typeof generateGeoQuery>) => void
): SchemaThunkAction<void> => {
return (dispatch, getState, { geoLayersRef }) => {
layers.eachLayer((layer) => {
delete geoLayersRef.current[(layer as any)._leaflet_id];
});
return generateGeoQuery(geoLayersRef.current);
onDeleted(generateGeoQuery(geoLayersRef.current));
};
};

Expand Down
65 changes: 64 additions & 1 deletion packages/compass-schema/src/stores/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
import { createNoopLogger } from '@mongodb-js/compass-logging/provider';
import type { FieldStoreService } from '@mongodb-js/compass-field-store';
import { createNoopTrack } from '@mongodb-js/compass-telemetry/provider';
import { startAnalysis, stopAnalysis } from './schema-analysis-reducer';
import {
geoLayerAdded,
geoLayersDeleted,
geoLayersEdited,
startAnalysis,
stopAnalysis,
} from './schema-analysis-reducer';
import Sinon from 'sinon';
import {
changeExportSchemaFormat,
openExportSchema,
} from './schema-export-reducer';
import { Circle, LayerGroup, Polygon } from 'leaflet';

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

describe('geoLayers', function () {
it('geoLayerAdded, geoLayersEdited, geoLayersDeleted: calls the onChange callback', function () {
const layer = new Circle([1, 2], {
radius: 1000,
});
const onChangeSpy = sandbox.spy();
store.dispatch(geoLayerAdded('coordinates', layer, onChangeSpy));
expect(onChangeSpy).to.have.been.calledOnceWith({
coordinates: {
$geoWithin: {
$centerSphere: Sinon.match.array,
},
},
});
});

it('geoLayersEdited: calls the onChange callback', function () {
const layersGroup = new LayerGroup();
layersGroup.addLayer(
new Polygon([
[1, 2],
[3, 4],
[5, 6],
])
);
const onChangeSpy = sandbox.spy();
store.dispatch(
geoLayersEdited('coordinates', layersGroup, onChangeSpy)
);
expect(onChangeSpy).to.have.been.calledOnceWith({
coordinates: {
$geoWithin: {
$geometry: {
type: 'Polygon',
coordinates: Sinon.match.array,
},
},
},
});
});

it('geoLayersDeleted: calls the onChange callback', function () {
const layersGroup = new LayerGroup();
layersGroup.addLayer(
new Polygon([
[1, 2],
[3, 4],
[5, 6],
])
);
const onChangeSpy = sandbox.spy();
store.dispatch(geoLayersDeleted(layersGroup, onChangeSpy));
expect(onChangeSpy).to.have.been.calledOnceWith({ $or: [] });
});
});

describe('schema export', function () {
describe('with an analyzed schema', function () {
beforeEach(async function () {
Expand Down
Loading