简体   繁体   English

React Native 测试库 fireEvent.press 不触发 onPress

[英]React Native Testing Library fireEvent.press not triggering onPress

I'm trying to get an integration test working in React Native (using Jest and RN Testing Library) and all my other tests work as expected but this one it seems the fireEvent.press() stops actually firing off the onPress event from the button TouchableHighlight for a specific component我正在尝试在 React Native 中进行集成测试(使用 Jest 和 RN 测试库),并且我的所有其他测试都按预期工作,但是这个测试似乎fireEvent.press()实际上停止从按钮触发onPress事件特定组件的TouchableHighlight

Workflow:工作流程:

  1. List of people人员名单
  2. press nav item to pull up filters (narrow down list)按导航项目拉起过滤器(缩小列表)
  3. inside modal 2 options of filter types, select "people" filters在过滤器类型的模态2选项中,选择“人”过滤器
  4. renders new list of filters and select one of these呈现新的过滤器列表并选择其中之一
  5. updates list of people and closes modal更新人员列表并关闭模式
  6. user presses "You know it" button用户按下“你知道”按钮
  7. console.log() or setPerson in onButtonPress never fires setPerson中的console.log()onButtonPress永远不会触发

View with button使用按钮查看

export default function PondLeadSnippet({person, setPerson}) {
  const onButtonPress = useCallback(() => {
    console.log("========> onButtonPress");
    setPerson({...person, flagged: true});
  }, [false]);

  return (
    <View style={Style.container}>
      <View style={Style.content}>
        <Label accessibilityRole='person-name' scale='medium'>{person.name}</Label>
        <Text style={Style.detail}>{detail}</Text>
        <Text>{person.flagged ? 'Flagged' : 'Not Flagged'}</Text>
      </View>

      <TouchableHighlight testID={`flag-person-${person.uuid}`} onPress={onButtonPress}}>
        <Text>You know it</Text>
      </TouchableHighlight>
    </View>
  );
}

My Test我的测试

    test.only('can flag a person from list', async () => {
      const { getByText, getAllByRole, findByTestId, getByTestId, store } = setup();
      const bobSaget = people[1];

      // Modal to view filters -> this works!
      const filter = await findByTestId('people-filters');
      await fireEvent.press(filter);

      // toggle between 2 lists of filters -> this works!
      await waitFor(() => expect(getByTestId("people-list")).toBeTruthy());
      fireEvent.press(getByTestId("people-list"));

      // select filter -> this works!
      const peopleFilter = await findByTestId(`list-item-${someID}`);
      fireEvent.press(peopleFilter);

      // wait for list of people to update and modal closed
      await waitFor(() => expect(getAllByRole('person-name')).toHaveLength(2));
      const [ first, second ] = getAllByRole('person-name');
      expect(first.children).toContain("Bob S.")

      // press "Flag Person" button on person -> Does not work
      expect(getByTestId(`flag-person-${bobSaget.uuid}`)).toBeTruthy();
      fireEvent.press(getByTestId(`flag-person-${bobSaget.uuid}`));

      // after event rerender should happen w/ new text element w/ "Flagged"
      await waitFor(() => expect(getByText("Flagged")).toBeTruthy());
    });

Your TouchableHighlight might have not been rendered when you try fire event.当您尝试触发事件时,您的 TouchableHighlight 可能尚未呈现。 Try to replace尝试更换

 expect(getByTestId(`flag-person-${bobSaget.uuid}`)).toBeTruthy();

with

 await waitFor(() => {
     expect(getByTestId(`flag-person-${bobSaget.uuid}`)).toBeTruthy();
   })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM