Skip to content

Commit 0f661d4

Browse files
Merge pull request openscd#875 from openscd/847_Adding_Substation_Without_Communication_Element_Present_Fails_First_Time
fix: Adding Subnetwork to a configuration without a Communication Ele…
2 parents 9d7916b + c1d2086 commit 0f661d4

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

src/editors/Communication.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,27 @@ export default class CommunicationPlugin extends LitElement {
1818
@property()
1919
doc!: XMLDocument;
2020

21-
private createCommunication(): void {
21+
/**
22+
* Creates the Communication Element and returns the created Element
23+
* @returns {Element} Communication `Element`
24+
*/
25+
private createCommunication(): Element {
26+
const element: Element = createElement(this.doc, 'Communication', {});
2227
this.dispatchEvent(
2328
newActionEvent({
2429
new: {
2530
parent: this.doc.documentElement,
26-
element: createElement(this.doc, 'Communication', {}),
31+
element: element,
2732
},
2833
})
2934
);
35+
return element;
3036
}
3137

3238
/** Opens a [[`WizardDialog`]] for creating a new `SubNetwork` element. */
3339
private openCreateSubNetworkWizard(): void {
34-
const parent = this.doc.querySelector(':root > Communication');
35-
if (!parent) this.createCommunication();
40+
const parent =this.doc.querySelector(':root > Communication') ||
41+
this.createCommunication();
3642

3743
this.dispatchEvent(newWizardEvent(createSubNetworkWizard(parent!)));
3844
}

test/integration/editors/communication/Communication.test.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { html, fixture, expect } from '@open-wc/testing';
22

3-
import '../../../mock-wizard.js';
4-
import { MockWizard } from '../../../mock-wizard.js';
3+
import '../../../mock-wizard-editor.js';
4+
import { MockWizardEditor } from '../../../mock-wizard-editor.js';
55

6-
import Communication from '../../../../src/editors/Communication.js';
7-
import { Editing } from '../../../../src/Editing.js';
8-
import { Wizarding } from '../../../../src/Wizarding.js';
6+
import Communication from '../../../../src/editors/Communication.js';
7+
import { Dialog } from '@material/mwc-dialog';
8+
import { WizardTextField } from '../../../../src/wizard-textfield.js';
99

1010
describe('Communication Plugin', () => {
1111
customElements.define(
1212
'communication-plugin',
13-
Wizarding(Editing(Communication))
13+
Communication
1414
);
1515
let element: Communication;
1616
beforeEach(async () => {
@@ -43,18 +43,23 @@ describe('Communication Plugin', () => {
4343

4444
describe('with a doc loaded missing a communication section', () => {
4545
let doc: XMLDocument;
46-
let parent: MockWizard;
46+
let parent: MockWizardEditor;
4747
let fab: HTMLElement;
48+
let element: Communication;
4849

4950
beforeEach(async () => {
5051
doc = await fetch('/test/testfiles/missingCommunication.scd')
5152
.then(response => response.text())
5253
.then(str => new DOMParser().parseFromString(str, 'application/xml'));
53-
parent = <MockWizard>(
54+
55+
element = await fixture(
56+
html`<communication-plugin .doc="${doc}"></communication-plugin>`
57+
);
58+
59+
parent = <MockWizardEditor>(
5460
await fixture(
55-
html`<mock-wizard
56-
><communication-plugin .doc=${doc}></communication-plugin
57-
></mock-wizard>`
61+
html`<mock-wizard-editor
62+
>${element}/mock-wizard-editor>`
5863
)
5964
);
6065
await element.updateComplete;
@@ -73,5 +78,28 @@ describe('Communication Plugin', () => {
7378
await parent.updateComplete;
7479
expect(parent.wizardUI.dialogs.length).to.equal(1);
7580
});
81+
82+
it('Should create a Communication Element', async () => {
83+
expect(parent.wizardUI.dialogs.length).to.equal(0);
84+
expect(element.doc.querySelector('Communication')).is.null;
85+
86+
await fab.click();
87+
await new Promise(resolve => setTimeout(resolve, 100)); // await animation
88+
await parent.updateComplete;
89+
90+
const dialog: Dialog = parent.wizardUI.dialog!;
91+
expect(dialog).to.not.be.undefined;
92+
93+
const nameInput: WizardTextField = dialog.querySelector<WizardTextField>('wizard-textfield[label="name"]')!;
94+
nameInput.value = 'Test';
95+
await new Promise(resolve => setTimeout(resolve, 100)); // await animation
96+
97+
const saveButton: HTMLElement = dialog.querySelector('mwc-button[slot="primaryAction"]')!;
98+
await saveButton.click();
99+
await new Promise(resolve => setTimeout(resolve, 100)); // await animation
100+
101+
expect(element.doc.querySelector('Communication')).not.is.null;
102+
expect(element.doc.querySelector('Communication > SubNetwork[name="Test"]')).to.exist;
103+
});
76104
});
77105
});

test/integration/editors/communication/__snapshots__/Communication.test.snap.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ snapshots["Communication Plugin without a doc loaded looks like the latest snaps
1313
>
1414
</mwc-fab>
1515
</h1>
16-
<wizard-dialog>
17-
</wizard-dialog>
1816
`;
1917
/* end snapshot Communication Plugin without a doc loaded looks like the latest snapshot */
2018

0 commit comments

Comments
 (0)