Jest/Enzyme shallow testing React stateless component - wrapper.find() not working

miuosh

I have problem with testing my React SFC. I'd like to test if 'PointDetailConfig' has two 'DayHours' components (can be mocked). I don't know why wrapper.find() not finding 'DayHours'. It worked with React statefull component. Received value indicate that 'DayHours' is props.children. Should I assert wrapper.props().children? Thanks for any tips how to test it properly.

expect(received).toHaveLength(length)

    Expected value to have length:
      2
    Received:
      {Symbol(enzyme.__root__): {Symbol(enzyme.__root__): [Circular], Symbol(enzyme.__unrendered__): <PointDetailConfig increaseCurrentDayPeriod={[]} increaseNextDayPeriod={[]} reduceCurrentDayPeriod={[]} reduceNextDayPeriod={[]} />, Symbol(enzyme.__renderer__): {"batchedUpdates": [Function batchedUpdates], "getNode": [Function getNode], "render": [Function render], "simulateEvent": [Function simulateEvent], "unmount": [Function unmount]}, Symbol(enzyme.__node__): {"instance": null, "key": undefined, "nodeType": "host", "props": {"children": [<DayHours increaseCurrentDayPeriod={[]} increaseHours={[]} increaseNextDayPeriod={[]} name="some name" reduceCurrentDayPeriod={[]} reduceHours={[]} reduceNextDayPeriod={[]} />, <DayHours increaseCurrentDayPeriod={[]} increaseHours={[]} increaseNextDayPeriod={[]} name="some name" reduceCurrentDayPeriod={[]} reduceHours={[]} reduceNextDayPeriod={[]} />]}, "ref": null, "rendered": [{"instance": null, "key": undefined, "nodeType": "function", "props": {"increaseCurrentDayPeriod": [], "increaseHours": [], "increaseNextDayPeriod": [], "name": "Doba bieżąca", "reduceCurrentDayPeriod": [], "reduceHours": [], "reduceNextDayPeriod": []}, "ref": null, "rendered": null, "type": [Function DayHours]}, {"instance": null, "key": undefined, "nodeType": "function", "props": {"increaseCurrentDayPeriod": [], "increaseHours": [], "increaseNextDayPeriod": [], "name": "Doba następna", "reduceCurrentDayPeriod": [], "reduceHours": [], "reduceNextDayPeriod": []}, "ref": null, "rendered": null, "type": [Function DayHours]}], "type": "div"}, Symbol(enzyme.__nodes__): [{"instance": null, "key": undefined, "nodeType": "host", "props": {"children": [<DayHours increaseCurrentDayPeriod={[]} increaseHours={[]} increaseNextDayPeriod={[]} name="some name" reduceCurrentDayPeriod={[]} reduceHours={[]} reduceNextDayPeriod={[]} />, <DayHours increaseCurrentDayPeriod={[]} increaseHours={[]} increaseNextDayPeriod={[]} name="some name" reduceCurrentDayPeriod={[]} reduceHours={[]} reduceNextDayPeriod={[]} />]}, "ref": null, "rendered": [{"instance": null, "key": undefined, "nodeType": "function", "props": {"increaseCurrentDayPeriod": [], "increaseHours": [], "increaseNextDayPeriod": [], "name": "some name", "reduceCurrentDayPeriod": [], "reduceHours": [], "reduceNextDayPeriod": []}, "ref": null, "rendered": null, "type": [Function DayHours]}, {"instance": null, "key": undefined, "nodeType": "function", "props": {"increaseCurrentDayPeriod": [], "increaseHours": [], "increaseNextDayPeriod": [], "name": "some name", "reduceCurrentDayPeriod": [], "reduceHours": [], "reduceNextDayPeriod": []}, "ref": null, "rendered": null, "type": [Function DayHours]}], "type": "div"}], Symbol(enzyme.__options__): {"adapter": {"options": {"lifecycles": {"componentDidUpdate": {"prevContext": true}}, "supportPrevContextArgumentOfComponentDidUpdate": true}}, "attachTo": undefined, "disableLifecycleMethods": true, "hydrateIn": undefined}}, Symbol(enzyme.__unrendered__): null, Symbol(enzyme.__renderer__): {"batchedUpdates": [Function batchedUpdates], "getNode": [Function getNode], "render": [Function render], "simulateEvent": [Function simulateEvent], "unmount": [Function unmount]}, Symbol(enzyme.__node__): undefined, Symbol(enzyme.__nodes__): [], Symbol(enzyme.__options__): {"adapter": {"options": {"lifecycles": {"componentDidUpdate": {"prevContext": true}}, "supportPrevContextArgumentOfComponentDidUpdate": true}}, "attachTo": undefined, "disableLifecycleMethods": true, "hydrateIn": undefined}}
    received.length:
      0

PointDetailConfig.test.js

import React from 'react';
    import renderer from 'react-test-renderer';
    import { shallow, configure, mount } from 'enzyme';
    import Adapter from 'enzyme-adapter-react-15';
    
    import PointDetailConfig from '../PointDetailConfig/PointDetailConfig';
    
    
    configure({ adapter: new Adapter() });
    
    // jest.mock('../PointDetailConfig/DayHours', () => {
    //     return 'DayHours mock';
    // });
    
    describe('PointDetailConfig', () => {
        let wrapper;
        let testProps;
        
        beforeEach(() => {
            testProps = {
    
            };
            wrapper = shallow(<PointDetailConfig {...testProps} />, { disableLifecycleMethods: true });
        });
    
        it('should render correctly', () => {
            const tree = renderer.create(<PointDetailConfig {...testProps} />);
            expect(tree.toJSON()).toMatchSnapshot();
        });
    
        it('should render DayHours', () => {
            console.log(wrapper.find('DayHours'));
            expect(wrapper.find('DayHours')).toHaveLength(2);
        });
    });

PointDetailConfig.js

    import React from 'react';
    import PropTypes from 'prop-types';
    import DayHours from './DayHours';
    
    
    function PointDetailConfig(props) {
        const currentDayProps = {
            increaseHours: props.increaseCurrentDayPeriod,
            reduceHours: props.reduceCurrentDayPeriod,
            name: 'some name',
            ...props
        };
        const nextDayProps = {
            increaseHours: props.increaseNextDayPeriod,
            reduceHours: props.reduceNextDayPeriod,
            name: 'some name',
            ...props
        };
        return (
            <div>
                <DayHours {...currentDayProps} />
                <DayHours {...nextDayProps} />
            </div>
        );
    }
    
    PointDetailConfig.defaultProps = {
        //default props
    };
    
    PointDetailConfig.propTypes = {
        //define proptypes
    };
    
    export default PointDetailConfig;

Shubham Khatri

Since you expect to find the component DayHours import it in the test suite and pass it as a component to find instead of a string.

import React from 'react';
    import renderer from 'react-test-renderer';
    import { shallow, configure, mount } from 'enzyme';
    import Adapter from 'enzyme-adapter-react-15';
    import DayHours from path/to/DayHours;
    import PointDetailConfig from '../PointDetailConfig/PointDetailConfig';
    
    
    configure({ adapter: new Adapter() });
   
    
    describe('PointDetailConfig', () => {
        let wrapper;
        let testProps;
        
        beforeEach(() => {
            testProps = {
    
            };
            wrapper = shallow(<PointDetailConfig {...testProps} />, { disableLifecycleMethods: true });
        });
    
        it('should render correctly', () => {
            const tree = renderer.create(<PointDetailConfig {...testProps} />);
            expect(tree.toJSON()).toMatchSnapshot();
        });
    
        it('should render DayNominHours', () => {
            console.log(wrapper.find(DayHours));
            expect(wrapper.find(DayHours)).toHaveLength(2);
        });
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React testing library wrapper component

Connect not working with StateLess component in Redux-react

onClick is not working in stateless functional component React

retrieving React child component from Higher Order Component when testing Stateless Functional Component

Stateless component React router

React - toggle in stateless component

react stateless component with connect

React onClick on stateless component

React contenteditable in stateless component

Stateless Functional Component - React

Testing wrapper.props shallow render is undefined

Error rendering stateless component: "Can't find variable: React"

Enzyme/Jest React Testing - Shallow connected component with react-redux > 6

Unit testing React stateless component wrapped with withRouter() in Enzyme, how to pass match parameter

Clear input in stateless React component

React: PropTypes in stateless functional component

React, observer stateless component with typescript

react stateless component with onClick issue

Passing array into React stateless component

Make wrapper component in React

React Component Wrapper

Creating a wrapper for a react component

react-router v4 mocha testing with enzyme shallow not working

Selecting an element in React in a stateless function component in React?

@testing-library/React : Clicking outside of component not working

React: what determines that a Stateless React component is actually a React component?

Jest/Enzyme Unit Testing: How to pass store to shallow component that uses redux 4 and react-redux 6 connect function

TypeScript compiler error with React Stateless Function component

How to define defaultProps for a stateless react component in typescript?

TOP Ranking

HotTag

Archive