https://stenciljs.com/docs/end-to-end-testing#set-a-prop-on-a-component-using-an-external-reference
its mentioned as using $eval
from puppetry. But when I inspect the testing browser I cant see the “click” event assigned to the button.
import { Component, h, Host, Prop } from '@stencil/core';
@Component({
tag: 'custom-button',
shadow: true,
})
export class CustomButton {
@Prop() onClick!: (event: UIEvent) => void;
render() {
return (
<Host>
<button
onClick={this.onClick}
>
<slot />
</button>
</Host>
);
}
}
and here is my test case
it('should render the button and click ', async () => {
const props = {
onClick: jest.fn(),
};
const page = await newE2EPage({
waitUntil: 'networkidle2',
});
await page.setViewport({ width: 400, height: 800 });
await page.setContent('<custom-button>some text</custom-button>');
await page.$eval(
'custom-button',
(elm, { onClick }) => {
elm.onClick = onClick;
},
props,
);
const customButton = await page.find('custom-button');
customButton.setProperty('onClick', props.onClick);
expect(customButton).not.toBeFalsy();
const buttonElement = await page.find('custom-button >>> button');
buttonElement.triggerEvent('onClick');
console.log('buttonElement ', buttonElement.triggerEvent('onClick'));
await buttonElement?.click();
await page.waitForChanges();
expect(props.onClick).toHaveBeenCalled();
});