Skip to content

Commit

Permalink
Added test case for shadowrootmode open vs. closed
Browse files Browse the repository at this point in the history
  • Loading branch information
briangrider committed Jan 17, 2025
1 parent 9ea019b commit f1d726c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/cases/shadowrootmode/shadowrootmode.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Use Case
* Run wcc against two custom elements, one with an open shadow root and one with a closed shadow root
*
* User Result
* Should return the expected HTML output based on the shadow root mode of each component
*
* User Workspace
* src/
* index.js
*/

import chai from 'chai';
import { JSDOM } from 'jsdom';
import { renderToString } from '../../../src/wcc.js';

const expect = chai.expect;

describe('Run WCC For ', function () {
const LABEL = 'Custom Elements w/ closed and open shadowrootmode';
let dom;

before(async function () {
const { html } = await renderToString(new URL('./src/index.js', import.meta.url));
console.log('hello', html);
dom = new JSDOM(html);
});

describe(LABEL, function () {
it('should have exactly one open shadowrootmode template', function () {
expect(
dom.window.document.querySelectorAll('open-shadow-component template[shadowrootmode="open"]').length
).to.equal(1);
});

it('should have exactly one closed shadowrootmode template', function () {
expect(
dom.window.document.querySelectorAll('closed-shadow-component template[shadowrootmode="closed"]').length
).to.equal(1);
});
});
});
19 changes: 19 additions & 0 deletions test/cases/shadowrootmode/src/closed-shadow-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default class ClosedShadowComponent extends HTMLElement {
constructor() {
super();
}

async connectedCallback() {

if (!this.shadowRoot) {
this.attachShadow({ mode: 'closed' });
const template = document.createElement('template');
template.innerHTML = `
<h1>Shadow Root Closed</h1>
`;
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
}
}

customElements.define('closed-shadow-component', ClosedShadowComponent);
17 changes: 17 additions & 0 deletions test/cases/shadowrootmode/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import './open-shadow-component.js';
import './closed-shadow-component.js';

export default class HomePage extends HTMLElement {
constructor() {
super();
}

connectedCallback() {
this.innerHTML = `
<open-shadow-component></open-shadow-component>
<closed-shadow-component></closed-shadow-component>
`;
}
}

customElements.define('wcc-home', HomePage);
19 changes: 19 additions & 0 deletions test/cases/shadowrootmode/src/open-shadow-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default class OpenShadowComponent extends HTMLElement {
constructor() {
super();
}

async connectedCallback() {

if (!this.shadowRoot) {
this.attachShadow({ mode: 'open' });
const template = document.createElement('template');
template.innerHTML = `
<h1>Shadow Root Open</h1>
`;
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
}
}

customElements.define('open-shadow-component', OpenShadowComponent);

0 comments on commit f1d726c

Please sign in to comment.