admin管理员组

文章数量:1431721

its mentioned as using $evalfrom 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();
  });

https://stenciljs/docs/end-to-end-testing#set-a-prop-on-a-component-using-an-external-reference its mentioned as using $evalfrom 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();
  });
Share Improve this question asked Nov 19, 2024 at 9:31 wasilikoslowwasilikoslow 1,9932 gold badges12 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Apparently you need to expose the function and do some typing to "this". https://github/ionic-team/stencil/issues/1174

it('should render the button and click ', async () => {
    const page = await newE2EPage({
      waitUntil: 'networkidle2',
    });
    await page.setViewport({ width: 400, height: 800 });
    await page.setContent('<custom-button>some text</custom-button>');
    const onClick = jest.fn();
    await page.exposeFunction('onClick', onClick);
    await page.$eval(
      'custom-button',
      (elm, { onClick }) => {
        elm.disabled = false;
        elm.onClick = (this as any).onClick;
      },
      props,
    );
    await page.waitForSelector('.hydrated');

    const customButton = await page.find('custom-button');
    await customButton?.click();
    customButton.setProperty('onClick', props.onClick);
    await page.waitForChanges();
    expect(customButton).not.toBeFalsy();

    const buttonElement = await page.find('custom-button >>> button');

    await buttonElement?.click();
    await page.waitForChanges();

    expect(onClick).toHaveBeenCalled();
    expect(onClick).toHaveBeenCalledTimes(1);
  });

本文标签: javascriptHow to pass function as prop in stenciljs e2e testsStack Overflow