无法读取未定义的属性“方向",仅测试 [英] Cannot read property 'Direction' of undefined, tests only

查看:34
本文介绍了无法读取未定义的属性“方向",仅测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将 TouchableOpacity 添加到一个组件中,该应用程序运行良好,但我使用 react-native-testing-library 的测试无法运行:

I just added TouchableOpacity to a component and the app is working fine, but my tests, using react-native-testing-library, fail to run:

  ● Test suite failed to run

    TypeError: Cannot read property 'Direction' of undefined

      at Object.Direction (node_modules/react-native-gesture-handler/Directions.js:3:39)
      at Object.<anonymous> (node_modules/react-native-gesture-handler/GestureHandler.js:2:1)

我刚刚删除并重新添加了带有纱线的 react-native-gesture-handler,然后运行了 pod install.同样,应用程序可以运行,但测试无法运行.

I just removed and re-added react-native-gesture-handler with yarn, and ran pod install. Again, the app is working, but the tests fail to run.

我实际上在使用 时遇到了同样的错误;onOptionPress(opt)}/> 而不是 TouchableOpacity.

I actually get the same error when using <Text onPress={() => onOptionPress(opt)} /> rather than TouchableOpacity.

组件:

const SelectOptions = ({ field, dismissOverlay, onOptionPress }) => {
  return (
    <Overlay
      isVisible
      overlayStyle={styles.overlay}
      height={"auto"}
      onBackdropPress={dismissOverlay}
    >
      <View>
        {field.options.map((opt, i) => (
          <TouchableOpacity
            style={styles.option}
            key={i}
            onPress={() => onOptionPress(opt)}
          >
            <Text>{opt}</Text>
          </TouchableOpacity>
        ))}
      </View>
    </Overlay>
  );
};

测试:

describe("CardFormView", () => {
  let wrapper, birthdayField;

  beforeEach(() => {
    wrapper = render(
      <React.Fragment>
        <CardFormView form={form} />
      </React.Fragment>
    );
    birthdayField = wrapper.getByText("Add a Birthday Gift Card");
  });

  const message1 =
    "...";
  const message2 =
    "...";

  it("shows the options for a birthday card when clicked", () => {
    fireEvent.press(birthdayField);
    expect(wrapper.getByText(message1)).toBeDefined();
  });

  it("sets an option when clicked", () => {
    fireEvent.press(birthdayField);
    const firstOption = wrapper.getByText(message1);
    fireEvent.press(firstOption);
    expect(wrapper.queryByText(message2)).toBeNull();
    expect(wrapper.getByText(message1)).toBeDefined();
  });
});

推荐答案

这是因为你没有嘲笑 react-navigation-gesture-handler

要使用 react-navigation-gesture-handler 的模拟,您应该从 jest.config.json 或 jest.config.js 中的 node_modules 添加 jestSetup.js

To use mock of react-navigation-gesture-handler you should add jestSetup.js from node_modules in jest.config.json or jest.config.js

setupFiles: [
  "./node_modules/react-native-gesture-handler/jestSetup.js"
]

我从以下链接找到了一个参考,它对我有用.

I found a reference from the following link and It's working for me.

https://github.com/software-mansion/react-native-gesture-handler/issues/344#issuecomment-489547513

这篇关于无法读取未定义的属性“方向",仅测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆