Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zeroline): add read-only subfuction-editor #706

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
13 changes: 12 additions & 1 deletion src/zeroline/function-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from 'lit-element';

import '../action-pane.js';
import './subfunction-editor.js';
import { getChildElementsByTagName } from '../foundation.js';

/** Pane rendering `Function` element with its children */
@customElement('function-editor')
Expand All @@ -24,12 +26,21 @@ export class FunctionEditor extends LitElement {
return `${name}${desc ? ` - ${desc}` : ''}${type ? ` (${type})` : ''}`;
}

renderSubFunctions(): TemplateResult {
const subfunctions = getChildElementsByTagName(this.element, 'SubFunction');
return html` ${subfunctions.map(
subFunction =>
html`<subfunction-editor .element=${subFunction}></subfunction-editor>`
)}`;
}

render(): TemplateResult {
return html`<action-pane
label="${this.header}"
icon="functions"
secondary
highlighted
></action-pane>`;
>${this.renderSubFunctions()}</action-pane
>`;
}
}
42 changes: 42 additions & 0 deletions src/zeroline/subfunction-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
html,
LitElement,
TemplateResult,
property,
customElement,
state,
} from 'lit-element';

import '../action-pane.js';
import './subfunction-editor.js';
import { getChildElementsByTagName } from '../foundation.js';

/** Pane rendering `SubFunction` element with its children */
@customElement('subfunction-editor')
export class SubFunctionEditor extends LitElement {
/** The edited `SubFunction` element */
@property({ attribute: false })
element!: Element;
@state()
get header(): string {
const name = this.element.getAttribute('name');
const desc = this.element.getAttribute('desc');
const type = this.element.getAttribute('type');

return `${name}${desc ? ` - ${desc}` : ''}${type ? ` (${type})` : ''}`;
}

renderSubFunctions(): TemplateResult {
const subfunctions = getChildElementsByTagName(this.element, 'SubFunction');
return html` ${subfunctions.map(
subFunction =>
html`<subfunction-editor .element=${subFunction}></subfunction-editor>`
)}`;
}

render(): TemplateResult {
return html`<action-pane label="${this.header}" icon="functions" secondary
>${this.renderSubFunctions()}</action-pane
>`;
}
}
10 changes: 7 additions & 3 deletions test/testfiles/zeroline/functions.scd
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
</History>
</Header>
<Substation name="AA1" desc="Substation">
<Function name="myFunc" desc="myDesc" type="myFuncType"/>
<Function name="myFunc" desc="myDesc" type="myFuncType">
<SubFunction name="mySubFunc"/>
</Function>
<VoltageLevel name="E1" desc="Voltage Level" nomFreq="50.0" numPhases="3">
<Voltage unit="V" multiplier="k">110.0</Voltage>
<Function name="voltLvName">
<SubFunction name="mySubFunc">
<SubFunction name="mySubSubFunction"/>
<SubFunction name="mySubFunc" desc="some string" type="some type">
<SubFunction name="mySubSubFunction">
<SubFunction name="mySubSubSubFunction"/>
</SubFunction>
</SubFunction>
</Function>
<Bay name="COUPLING_BAY" desc="Bay">
Expand Down
31 changes: 31 additions & 0 deletions test/unit/zeroline/__snapshots__/function-editor.test.snap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* @web/test-runner snapshot v1 */
export const snapshots = {};

snapshots["web component rendering Function element with complete attribute set and existing children looks like the latest snapshot"] =
`<action-pane
highlighted=""
icon="functions"
label="myFunc - myDesc (myFuncType)"
secondary=""
tabindex="0"
>
<subfunction-editor>
</subfunction-editor>
</action-pane>
`;
/* end snapshot web component rendering Function element with complete attribute set and existing children looks like the latest snapshot */

snapshots["web component rendering Function element with missing desc and type attribute looks like the latest snapshot"] =
`<action-pane
highlighted=""
icon="functions"
label="voltLvName"
secondary=""
tabindex="0"
>
<subfunction-editor>
</subfunction-editor>
</action-pane>
`;
/* end snapshot web component rendering Function element with missing desc and type attribute looks like the latest snapshot */

27 changes: 27 additions & 0 deletions test/unit/zeroline/__snapshots__/subfunction-editor.test.snap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* @web/test-runner snapshot v1 */
export const snapshots = {};

snapshots["web component rendering SubFunction element with complete attribute set and existing children looks like the latest snapshot"] =
`<action-pane
icon="functions"
label="mySubFunc - some string (some type)"
secondary=""
tabindex="0"
>
<subfunction-editor>
</subfunction-editor>
</action-pane>
`;
/* end snapshot web component rendering SubFunction element with complete attribute set and existing children looks like the latest snapshot */

snapshots["web component rendering SubFunction element with missing desc and type attribute looks like the latest snapshot"] =
`<action-pane
icon="functions"
label="mySubFunc"
secondary=""
tabindex="0"
>
</action-pane>
`;
/* end snapshot web component rendering SubFunction element with missing desc and type attribute looks like the latest snapshot */

47 changes: 47 additions & 0 deletions test/unit/zeroline/function-editor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { fixture, html, expect } from '@open-wc/testing';

import '../../../src/zeroline/function-editor.js';
import { FunctionEditor } from '../../../src/zeroline/function-editor.js';

describe('web component rendering Function element', () => {
let element: FunctionEditor;
let doc: XMLDocument;

beforeEach(async () => {
doc = await fetch('/test/testfiles/zeroline/functions.scd')
.then(response => response.text())
.then(str => new DOMParser().parseFromString(str, 'application/xml'));
});

describe('with complete attribute set and existing children', () => {
beforeEach(async () => {
element = <FunctionEditor>(
await fixture(
html`<function-editor
.element=${doc.querySelector('Function')}
></function-editor>`
)
);
});

it('looks like the latest snapshot', async () => {
await expect(element).shadowDom.to.equalSnapshot();
});
});

describe('with missing desc and type attribute', () => {
beforeEach(async () => {
element = <FunctionEditor>(
await fixture(
html`<function-editor
.element=${doc.querySelector('VoltageLevel Function')}
></function-editor>`
)
);
});

it('looks like the latest snapshot', async () => {
await expect(element).shadowDom.to.equalSnapshot();
});
});
});
47 changes: 47 additions & 0 deletions test/unit/zeroline/subfunction-editor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { fixture, html, expect } from '@open-wc/testing';

import '../../../src/zeroline/subfunction-editor.js';
import { SubFunctionEditor } from '../../../src/zeroline/subfunction-editor.js';

describe('web component rendering SubFunction element', () => {
let element: SubFunctionEditor;
let doc: XMLDocument;

beforeEach(async () => {
doc = await fetch('/test/testfiles/zeroline/functions.scd')
.then(response => response.text())
.then(str => new DOMParser().parseFromString(str, 'application/xml'));
});

describe('with complete attribute set and existing children', () => {
beforeEach(async () => {
element = <SubFunctionEditor>(
await fixture(
html`<subfunction-editor
.element=${doc.querySelector('VoltageLevel SubFunction')}
></subfunction-editor>`
)
);
});

it('looks like the latest snapshot', async () => {
await expect(element).shadowDom.to.equalSnapshot();
});
});

describe('with missing desc and type attribute', () => {
beforeEach(async () => {
element = <SubFunctionEditor>(
await fixture(
html`<subfunction-editor
.element=${doc.querySelector('SubFunction')}
></subfunction-editor>`
)
);
});

it('looks like the latest snapshot', async () => {
await expect(element).shadowDom.to.equalSnapshot();
});
});
});