Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

click event is always passing false in checked field for input with type checkbox #103

Open
almo7 opened this issue Jun 7, 2020 · 2 comments

Comments

@almo7
Copy link

almo7 commented Jun 7, 2020

For example, a simple component:

const App = (props: AppProps) => {
  const [checked, setChecked] = React.useState(false);

  const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    console.log('event.target.checked', event.target.checked);
    setChecked(event.target.checked);
  }

  return (
    <label>
      <input type="checkbox" checked={checked} onChange={onChange}></input>
      Click me!
    </label>
  );
}

Test file:

const createDriver = (driver: UniDriver) => {
  return {
    checked: async () => {
      return (await driver.$('input').getNative()).checked
    },
    click: () => {
      driver.$('input').click();
    }
  };
};

const renderAppAndCreateDriver = () => {
	const div = document.createElement('div');
	ReactDOM.render(<App />, div);
	const base = jsdomReactUniDriver(div);
	return createDriver(base);
};

describe('App', () => {
  it('changes checkbox on click - UniDriver', async () => {
    const driver = renderAppAndCreateDriver();

    expect(await driver.checked()).toBeFalsy();

    await driver.click();

    expect(await driver.checked()).toBeTruthy();
  });

  it('changes checkbox on click', async () => {
    const { getByLabelText } = render(<App />);

    const el = await waitForElement(() => getByLabelText('Click me!')) as HTMLInputElement;

    expect(el.checked).toBeFalsy();

    el.click();

    expect(el.checked).toBeTruthy();
  });
});

When simulating a click with event.target.checked (1st test) the event.target.checked is always false. The expected behaviour is that it will alternate between true and false. When using el.click() directly (2nd test) it's alternating as expected.
Full code: https://github.com/wix-a/test-unidriver

@GabiGrin
Copy link
Contributor

GabiGrin commented Jun 7, 2020

@almo7 sounds like a potential jsdom issue
did you check if jsdom returns the right property?

@almo7
Copy link
Author

almo7 commented Jun 7, 2020

I have this log https://github.com/wix-a/test-unidriver/blob/master/src/components/App/App.tsx#L10 It prints false instead of true when triggering the click with UniDirver but prints true when triggering the click with react-testing-lib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants