2

I'm trying to test if the LoginContainer component via props exists in the Header with Enzyme, but I'm not having luck.

Here's my main app:

import React from 'react'
import Header from '../components/Header'
import LoginContainer from './LoginContainer'

const App = () => (
    <Header login={<LoginContainer />} />
)
export default App

My Header:

import React from "react";
const Header = ({login}) =>
    <header role="banner">
        {login}
    </header>
export default Header

And this is my Test:

import React from 'react'
import { shallow } from 'enzyme'
import Header from '../components/Header'

const setup = props => {
    const component = shallow(
        <Header {...props} />
    )
    return {
        component: component
    }
}
describe('Header component', () => {
    it('should render login', () => {
        const { component } = setup();
        console.log(component.props({ login })) //this is undefined
    })
})

The problem is when I run the test it seems that login(LoginContainer) props is undefined. Can someone advice please?

3
  • where do you call setup? I dont see any call. Commented Feb 28, 2017 at 13:36
  • sorry, forgot to add. I've just updated. Commented Feb 28, 2017 at 13:38
  • You are not passing anything to setup Commented Feb 28, 2017 at 13:40

2 Answers 2

3

You should get the instance first.

NOTE: When called on a shallow wrapper, .props() will return values for props on the root node that the component renders, not the component itself. To return the props for the entire React component, use wrapper.instance().props. See .instance() => ReactComponent

e.g.

            it('should have props', () => {                
                const l = <div>Login ...</div>;
                const {component} = setup({login:l});
                console.log(component.instance().props.login);
                expect(component.instance().props.login).to.exist;
            });
Sign up to request clarification or add additional context in comments.

3 Comments

The above gives an error: TypeError: Cannot read property 'exist' of undefined. :(
That's because I'm using Chai for the example. If you are not using Chai, you should use another expectation, like to.have.length or to.equal depending of what you expect.
I know. I've tried everything to.have.length,to.equal,.to.equal(true), toBe(1) but none of them worked. Always getting undefined. I really don't know what I'm doing wrong :(
0

You are not passing anything to setup.

import React from 'react'
import { shallow } from 'enzyme'
import Header from '../components/Header'

const setup = props => {
    const component = shallow(
        <Header {...props} />
    )
    return {
        component: component
    }
}
describe('Header component', () => {
    it('should render login', () => {
        const { component } = setup({ login: <LoginContainer/> });
        expect(component.props().login).toBeDefined()
    })
})

1 Comment

Thank you for advice, but your solution still shows undefined or Expected value to be defined, instead received "undefined"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.