Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot test fails when using React Native Picker and Picker.Item #1960

Closed
Gumija opened this issue Oct 20, 2016 · 12 comments
Closed

Snapshot test fails when using React Native Picker and Picker.Item #1960

Gumija opened this issue Oct 20, 2016 · 12 comments

Comments

@Gumija
Copy link

Gumija commented Oct 20, 2016

I have seen similar problems with other components, but couldn't find the right mock implementation for Picker.

My error:

TypeError: Cannot read property '_tag' of undefined

My component:

import React, { Component } from 'react';
import {
  Picker,
} from 'react-native';


export default class TestComponent extends Component {

  render() {
    return (
      <Picker
        selectedValue={this.props.asset.type}
        onValueChange={this.props.onTypeChange}>
        <Picker.Item label="Type of asset" value="default" />
        <Picker.Item label="Car" value="car" />
        <Picker.Item label="Boat" value="boat" />
        <Picker.Item label="Ship" value="ship" />
      </Picker>
    );
  }
}

My test:

import 'react-native';
import React from 'react';
import TestComponent from '../testExample/TestComponent';
import renderer from 'react-test-renderer';

describe('TestComponent', () => {
  const asset = {
    type: 'car',
  }
  it('renders correctly', () => {
    const comp = renderer.create(
      <TestComponent 
        asset={asset} />
    )
    const tree = comp.toJSON();
    expect(tree).toMatchSnapshot();
  });
})

Tried mocking it some way but didn't help. I could make it work with Picker.Item-s commented out.

jest.mock('Picker', () => 'Picker'); //  Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `TestComponent`.
@elimydlarz
Copy link

I have the same issue =/

@cpojer
Copy link
Member

cpojer commented Oct 28, 2016

For this you'll likely need to do something like:

jest.mock('Picker', () => {
  const Picker = new String('Picker');
  Picker.Item = 'Item';
  return Picker;
});

@cpojer cpojer closed this as completed Oct 28, 2016
@elimydlarz
Copy link

@cpojer This doesn't work. If you do this, as suggested:

jest.mock('Picker', () => {
  const Picker = new String('Picker');
  Picker.Item = 'Item';
  return Picker;
});

Then it says this about the Picker component:

...expected a string (for built-in components) or a class/function (for composite components) but got: object...

If you try this:

jest.mock('Picker', () => {
  const Picker = 'Picker';
  Picker.Item = 'Item';
  return Picker;
});

Then you get this about the Picker.Item component:

...expected a string (for built-in components) or a class/function (for composite components) but got: undefined...

@cpojer
Copy link
Member

cpojer commented Oct 29, 2016

In that case I'd do this:

const Picker = class extends React.Component {
  render() {
    return 'Picker';
  }
}
Picker.Item = 'Item';
return Picker;

@elimydlarz
Copy link

Thanks! I ended up doing this:

const mockPicker = class MockPicker extends Component {
  static Item = props => React.createElement('Item', props, props.children);
  static propTypes = { children: React.PropTypes.any };

  render() {
    return React.createElement('Picker', this.props, this.props.children);
  }
};

jest.mock('Picker', () => mockPicker);

Which works =)

@RobinClowers
Copy link

When I tried this, I got:

The module factory of jest.mock() is not allowed to reference any out-of-scope variables.

@cpojer
Copy link
Member

cpojer commented Nov 3, 2016

you should instead do this, actually:

jest.mock('Picker', () => {
  const React = require('React');
  return class MockPicker extends Component {
    static Item = props => React.createElement('Item', props, props.children);
    static propTypes = { children: React.PropTypes.any };

    render() {
      return React.createElement('Picker', this.props, this.props.children);
    }
  }
});

@simonsmith
Copy link

Just curious, is it Jest's goal to support all the RN component out of the box?

@cpojer
Copy link
Member

cpojer commented Nov 29, 2016

No. jest-react-native was always just an experimental feature until we merged the configuration back into react-native with 0.38. From now on, if you'd like to mock RN components, please send a diff for that directly to react-native. This decouples our dependency on RN and makes this solely an RN concern :)

@WilliamIPark
Copy link

If you're getting this error after having tried some of the solutions here:
TypeError: Cannot read property 'any' of undefined

It's because proptypes is no longer bundled with React. Just remove the
static propTypes = { children: React.PropTypes.any };
line.

@LuisUrrutia
Copy link

@WilliamIPark is right, you should use this in the newest versions:

jest
  .mock('Picker', () => {
    // eslint-disable-next-line import/no-unresolved
    const React = require('React');
    const PropTypes = require('prop-types');
    return class MockPicker extends React.Component {
      static Item = props => React.createElement('Item', props, props.children);
      static propTypes = { children: PropTypes.any };
      static defaultProps = { children: '' };

      render() {
        return React.createElement('Picker', this.props, this.props.children);
      }
    };
  });

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants