Skip to content

Commit b93dab2

Browse files
ca-dJakobVogelsang
andauthored
fix(editing): don't validate after no-op action (openscd#889)
* fix(editing): don't validate after no-op action After committing an empty complex action (e.g. clicking "merge" in a merge wizard without any additions or deletions selected) the `Editing` mixin needlessly dispatches a ValidateEvent causing lengthy validation of an already validated document. This fixes that issue. * test(Editing): add regression tests Co-authored-by: Jakob Vogelsang <[email protected]>
1 parent 027462c commit b93dab2

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/Editing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ export function Editing<TBase extends LitElementConstructor>(Base: TBase) {
419419
action: event.detail.action,
420420
})
421421
);
422-
}
422+
} else return;
423423

424424
if (!this.doc) return;
425425

test/unit/Editing.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { html, fixture, expect } from '@open-wc/testing';
2+
import { SinonSpy, spy } from 'sinon';
23

34
import './mock-editor.js';
45
import { MockEditor } from './mock-editor.js';
@@ -12,6 +13,8 @@ describe('EditingElement', () => {
1213
let element: Element;
1314
let reference: Node | null;
1415

16+
let validateEvent: SinonSpy;
17+
1518
beforeEach(async () => {
1619
doc = await fetch('/test/testfiles/Editing.scd')
1720
.then(response => response.text())
@@ -23,6 +26,9 @@ describe('EditingElement', () => {
2326
parent = elm.doc!.querySelector('VoltageLevel[name="E1"]')!;
2427
element = parent.querySelector('Bay[name="Q01"]')!;
2528
reference = element.nextSibling;
29+
30+
validateEvent = spy();
31+
window.addEventListener('validate', validateEvent);
2632
});
2733

2834
it('creates an element on receiving a Create Action', () => {
@@ -390,4 +396,57 @@ describe('EditingElement', () => {
390396
expect(elm.doc!.querySelector('VoltageLevel[name="J1"] > newBay')).to.not.be
391397
.null;
392398
});
399+
400+
it('triggers a validation event on receiving a ComplexAction', async () => {
401+
const child3 = elm.doc!.createElement('newBay');
402+
elm.dispatchEvent(
403+
newActionEvent({
404+
title: 'Test complex action',
405+
actions: [
406+
{
407+
old: { element },
408+
new: { element: child3 },
409+
},
410+
{
411+
old: {
412+
parent,
413+
element: child3,
414+
reference,
415+
},
416+
new: {
417+
parent: elm.doc!.querySelector('VoltageLevel[name="J1"]')!,
418+
reference: null,
419+
},
420+
},
421+
],
422+
})
423+
);
424+
await elm.updateComplete;
425+
426+
expect(validateEvent).to.be.calledOnce;
427+
});
428+
429+
it('does not exchange doc with empty complex action', async () => {
430+
elm.dispatchEvent(
431+
newActionEvent({
432+
title: 'Test complex action',
433+
actions: [],
434+
})
435+
);
436+
await elm.updateComplete;
437+
438+
expect(doc).to.equal(elm.doc);
439+
});
440+
441+
it('does not trigger validation with empty complex action', async () => {
442+
elm.dispatchEvent(
443+
newActionEvent({
444+
title: 'Test complex action',
445+
actions: [],
446+
})
447+
);
448+
await elm.updateComplete;
449+
450+
expect(validateEvent).to.not.been.called;
451+
});
393452
});

0 commit comments

Comments
 (0)