From 40c12e1c0ee991e3e2239de5d22046c1a22c205c Mon Sep 17 00:00:00 2001 From: Jakob Vogelsang Date: Wed, 27 Apr 2022 14:15:29 +0200 Subject: [PATCH] feat(zeroline): add read-only subfuction-editor --- src/zeroline/function-editor.ts | 13 ++++- src/zeroline/subfunction-editor.ts | 42 +++++++++++++++++ test/testfiles/zeroline/functions.scd | 10 ++-- .../function-editor.test.snap.js | 31 ++++++++++++ .../subfunction-editor.test.snap.js | 27 +++++++++++ test/unit/zeroline/function-editor.test.ts | 47 +++++++++++++++++++ test/unit/zeroline/subfunction-editor.test.ts | 47 +++++++++++++++++++ 7 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 src/zeroline/subfunction-editor.ts create mode 100644 test/unit/zeroline/__snapshots__/function-editor.test.snap.js create mode 100644 test/unit/zeroline/__snapshots__/subfunction-editor.test.snap.js create mode 100644 test/unit/zeroline/function-editor.test.ts create mode 100644 test/unit/zeroline/subfunction-editor.test.ts diff --git a/src/zeroline/function-editor.ts b/src/zeroline/function-editor.ts index 980f0a6880..2ed81f255b 100644 --- a/src/zeroline/function-editor.ts +++ b/src/zeroline/function-editor.ts @@ -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') @@ -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`` + )}`; + } + render(): TemplateResult { return html``; + >${this.renderSubFunctions()}`; } } diff --git a/src/zeroline/subfunction-editor.ts b/src/zeroline/subfunction-editor.ts new file mode 100644 index 0000000000..6e12ef7605 --- /dev/null +++ b/src/zeroline/subfunction-editor.ts @@ -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`` + )}`; + } + + render(): TemplateResult { + return html`${this.renderSubFunctions()}`; + } +} diff --git a/test/testfiles/zeroline/functions.scd b/test/testfiles/zeroline/functions.scd index 51be8e942d..5e87b5f157 100644 --- a/test/testfiles/zeroline/functions.scd +++ b/test/testfiles/zeroline/functions.scd @@ -7,12 +7,16 @@ - + + + 110.0 - - + + + + diff --git a/test/unit/zeroline/__snapshots__/function-editor.test.snap.js b/test/unit/zeroline/__snapshots__/function-editor.test.snap.js new file mode 100644 index 0000000000..f4131b1c64 --- /dev/null +++ b/test/unit/zeroline/__snapshots__/function-editor.test.snap.js @@ -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"] = +` + + + +`; +/* 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"] = +` + + + +`; +/* end snapshot web component rendering Function element with missing desc and type attribute looks like the latest snapshot */ + diff --git a/test/unit/zeroline/__snapshots__/subfunction-editor.test.snap.js b/test/unit/zeroline/__snapshots__/subfunction-editor.test.snap.js new file mode 100644 index 0000000000..8f5f8d9874 --- /dev/null +++ b/test/unit/zeroline/__snapshots__/subfunction-editor.test.snap.js @@ -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"] = +` + + + +`; +/* 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"] = +` + +`; +/* end snapshot web component rendering SubFunction element with missing desc and type attribute looks like the latest snapshot */ + diff --git a/test/unit/zeroline/function-editor.test.ts b/test/unit/zeroline/function-editor.test.ts new file mode 100644 index 0000000000..9536ff5008 --- /dev/null +++ b/test/unit/zeroline/function-editor.test.ts @@ -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 = ( + await fixture( + html`` + ) + ); + }); + + 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(); + }); + }); +}); diff --git a/test/unit/zeroline/subfunction-editor.test.ts b/test/unit/zeroline/subfunction-editor.test.ts new file mode 100644 index 0000000000..97740500a4 --- /dev/null +++ b/test/unit/zeroline/subfunction-editor.test.ts @@ -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 = ( + await fixture( + html`` + ) + ); + }); + + 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(); + }); + }); +});