Skip to content

Commit

Permalink
Add ts test
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Dec 29, 2024
1 parent e0ddebe commit 6740b2c
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions test/ts/custom-elements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface WhateveElAttributes extends createElement.JSX.HTMLAttributes {
}

// Ensure context still works
const { Provider, Consumer } = createContext({ contextValue: '' });
const Ctx = createContext({ contextValue: '' });

// Sample component that uses custom elements

Expand All @@ -50,7 +50,7 @@ class SimpleComponent extends Component {
render() {
// Render inside div to ensure standard JSX elements still work
return (
<Provider value={{ contextValue: 'value' }}>
<Ctx.Provider value={{ contextValue: 'value' }}>
<div>
<clickable-ce
onClick={e => {
Expand All @@ -73,11 +73,48 @@ class SimpleComponent extends Component {
></custom-whatever>

{/* Ensure context still works */}
<Consumer>
<Ctx.Consumer>
{({ contextValue }) => contextValue.toLowerCase()}
</Consumer>
</Ctx.Consumer>
</div>
</Provider>
</Ctx.Provider>
);
}
}

class SimpleComponentWithContextAsProvider extends Component {
componentProp = 'componentProp';
render() {
// Render inside div to ensure standard JSX elements still work
return (
<Ctx value={{ contextValue: 'value' }}>
<div>
<clickable-ce
onClick={e => {
// `this` should be instance of SimpleComponent since this is an
// arrow function
console.log(this.componentProp);

// Validate `currentTarget` is HTMLElement
console.log('clicked ', e.currentTarget.style.display);
}}
></clickable-ce>
<color-picker space="rgb" dir="rtl"></color-picker>
<custom-whatever
dir="auto" // Inherited prop from HTMLAttributes
someattribute="string"
onsomeevent={function (e) {
// Validate `this` and `e` are the right type
console.log('clicked', this.instanceProp, e.eventProp);
}}
></custom-whatever>

{/* Ensure context still works */}
<Ctx.Consumer>
{({ contextValue }) => contextValue.toLowerCase()}
</Ctx.Consumer>
</div>
</Ctx>
);
}
}
Expand Down

0 comments on commit 6740b2c

Please sign in to comment.