Unable to mock native module

3.9k views Asked by At

I'm trying to test a component I've created that uses MapView from react-native-maps as a child component. I know I need to mock that native component but what I've written still flags up an error:

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();
});

and the error I receive:

TypeError: Cannot read property 'setNativeProps' of null

Any help would be appreciated.

EDIT: If it helps, here's the component I'm trying to test...

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>
    );
  }
}
3

There are 3 answers

1
SimonTheEngineer On BEST ANSWER

Found a solution from this issue on GitHub: https://github.com/facebook/jest/issues/1960

I think the issue was that MapView.Marker child components weren't being mocked. This answer is mocking the child components as well.

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);
    }
  }
});
4
Matt Aft On

When I ran into this issue I added the modules to the ignore list under jest in your package.json:

"transformIgnorePatterns": [
  "node_modules/(?!react-native|react-native-maps)"
]
0
Hamza Hmem On

Please try this for the latest react-native-maps library version:


jest.mock('react-native-maps', () => {
  const React = require('react');
  const {View} = require('react-native');

  class MockMapView extends React.Component {
    render() {
      const {testID, children, ...props} = this.props;

      return (
        <View
          {...{
            ...props,
            testID,
          }}>
          {children}
        </View>
      );
    }
  }

  class MockMarker extends React.Component {
    render() {
      const {testID, children, ...props} = this.props;

      return (
        <View
          {...{
            ...props,
            testID,
          }}>
          {children}
        </View>
      );
    }
  }

  return {
    __esModule: true,
    default: MockMapView,
    Marker: MockMarker,
    enableLatestRenderer: jest.fn(),
  };
});