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

How to interact with checkbox in jest test? #78

Closed
JanithaR opened this issue Sep 9, 2020 · 5 comments
Closed

How to interact with checkbox in jest test? #78

JanithaR opened this issue Sep 9, 2020 · 5 comments

Comments

@JanithaR
Copy link

JanithaR commented Sep 9, 2020

I am testing with react-native-testing-library and would like to interact with a couple of checkboxes I have in one of my components.

Using fireEvent.press(getByA11yLabel('Drivers bag')); would throw error

No handler function found for event: "press"

I tried calling the onValueChange() like so getByA11yLabel('Drivers bag').props.onValueChange(); and that would throw error

TypeError: Cannot read property 'nativeEvent' of undefined

All I'm interested in testing is whether an effect would run when the user toggles the checkbox.

@nicholaslee119
Copy link
Collaborator

https://github.com/react-native-community/react-native-checkbox/blob/223e604f4fbe1abd57a476d743d294f6de82a38a/example/AppFunction.tsx#L25-L86

There are some examples to show how to change the value of checkbox outside. Please feel free to let us know if you have any trouble in the test of checkbox

@Selfmade-RuLeZ
Copy link

Selfmade-RuLeZ commented Sep 22, 2020

do you still need a better implementation as a new Button just for testing? This is how I solved it:

it('performs onValueChange', () => {
  const func = jest.fn();
  const {getByTestId} = render(
    <CheckBox onValueChange={func} testID="myTest" />,
  );
  const checkBox = getByTestId('myTest');
  fireEvent(checkBox, 'onValueChange', {nativeEvent: {}});
  expect(func).toBeCalledTimes(1);
});


it('changes value', () => {
  let checked = false;
  const {getByTestId} = render(
    <CheckBox
      value={checked}
      onValueChange={() => (checked = !checked)}
      testID="myTest"
    />,
  );
  const checkBox = getByTestId('myTest');
  expect(checked).toBeFalsy();
  fireEvent(checkBox, 'onValueChange', {nativeEvent: {}});
  expect(checked).toBeTruthy();
});

The last one is not that much beauty, maybe someone can beautify it a little bit 👍

Edit: Realized it with @testing-library/react-native

@JanithaR
Copy link
Author

JanithaR commented Oct 1, 2020

I went ahead with wrapping the checkbox label with a TouchableOpacity and testing the onPress callback on that.

@echase03
Copy link

Had a similar issue, ended up using this to toggle the checkbox:

fireEvent(getByRole('checkbox'), 'onValueChange', true)

Just wanted to add this in case it helps anybody else!

@anabeatrizzz
Copy link

anabeatrizzz commented Dec 27, 2023

Had a similar issue, ended up using this to toggle the checkbox:

fireEvent(getByRole('checkbox'), 'onValueChange', true)

Just wanted to add this in case it helps anybody else!

Thanks @echase03!

I would like to add also that in my case, I needed to add an await

const checkbox = await findByRole('checkbox');
fireEvent(checkbox, 'onValueChange', true);

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

5 participants