Skip to content

Commit a3bdfed

Browse files
ffriedl89marvinhagemeister
authored andcommitted
test: Context passing to children
1 parent 1beca66 commit a3bdfed

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/index.test.jsx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assert } from '@open-wc/testing';
2-
import { h } from 'preact';
2+
import { h, createContext } from 'preact';
3+
import { useContext } from 'preact/hooks';
34
import registerElement from './index';
45

56
function Clock({ time }) {
@@ -113,3 +114,45 @@ it('handles kebab-case attributes with passthrough', () => {
113114

114115
document.body.removeChild(root);
115116
});
117+
118+
const Theme = createContext('light');
119+
120+
function DisplayTheme() {
121+
const theme = useContext(Theme);
122+
return <p>Active theme: {theme}</p>;
123+
}
124+
125+
registerElement(DisplayTheme, 'x-display-theme', [], { shadow: true });
126+
127+
function Parent({ children }) {
128+
return (
129+
<Theme.Provider value="dark">
130+
<div class="children">{children}</div>
131+
</Theme.Provider>
132+
);
133+
}
134+
135+
registerElement(Parent, 'x-parent', [], { shadow: true });
136+
137+
it('passes down information using context over custom element boundaries', () => {
138+
const root = document.createElement('div');
139+
const el = document.createElement('x-parent');
140+
141+
const noSlot = document.createElement('x-display-theme');
142+
el.appendChild(noSlot);
143+
144+
root.appendChild(el);
145+
document.body.appendChild(root);
146+
147+
assert.equal(
148+
root.innerHTML,
149+
'<x-parent><x-display-theme></x-display-theme></x-parent>'
150+
);
151+
152+
const shadowHTML = document.querySelector('x-display-theme').shadowRoot
153+
.innerHTML;
154+
155+
assert.equal(shadowHTML, '<p>Active theme: dark</p>');
156+
157+
document.body.removeChild(root);
158+
});

0 commit comments

Comments
 (0)