From 5b4c42b35e9e040023e1cda703d59809dfd71ac8 Mon Sep 17 00:00:00 2001 From: Jakob Vogelsang Date: Thu, 5 May 2022 14:27:47 +0200 Subject: [PATCH] feat(editors/substation): add read-only eq-sub-function-editor --- src/editors/substation/eq-function-editor.ts | 20 +++++++- .../substation/eq-sub-function-editor.ts | 46 +++++++++++++++++++ test/testfiles/zeroline/functions.scd | 4 +- .../eq-function-editor.test.snap.js | 4 ++ .../eq-sub-function-editor.test.snap.js | 27 +++++++++++ .../substation/eq-sub-function-editor.test.ts | 45 ++++++++++++++++++ 6 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 src/editors/substation/eq-sub-function-editor.ts create mode 100644 test/unit/editors/substation/__snapshots__/eq-sub-function-editor.test.snap.js create mode 100644 test/unit/editors/substation/eq-sub-function-editor.test.ts diff --git a/src/editors/substation/eq-function-editor.ts b/src/editors/substation/eq-function-editor.ts index f3c26a0177..9a6c374da2 100644 --- a/src/editors/substation/eq-function-editor.ts +++ b/src/editors/substation/eq-function-editor.ts @@ -8,8 +8,10 @@ import { } from 'lit-element'; import '../../action-pane.js'; +import './eq-sub-function-editor.js'; +import { getChildElementsByTagName } from '../../foundation.js'; -/** Pane rendering `Function` element with its children */ +/** Pane rendering `EqFunction` element with its children */ @customElement('eq-function-editor') export class EqFunctionEditor extends LitElement { /** The edited `EqFunction` element */ @@ -24,12 +26,26 @@ export class EqFunctionEditor extends LitElement { return `${name}${desc ? ` - ${desc}` : ''}${type ? ` (${type})` : ''}`; } + renderEqSubFunctions(): TemplateResult { + const eqSubFunctions = getChildElementsByTagName( + this.element, + 'EqSubFunction' + ); + return html` ${eqSubFunctions.map( + eqSubFunction => + html`` + )}`; + } + render(): TemplateResult { return html``; + >${this.renderEqSubFunctions()}`; } } diff --git a/src/editors/substation/eq-sub-function-editor.ts b/src/editors/substation/eq-sub-function-editor.ts new file mode 100644 index 0000000000..552734813e --- /dev/null +++ b/src/editors/substation/eq-sub-function-editor.ts @@ -0,0 +1,46 @@ +import { + html, + LitElement, + TemplateResult, + property, + customElement, + state, +} from 'lit-element'; + +import '../../action-pane.js'; +import { getChildElementsByTagName } from '../../foundation.js'; + +/** Pane rendering `EqSubFunction` element with its children */ +@customElement('eq-sub-function-editor') +export class EqSubFunctionEditor extends LitElement { + /** The edited `EqSubFunction` 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})` : ''}`; + } + + renderEqSubFunctions(): TemplateResult { + const eqSubFunctions = getChildElementsByTagName( + this.element, + 'EqSubFunction' + ); + return html` ${eqSubFunctions.map( + eqSubFunction => + html`` + )}`; + } + + render(): TemplateResult { + return html`${this.renderEqSubFunctions()}`; + } +} diff --git a/test/testfiles/zeroline/functions.scd b/test/testfiles/zeroline/functions.scd index 6beb81fb57..5e7054da2b 100644 --- a/test/testfiles/zeroline/functions.scd +++ b/test/testfiles/zeroline/functions.scd @@ -35,7 +35,9 @@ - + + + diff --git a/test/unit/editors/substation/__snapshots__/eq-function-editor.test.snap.js b/test/unit/editors/substation/__snapshots__/eq-function-editor.test.snap.js index 674815ab52..da906462c9 100644 --- a/test/unit/editors/substation/__snapshots__/eq-function-editor.test.snap.js +++ b/test/unit/editors/substation/__snapshots__/eq-function-editor.test.snap.js @@ -9,6 +9,8 @@ snapshots["web component rendering EqFunction element with complete attribute se secondary="" tabindex="0" > + + `; /* end snapshot web component rendering EqFunction element with complete attribute set and existing children looks like the latest snapshot */ @@ -21,6 +23,8 @@ snapshots["web component rendering EqFunction element with missing desc and type secondary="" tabindex="0" > + + `; /* end snapshot web component rendering EqFunction element with missing desc and type attribute looks like the latest snapshot */ diff --git a/test/unit/editors/substation/__snapshots__/eq-sub-function-editor.test.snap.js b/test/unit/editors/substation/__snapshots__/eq-sub-function-editor.test.snap.js new file mode 100644 index 0000000000..40b9949528 --- /dev/null +++ b/test/unit/editors/substation/__snapshots__/eq-sub-function-editor.test.snap.js @@ -0,0 +1,27 @@ +/* @web/test-runner snapshot v1 */ +export const snapshots = {}; + +snapshots["web component rendering EqSubFunction element with complete attribute set and existing children looks like the latest snapshot"] = +` + +`; +/* end snapshot web component rendering EqSubFunction element with complete attribute set and existing children looks like the latest snapshot */ + +snapshots["web component rendering EqSubFunction element with missing desc and type attribute looks like the latest snapshot"] = +` + + + +`; +/* end snapshot web component rendering EqSubFunction element with missing desc and type attribute looks like the latest snapshot */ + diff --git a/test/unit/editors/substation/eq-sub-function-editor.test.ts b/test/unit/editors/substation/eq-sub-function-editor.test.ts new file mode 100644 index 0000000000..1f52d40589 --- /dev/null +++ b/test/unit/editors/substation/eq-sub-function-editor.test.ts @@ -0,0 +1,45 @@ +import { fixture, html, expect } from '@open-wc/testing'; + +import '../../../../src/editors/substation/eq-sub-function-editor.js'; +import { EqSubFunctionEditor } from '../../../../src/editors/substation/eq-sub-function-editor.js'; + +describe('web component rendering EqSubFunction element', () => { + let element: EqSubFunctionEditor; + 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 = ( + await fixture( + html` EqSubFunction')} + >` + ) + ); + }); + + it('looks like the latest snapshot', async () => + await expect(element).shadowDom.to.equalSnapshot()); + }); + + describe('with missing desc and type attribute', () => { + beforeEach(async () => { + element = ( + await fixture( + html`` + ) + ); + }); + + it('looks like the latest snapshot', async () => + await expect(element).shadowDom.to.equalSnapshot()); + }); +});