首页 > 解决方案 > 如何在 react-native-router-flux 中模拟 Actions 的 Scene 键?

问题描述

我正在尝试测试按钮按下是否会调用Actions.sceneKey(); 我尝试使用它来模拟它

jest.mock(
  'react-native-router-flux', () => ({
    Actions: {
      sceneKey: jest.fn(),
    },
  })
);

但是当这段代码运行时:

test('enter received code', () => {
  const { getByPlaceholderText, getByText } = render(
    <EnterCode />
  );
  const button = getByText('confirm');
  fireEvent.press(button);
  expect(button).toBeTruthy();
  expect(Actions.sceneKey).toBeCalledTimes(1);
});

它说

expect(jest.fn()).toBeCalledTimes(expected)
Expected number of calls: 1
Received number of calls: 0
> 45 |   expect(Actions.sceneKey).toBeCalledTimes(1);

同样在我的组件中,我的 onPress 回调中有这个函数:

<Button
  onPress={() => {
    Actions.sceneKey();
    }}
  text="confirm"
/>

另外,由于我是新来测试一个更重要的问题是我什至需要测试场景转换功能吗?

标签: react-nativemockingjestjsreact-native-router-flux

解决方案


推荐阅读