无法模拟本机模块

Sim*_*eer 2 unit-testing jestjs react-native

我正在尝试测试创建的组件,该组件使用MapViewfrom react-native-maps作为子组件。我知道我需要模拟该本机组件,但是我写的内容仍然标记一个错误:

import 'react-native';
import React from 'react';
import Detail from '../js/components/Detail';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

jest.mock('react-native-maps', () => 'MapView');

it('renders correctly', () => {
  const tree = renderer.create(
    <Detail />
  ).toJSON();
  expect(tree).toMatchSnapshot();
});
Run Code Online (Sandbox Code Playgroud)

和我收到的错误:

TypeError: Cannot read property 'setNativeProps' of null

任何帮助,将不胜感激。

编辑:如果有帮助,这是我要测试的组件...

import React, { Component } from 'react';
import {
  Dimensions,
  LayoutAnimation,
  StyleSheet,
  Text,
  TouchableHighlight,
  View,
} from 'react-native';
import MapView from 'react-native-maps';

import MapExpandButton from './buttons/MapExpandButton';

const styles = StyleSheet.create({
  ...
});

export default class AppointmentDetail extends Component {
  constructor(props) {
    super(props);
    this.state = {
      expanded: false,
    }
  }

  componentWillUpdate() {
    LayoutAnimation.easeInEaseOut();
  }

  renderMap() {
    return (
      <MapView
        initialRegion={{
          latitude: 51.501211,
          longitude: -0.110530,
          latitudeDelta: 0.005,
          longitudeDelta: 0.005,
        }}
        scrollEnabled={this.state.expanded}
        pitchEnabled={this.state.expanded}
        zoomEnabled={this.state.expanded}
        rotateEnabled={this.state.expanded}
        loadingEnabled={true}
        showsPointsOfInterest={false}
        style={[styles.map, this.state.expanded ? styles.mapExpanded : undefined]}>
        <MapView.Marker
          coordinate={{
            latitude: 51.501211,
            longitude: -0.110530,
          }}
          title='My Pin'
          description='Somewhere'
        />
      </MapView>
    );
  }

  renderSummary() {
    return (
      <View style={styles.section}>
        <Text style={styles.headline}>
          Appointment Type
        </Text>
        <Text style={styles.subheading}>
          on Date at Timeslot
        </Text>
      </View>
    );
  }

  renderAddress() {
    if (!this.state.expanded) {
      return (
        <View style={styles.section}>
          <Text style={styles.body}>
            Address Line 1,
          </Text>
          <Text style={styles.body}>
            Address Line 2,
          </Text>
          <Text style={styles.body}>
            City POSTCODE,
          </Text>
        </View>
      );
    }
  }

  renderSections() {
    return (
      <View>
        { this.renderSummary() }
        { this.renderAddress() }
      </View>
    );
  }

  toggleExpand() {
    const newExpandedState = !this.state.expanded;
    this.setState({ expanded: newExpandedState });
    if (this.props.didChangeExpandedState) {
      this.props.didChangeExpandedState(newExpandedState);
    }
  }

  render() {
    return (
      <View style={styles.container}>
        { this.renderMap() }
        { this.renderSections() }
        <View style={{ position: 'absolute', right: 8, top: 8 }}>
          <MapExpandButton onPress={this.toggleExpand.bind(this)} />
        </View>
      </View>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*eer 5

在GitHub上找到了此问题的解决方案:https : //github.com/facebook/jest/issues/1960

我认为问题在于没有嘲笑MapView.Marker子组件。这个答案也在嘲笑子组件。

jest.mock('react-native-maps', () => {
  return class MockMapView extends React.Component {
    static Marker = props => React.createElement('Marker', props, props.children);
    static propTypes = { children: React.PropTypes.any };

    render() {
      return React.createElement('MapView', this.props, this.props.children);
    }
  }
});
Run Code Online (Sandbox Code Playgroud)