Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stepankuzmin committed Jul 30, 2024
1 parent 969486a commit 0d536f9
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions test/interaction_events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,72 +983,77 @@ test('ensure user interactions fire right events', async (t) => {
});
});

test('ensure API fire right events', async () => {
test('ensure API fire right events', { only: true }, async (t) => {
const container = document.createElement('div');
document.body.appendChild(container);
const map = createMap({ container });
const map = createMap({container});
const fireSpy = spy(map, 'fire');
const afterNextRender = setupAfterNextRender(map);

// Explicitly set silent to false to ensure events are fired
const Draw = new MapboxDraw({ silent: false });

map.addControl(Draw);
await map.on('load');

document.body.removeChild(container);

function flushDrawEvents() {
const drawEvents = [];
for (let i = 0; i < fireSpy.callCount; i++) {
const eventName = fireSpy.getCall(i).args[0];
if (typeof eventName !== 'string' || eventName.indexOf('draw.') !== 0) continue;
// Ignore draw.render events for now
if (eventName === 'draw.render') continue;
// Ignore draw.actionable events for now
if (eventName === 'draw.actionable') continue;
drawEvents.push(eventName);
}
fireSpy.resetHistory();
return drawEvents;
}

Draw.add({
const point = {
type: 'Feature',
id: 'point',
properties: {},
geometry: {
type: 'Point',
coordinates: [10, 10]
}
});

Draw.deleteAll();

await afterNextRender();
};

Draw.add({
const line = {
type: 'Feature',
id: 'line',
properties: {},
geometry: {
type: 'LineString',
coordinates: [[10, 10], [20, 20]]
}
};

await t.test('Draw#add fires draw.create event', async () => {
Draw.add(point);
assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.create');
assert.deepStrictEqual(fireSpy.lastCall.lastArg, {features: [point]});

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

await afterNextRender();
await t.test('Draw#delete fires draw.delete event', async () => {
Draw.delete(point.id);
assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.delete');
assert.deepStrictEqual(fireSpy.lastCall.lastArg, {features: [point]});
});

Draw.changeMode('draw_point');
Draw.changeMode('draw_line_string');
Draw.changeMode('draw_polygon');
Draw.changeMode('simple_select');
await t.test('Draw#deleteAll fires draw.delete event', async () => {
Draw.deleteAll();
assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.delete');
assert.deepStrictEqual(fireSpy.lastCall.lastArg, {features: [line]});
});

await afterNextRender();
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');
assert.deepStrictEqual(fireSpy.lastCall.lastArg, { mode: 'draw_point' }, 'Draw.changeMode triggers draw.modechange event with correct data');

Draw.delete('line');
Draw.changeMode('draw_line_string');
assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.modechange', 'Draw.changeMode triggers draw.modechange event');
assert.deepStrictEqual(fireSpy.lastCall.lastArg, { mode: 'draw_line_string' }, 'Draw.changeMode triggers draw.modechange event with correct data');

await afterNextRender();
Draw.changeMode('draw_polygon');
assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.modechange', 'Draw.changeMode triggers draw.modechange event');
assert.deepStrictEqual(fireSpy.lastCall.lastArg, { mode: 'draw_polygon' }, 'Draw.changeMode triggers draw.modechange event with correct data');

assert.deepEqual(flushDrawEvents(), [
'draw.create', 'draw.delete', 'draw.create', 'draw.modechange', 'draw.modechange', 'draw.modechange', 'draw.modechange', 'draw.delete'
], 'no unexpected draw events');
Draw.changeMode('simple_select');
assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.modechange', 'Draw.changeMode triggers draw.modechange event');
assert.deepStrictEqual(fireSpy.lastCall.lastArg, { mode: 'simple_select' }, 'Draw.changeMode triggers draw.modechange event with correct data');
});
});

0 comments on commit 0d536f9

Please sign in to comment.