WILT: Behat, Mink and ShadowDom

Once again I'm rebuilding part of the site using more modern coding practices in an effort to keep skills up to date. Also it's fun. This time I'm looking at the GURPS character generator I wrong years ago. Of all the pages on my site, it's the most referenced in my apache logs so I'm looking at what I can do better. The statistics, advantages and skills are a lot of repeated components, and nowadays we've got custom elements and templates/ slots to make life easier. Currently, I'm playing with custom elements because all my components have a lot of javascript attached to them, and later on using templates may help but not for now.

Statistics are the simplest of the elements so far. You've got one score you change, which alters a lot of other scores and costs. So my strength attribute looks like

<statistic-group id="strength" value="10" costper="10" abbr="ST" linked="hitpoints" labbr="HP" current>

The custom element is statistic-group and it has javascript to make it do things.

    class Statistic extends HTMLElement {
        constructor() {
            super();

            // get config from attributes
            const value = this.getAttribute('value');
            const id = this.getAttribute('id');
            const abbr = this.getAttribute('abbr');
            const costper = this.getAttribute('costper');
            const cost = (value - 10) * costper;
            const labbr = this.getAttribute('labbr');
            const current = this.hasAttribute('current');

            // events
            this.adjustValue = this.adjustValue.bind(this);

            // create shadow dom root
            this._root = this.attachShadow({
                mode: 'open'
            });
            let currentAttr = `<span class="spacer"></span>`;
            if (current) {
                currentAttr = `<label class="current">current</label><input type="text" value="" disabled/>`;
            }
            this._root.innerHTML = `
            <style>...</style>
            <section style="position:relative;">
            <label class="primary" for="${id}">${abbr}</label>
            <input type="text" class="keyvalue" value="${value}" id="${id}" />
            <span class="cost">${cost}</span>
            <label class="secondary" for="l${id}">${labbr}</label>
            <input type="text" value="${value}" id="l${id}" disabled />${currentAttr}
            <span class="cost">&nbsp;&nbsp;&nbsp;</span>
            </section>`;

            // attach
            this.valueField = this.shadowRoot.querySelector('input.keyvalue');
            statistics_cost.value = parseInt(statistics_cost.value, 10) + cost;
            this.dispatchEvent(new CustomEvent('statchange', {
                bubbles: true,
                composed: true,
                detail: "composed"
            }));
        }
        connectedCallback() {
            this.valueField.addEventListener('input', this.adjustValue);
            if (!this.hasAttribute('value')) {
                this.setAttribute('value', 10);
            }
        }
        adjustValue() {
            let newValue = +this.shadowRoot.querySelector('input').value,
                oldValue = +this.getAttribute('value'),
                costPer = this.getAttribute('costper');
            if (newValue > 20) {
                newValue = 20;
                this.shadowRoot.querySelector('input').value = 20;
            } else if (isNaN(newValue)) {
                newValue = 0;
            }
            statistics_cost.value = parseInt(statistics_cost.value) + (newValue - 10) * costPer - (oldValue - 10) *
                costPer;
            document.getElementById("spent").value = (parseInt(document.getElementById("spent").value, 10) || 0) + (
                newValue - 10) * costPer - (oldValue - 10) * costPer;
            this.shadowRoot.querySelector('span.cost').innerHTML = (newValue - 10) * costPer;
            this.shadowRoot.querySelectorAll('input')[1].value = newValue;
            this.setAttribute('value', newValue);
            this.dispatchEvent(new CustomEvent('statchange', {
                bubbles: true,
                composed: true,
                detail: "composed"
            }));
        }
    }
    window.customElements.define('statistic-group', Statistic);

The attribute itself informs the code what the cost-per-dot is and any linked attributes; and the GURPS page builds the rest. I can also attach events (note the statchange event) that the outer javascript can listen for and use to update other values like the total-character-spend.


This was useful, but I remembered untested code is unreliable, so I pulled out my trusted Behat and Mink test PDF and started writing tests. And they immediately failed on checking the value of statistics because they're not elements it expects - the shadowRoot holds the actual things I want to check. So now I've had to write new code into the FeatureContext.php for Mink to add finding values for events that have shadowRoot. And since shadowRoot is not generic, I'm looking forward to writing a lot of custom test handlers and custom test statements and... well, sure. Because coding is learning and this is new. Huzzah!