|
1 | 1 | import { assert } from '@open-wc/testing';
|
2 |
| -import { h } from 'preact'; |
| 2 | +import { h, createContext } from 'preact'; |
| 3 | +import { useContext } from 'preact/hooks'; |
3 | 4 | import registerElement from './index';
|
4 | 5 |
|
5 | 6 | function Clock({ time }) {
|
@@ -113,3 +114,45 @@ it('handles kebab-case attributes with passthrough', () => {
|
113 | 114 |
|
114 | 115 | document.body.removeChild(root);
|
115 | 116 | });
|
| 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