lundi 24 juillet 2017

React Jest test Cannot read property 'pathname' of undefined

Not sure why I'm getting this error in my simple Main.test file.

enter image description here

The constructor of Main.js

export class Main extends Component {
    constructor(props) {
        super(props);

        this.state = {
            location: splitString(props.location.pathname, '/dashboard/')
        }

        if (R.isEmpty(props.view)) {
            isViewServices(this.state.location)
                ? this.props.gotoServicesView()
                : this.props.gotoUsersView()
        }
    }

Main.test

import React from 'react'
import * as enzyme from 'enzyme'
import toJson from 'enzyme-to-json'
import { Main } from './Main'
import Sidebar from '../../components/Common/sidebar'

const main = enzyme.shallow(<Main />);

describe('<Main /> component', () => {

  it('should render', () => {
    const tree = toJson(main);
    expect(tree).toMatchSnapshot();
  });

  it('contains the Sidebar', () => {
    expect(main.find(Sidebar).length).toBe(1);
  });
});

Is there a way to mock up the 'pathname'?

Aucun commentaire:

Enregistrer un commentaire