使用 react-native-testing-library 在 Jest 中未呈现 NativeBase 内容

NativeBase Content not rendered in Jest with react-native-testing-library

我有一些 react-native/expo native-base 代码可以在 phone 或模拟器上正常运行. 我尝试使用 jestreact-native-testing-library 为它创建一个测试。这样做时,来自 native-base 中的任何内容都不会呈现并且无法在测试中找到。

有没有人经历过这个并且知道解决方案以便在测试期间呈现内容的子项?

下面是一个示例代码来说明我在说什么。 非常感谢您的帮助。

import { render } from 'react-native-testing-library';
import {
  Content, Container, Text
} from 'native-base';


class App extends React.Component {

  render() {
    return (
      <Container>
        <Content>
          <Text testID="textId">Hello</Text>
        </Content>
      </Container>
    );
  }
}

describe('Testing Content', () => {
  const { queryByTestId } = render(<App />)
  it('renders text inside content', () => {
    expect(queryByTestId('textId')).not.toBeNull()
  });

})

包的版本是:

"expo": "^32.0.0",
"react": "16.5.0",
"native-base": "^2.12.1",
"jest-expo": "^32.0.0",
"react-native-testing-library": "^1.7.0"

我在 github (https://github.com/callstack/react-native-testing-library/issues/118) 的 react-native-testing-library 中问了这个问题。

问题出在 react-native-keyboard-aware-scroll-view

为了解决这个问题,我们可以通过以下方式mock它

jest.mock('react-native-keyboard-aware-scroll-view', () => {
    const KeyboardAwareScrollView = ({ children }) => children;
    return { KeyboardAwareScrollView };
});

我也在这里为可能正在寻找的人举了一个例子: https://github.com/pedrohbtp/rntl-content-bug