Skip to content

Commit 613b609

Browse files
committed
Add test for shadow DOM support
1 parent a97f41d commit 613b609

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/includes/test-helpers.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,25 +158,28 @@ export function $$<K extends AllTagNames>(
158158
selector: K
159159
): NodeListOf<TagNameToElement<K>>;
160160
export function $$<K extends AllTagNames>(
161-
root: Element,
161+
root: Element | ShadowRoot,
162162
selector: K
163163
): NodeListOf<TagNameToElement<K>>;
164164
export function $$<T extends Element = Element>(
165165
selector: string
166166
): NodeListOf<T>;
167167
export function $$<T extends Element = Element>(
168-
root: Element,
168+
root: Element | ShadowRoot,
169169
selector: string
170170
): NodeListOf<T>;
171171
export function $$<T extends Element = Element>(
172-
arg1: string | Element,
172+
arg1: string | Element | ShadowRoot,
173173
arg2?: string
174174
): NodeListOf<T> {
175175
let result: NodeListOf<Element>;
176176

177177
if (typeof arg1 === 'string') {
178178
result = document.querySelectorAll(arg1);
179-
} else if (arg1 instanceof Element && typeof arg2 === 'string') {
179+
} else if (
180+
(arg1 instanceof Element || arg1 instanceof ShadowRoot) &&
181+
typeof arg2 === 'string'
182+
) {
180183
result = arg1.querySelectorAll(arg2);
181184
} else {
182185
throw new Error('Invalid arguments passed to $$()');

src/vscode-radio-group/vscode-radio-group.test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
11
import {expect, fixture, html} from '@open-wc/testing';
2-
import {$} from '../includes/test-helpers.js';
2+
import {$, $$, clickOnElement} from '../includes/test-helpers.js';
33
import '../vscode-radio/index.js';
44
import {VscodeRadio} from '../vscode-radio/index.js';
55
import {VscodeRadioGroup} from './index.js';
66

7+
class DemoElement extends HTMLElement {
8+
static template = (() => {
9+
const t = document.createElement('template');
10+
t.innerHTML = `
11+
<vscode-radio-group>
12+
<vscode-radio value="1">Option 1</vscode-radio>
13+
<vscode-radio value="2">Option 2</vscode-radio>
14+
<vscode-radio value="3" default-checked>Option 3</vscode-radio>
15+
</vscode-radio-group>
16+
`;
17+
18+
return t;
19+
})();
20+
21+
constructor() {
22+
super();
23+
24+
const shadow = this.attachShadow({mode: 'open'});
25+
shadow.appendChild(DemoElement.template.content.cloneNode(true));
26+
27+
const sheet = new CSSStyleSheet();
28+
sheet.replaceSync(`
29+
:host {
30+
display: block;
31+
padding: 16px;
32+
}
33+
`);
34+
35+
shadow.adoptedStyleSheets = [sheet];
36+
}
37+
}
38+
39+
customElements.define('demo-element', DemoElement);
40+
741
describe('vscode-radio-group', () => {
842
it('is defined', () => {
943
const el = document.createElement('vscode-radio-group');
@@ -48,4 +82,17 @@ describe('vscode-radio-group', () => {
4882
expect(icon2.classList.contains('checked')).to.be.false;
4983
expect(icon3.classList.contains('checked')).to.be.true;
5084
});
85+
86+
it('works in shadow DOM', async () => {
87+
const el = await fixture<DemoElement>(html`<demo-element></demo-element>`);
88+
89+
const radios = $$(el.shadowRoot!, 'vscode-radio');
90+
const lastRadioDefaultChecked = radios[2].checked;
91+
92+
await clickOnElement(radios[0]);
93+
94+
expect(lastRadioDefaultChecked).to.be.true;
95+
expect(radios[0].checked).to.be.true;
96+
expect(radios[2].checked).to.be.false;
97+
});
5198
});

0 commit comments

Comments
 (0)