React-native-testing-library: Select various segments on SegmentedControlIOS

Created on 27 Aug 2019  路  4Comments  路  Source: callstack/react-native-testing-library

Ask your Question

I have a <SegmentedControlIOS> - https://facebook.github.io/react-native/docs/segmentedcontrolios

I want to pres the first segment. I am doing this:

const testID = "SegmentedControl";
const stub = jest.fn();
const values = [{ label: "foo" }];
const { getByTestId } = render(
  <SegmentedControlIOS values={['foo', 'bar']} onChange={stub} testID={testID} />
);

expect(() => {
  getByTestId(testID);
}).not.toThrow();

fireEvent(getByTestId(testID), "change ", {
  nativeEvent: {
    value: values[0],
    selectedSegmentIndex: 0,
  },
});

However I get the error:

No handler function found for event: "change "

Screenshot below. Anyone know how to press different segments in <SegmentedControlIOS>?

enter image description here

question

Most helpful comment

I forgot to say, it works! Thank you!

All 4 comments

I believe the event should be "onChange"

import React from "react";
import { SegmentedControlIOS } from "react-native";
import { fireEvent, render } from "react-native-testing-library";

const testID = "SegmentedControl";
const stub = jest.fn();
const values = [{ label: "foo" }];
const { getByTestId } = render(
  <SegmentedControlIOS
    values={["foo", "bar"]}
    onChange={stub}
    testID={testID}
  />,
);
it("sends events", () => {
  fireEvent(getByTestId(testID), "onChange", {
    nativeEvent: {
      value: values[0],
      selectedSegmentIndex: 0,
    },
  });
});

Thank you! Will try that tongiht!

I forgot to say, it works! Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

deepakaggarwal7 picture deepakaggarwal7  路  3Comments

jonmchan picture jonmchan  路  11Comments

acollazomayer picture acollazomayer  路  3Comments

bitttttten picture bitttttten  路  9Comments

ptrckhjnl picture ptrckhjnl  路  6Comments