import { LitElement } from '@lion/core'; import { defineCE, expect, fixture, html, unsafeStatic } from '@open-wc/testing'; import { FormRegisteringMixin } from '../src/registration/FormRegisteringMixin.js'; import { FormRegistrarMixin } from '../src/registration/FormRegistrarMixin.js'; import { FormRegistrarPortalMixin } from '../src/registration/FormRegistrarPortalMixin.js'; export const runRegistrationSuite = customConfig => { const cfg = { baseElement: HTMLElement, ...customConfig, }; describe(`FormRegistrationMixins ${cfg.suffix}`, () => { let parentTag; let childTag; let portalTag; let parentTagString; let childTagString; before(async () => { if (!cfg.parentTagString) { cfg.parentTagString = defineCE(class extends FormRegistrarMixin(cfg.baseElement) {}); } if (!cfg.childTagString) { cfg.childTagString = defineCE(class extends FormRegisteringMixin(cfg.baseElement) {}); } if (!cfg.portalTagString) { cfg.portalTagString = defineCE(class extends FormRegistrarPortalMixin(cfg.baseElement) {}); } parentTag = unsafeStatic(cfg.parentTagString); childTag = unsafeStatic(cfg.childTagString); portalTag = unsafeStatic(cfg.portalTagString); parentTagString = cfg.parentTagString; childTagString = cfg.childTagString; }); it('can register a formElement', async () => { const el = await fixture(html` <${parentTag}> <${childTag}> `); expect(el.formElements.length).to.equal(1); }); it('works with document.createElement', async () => { const el = document.createElement(parentTagString); const childEl = document.createElement(childTagString); expect(el.formElements.length).to.equal(0); const wrapper = await fixture('
'); el.appendChild(childEl); wrapper.appendChild(el); expect(el.formElements.length).to.equal(1); }); it('can register a formElement with arbitrary dom tree in between registrar and registering', async () => { const el = await fixture(html` <${parentTag}>
<${childTag}>
`); expect(el.formElements.length).to.equal(1); }); it('supports nested registration parents', async () => { const el = await fixture(html` <${parentTag}> <${parentTag} class="sub-group"> <${childTag}> <${childTag}> `); expect(el.formElements.length).to.equal(1); const subGroup = el.querySelector('.sub-group'); expect(subGroup.formElements.length).to.equal(2); }); it('works for components that have a delayed render', async () => { const tagWrapperString = defineCE( class extends FormRegistrarMixin(LitElement) { async performUpdate() { await new Promise(resolve => setTimeout(() => resolve(), 10)); await super.performUpdate(); } render() { return html``; } }, ); const tagWrapper = unsafeStatic(tagWrapperString); const el = await fixture(html` <${tagWrapper}> <${childTag}> `); expect(el.formElements.length).to.equal(1); }); it('can dynamically add/remove elements', async () => { const el = await fixture(html` <${parentTag}> <${childTag}> `); const newField = await fixture(html` <${childTag}> `); expect(el.formElements.length).to.equal(1); el.appendChild(newField); expect(el.formElements.length).to.equal(2); el.removeChild(newField); expect(el.formElements.length).to.equal(1); }); it('adds elements to formElements in the right order (DOM)', async () => { const el = await fixture(html` <${parentTag}> <${childTag}> <${childTag}> <${childTag}> `); const newField = await fixture(html` <${childTag}> `); newField.myProp = 'test'; el.insertBefore(newField, el.children[1]); expect(el.formElements.length).to.equal(4); expect(el.children[1].myProp).to.equal('test'); expect(el.formElements[1].myProp).to.equal('test'); }); describe('FormRegistrarPortalMixin', () => { it('forwards registrations to the .registrationTarget', async () => { const el = await fixture(html`<${parentTag}>`); await fixture(html` <${portalTag} .registrationTarget=${el}> <${childTag}> `); expect(el.formElements.length).to.equal(1); }); it('can dynamically add/remove elements', async () => { const el = await fixture(html`<${parentTag}>`); const portal = await fixture(html` <${portalTag} .registrationTarget=${el}> <${childTag}> `); const newField = await fixture(html` <${childTag}> `); expect(el.formElements.length).to.equal(1); portal.appendChild(newField); expect(el.formElements.length).to.equal(2); portal.removeChild(newField); expect(el.formElements.length).to.equal(1); }); it('adds elements to formElements in the right order', async () => { const el = await fixture(html` <${parentTag}> <${childTag}> <${childTag}> <${childTag}> `); expect(el.formElements.length).to.equal(3); // In the middle const secondChild = el.firstElementChild.nextElementSibling; const newField = await fixture(html` <${childTag}> `); secondChild.insertAdjacentElement('beforebegin', newField); expect(el.formElements.length).to.equal(4); expect(el.formElements[1]).dom.to.equal(newField); // Prepending const anotherField = await fixture(html` <${childTag}> `); el.prepend(anotherField); expect(el.formElements.length).to.equal(5); expect(el.formElements[0]).dom.to.equal(anotherField); // Appending const yetAnotherField = await fixture(html` <${childTag}> `); el.appendChild(yetAnotherField); expect(el.formElements.length).to.equal(6); expect(el.formElements[5]).dom.to.equal(anotherField); }); it('keeps working if moving the portal itself', async () => { const el = await fixture(html`<${parentTag}>`); const portal = await fixture(html` <${portalTag} .registrationTarget=${el}> <${childTag}> `); const otherPlace = await fixture(html`
`); otherPlace.appendChild(portal); const newField = await fixture(html` <${childTag}> `); expect(el.formElements.length).to.equal(1); portal.appendChild(newField); expect(el.formElements.length).to.equal(2); portal.removeChild(newField); expect(el.formElements.length).to.equal(1); }); it('works for portals that have a delayed render', async () => { const delayedPortalString = defineCE( class extends FormRegistrarPortalMixin(LitElement) { async performUpdate() { await new Promise(resolve => setTimeout(() => resolve(), 10)); await super.performUpdate(); } render() { return html``; } }, ); const delayedPortalTag = unsafeStatic(delayedPortalString); const el = await fixture(html`<${parentTag}>`); await fixture(html` <${delayedPortalTag} .registrationTarget=${el}> <${childTag}> `); expect(el.formElements.length).to.equal(1); }); }); }); };