Skip to content

Commit

Permalink
Draw#setFeatureProperty fires draw.update event
Browse files Browse the repository at this point in the history
  • Loading branch information
stepankuzmin committed Jul 30, 2024
1 parent 0d536f9 commit 46ece49
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default function(ctx, api) {
};

api.setFeatureProperty = function(featureId, property, value) {
ctx.store.setFeatureProperty(featureId, property, value);
ctx.store.setFeatureProperty(featureId, property, value, { silent });
return api;
};

Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const events = {

export const updateActions = {
MOVE: 'move',
CHANGE_PROPERTIES: 'change_properties',
CHANGE_COORDINATES: 'change_coordinates'
};

Expand Down
11 changes: 9 additions & 2 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Store.prototype.add = function(feature, options = {}) {
this._featureIds.add(feature.id);

if (options.silent != null && options.silent === false) {
this.ctx.map.fire(Constants.events.CREATE, {
this.ctx.events.fire(Constants.events.CREATE, {
features: [this._features[feature.id].toGeoJSON()]
});
}
Expand Down Expand Up @@ -322,9 +322,16 @@ Store.prototype.isSelected = function(featureId) {
* @param {string} property property
* @param {string} property value
*/
Store.prototype.setFeatureProperty = function(featureId, property, value) {
Store.prototype.setFeatureProperty = function(featureId, property, value, options) {
this.get(featureId).setProperty(property, value);
this.featureChanged(featureId);

if (options.silent != null && options.silent === false) {
this.ctx.events.fire(Constants.events.UPDATE, {
action: Constants.updateActions.CHANGE_PROPERTIES,
features: [this.get(featureId).toGeoJSON()]
});
}
};

function refreshSelectedCoordinates(store, options) {
Expand Down
47 changes: 47 additions & 0 deletions test/interaction_events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,10 @@ test('ensure API fire right events', { only: true }, async (t) => {
}
};

t.afterEach(() => {
fireSpy.resetHistory();
});

await t.test('Draw#add fires draw.create event', async () => {
Draw.add(point);
assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.create');
Expand All @@ -1039,6 +1043,49 @@ test('ensure API fire right events', { only: true }, async (t) => {
assert.deepStrictEqual(fireSpy.lastCall.lastArg, {features: [line]});
});

await t.test('Draw#set fires draw.create event', async () => {
const collection = {
type: 'FeatureCollection',
features: [point, line]
};

Draw.set(collection);

assert.strictEqual(fireSpy.callCount, 2, 'fires draw.create event for each feature');

assert.strictEqual(fireSpy.firstCall.firstArg, 'draw.create');
assert.deepStrictEqual(fireSpy.firstCall.lastArg, {features: [point]});

assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.create');
assert.deepStrictEqual(fireSpy.lastCall.lastArg, {features: [line]});
});

await t.test('Draw#set fires draw.delete event', async () => {
const collection = {
type: 'FeatureCollection',
features: [line]
};

Draw.set(collection);

assert.strictEqual(fireSpy.callCount, 1, 'fires draw.delete event for deleted feature');

assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.delete');
assert.deepStrictEqual(fireSpy.lastCall.lastArg, {features: [point]});
});

await t.test('Draw#setFeatureProperty fires draw.update event', () => {
Draw.add(point);

Draw.setFeatureProperty(point.id, 'price', 200);

assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.update');
assert.deepStrictEqual(fireSpy.lastCall.lastArg, {
action: 'change_properties',
features: [{...point, properties: {price: 200}}]
});
});

await t.test('Draw#changeMode fires draw.modechange event', async () => {
Draw.changeMode('draw_point');
assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.modechange', 'Draw.changeMode triggers draw.modechange event');
Expand Down

0 comments on commit 46ece49

Please sign in to comment.