0

I am using React Native Switch. I want to write a unit test (using jest and testing-library/react-native) for its value prop. But there is no value in its props

My use case is there will be a default value for the Switch when the page load. But I can not find a way to check the Switch value. Is there a correct way to check if its value is true or false?enter image description here

Picture attached is the Switch's list of props

manh.vu
  • 335
  • 1
  • 4
  • 14

1 Answers1

0

You can check the value of the prop like this, including if it works when you change it:

expect(screen.getByRole('switch').props.value).toBe(true);

fireEvent(screen.getByRole('switch'), 'onValueChange');

expect(screen.getByRole('switch').props.value).toBe(false);

Assuming your component looks something like this:

    <Switch
      trackColor={{true: something, false:somethingElse}}
      thumbColor={someColor}
      ios_backgroundColor={somethingSomething}
      onValueChange={toggleValue}
      value={value}
    />

with somthing like const [value, toggleValue] = useState(false); for the value. Let me know if this doesn't work for you, please also add the component and the test you have now. It is hard to say what's going wrong without seeing that

Mister_CK
  • 668
  • 3
  • 13