enzyme icon indicating copy to clipboard operation
enzyme copied to clipboard

Error with setting context for stateless component using FullDom rendering(mount)

Open nikunjgami1991 opened this issue 9 years ago • 6 comments

Hi,

I have one stateless component which is accessing context onClick event of button.

Now I am writing unit test case for same using full dom rendering method. I have created wrapper and after that I am setting context. but I am getting below error. I am not getting why this error is coming.

Test case code

    it('Checking main Tag-Snapshot for row with selectedRating when RatingButton will be clicked', () => {

        let selectedRating = [[false,false, false,false, false],[false,false, false,false, false]];
        let rowIndex = 0;

        //render component for even number row with selectedRating
        let wrapper = mount(<RatingButton index={rowIndex} selectedRating={selectedRating} rating={rating}/>);        
        wrapper.setContext({ ratingSelected: React.PropTypes.func });
        //wrapper.setContext({ ratingSelected: function(){}); //tried this also but, it is also not working.
        const ratingButton = wrapper.find('button').first();
        ratingButton.simulate('click', 0,rowIndex);

        expect(selectedRating[0][0]).toEqual(true);
    });

Component code

import React from 'react';
import {RatingButtonContainer, RatingButtonTextContainer, RatingButtonStyle} from './../Styles/RatingInputStyles';

const RatingButton = (props, context) => {
    let ratingButton = null, selectedRow;    

    const ratingSelected = (index, rowIndex) => {
         
        //This line is causing error in test case 
        context.ratingSelected(index,rowIndex);

        selectedRow = props.selectedRating[rowIndex];
        selectedRow.forEach((content,selectedRowIndex)=>{
            if(selectedRowIndex === index-1){
                selectedRow[index-1] = true;
            }
            else{
                selectedRow[selectedRowIndex] = false;
            }
        })

        props.selectedRating[rowIndex] = selectedRow;                    
    }
   
    if(props.rating){   
        ratingButton = props.rating.map((content,index) => {
            if(index > 0){
                const RatingButtonInput = RatingButtonStyle(props.selectedRating, index, props.index);
                let ratingValue;
                if(props.calDifferent === true){
                    ratingValue = 1;
                }else{
                    ratingValue = index;
                }
                return (
                    <RatingButtonTextContainer key={index}>                       
                        <RatingButtonInput onClick={()=> ratingSelected(index,props.index)}>{ratingValue}</RatingButtonInput>
                    </RatingButtonTextContainer>
                );                    
            }
            else{
                return false;
            }   
        });
    }
    return(
         <RatingButtonContainer value={props.index}>{ratingButton}</RatingButtonContainer> 
    )
}

RatingButton.contextTypes= {
    ratingSelected: React.PropTypes.func
}
export default RatingButton;

Error

    ShallowWrapper::setContext() can only be called on a wrapper that was originally passed a context option

      at ReactWrapper.setContext (node_modules\enzyme\build\ReactWrapper.js:388:17)
      at Object.<anonymous> (src\Components\__test__\RatingButton.test.js:91:17)

Please guide me if I am missing anything in test case code.

Thanks in advance.

Regards, Nikunj

nikunjgami1991 avatar Dec 29 '16 08:12 nikunjgami1991

Instead of using setContext initially, do this:

const context = { ratingSelected: React.PropTypes.func };
let wrapper = mount(<RatingButton index={rowIndex} selectedRating={selectedRating} rating={rating}/>, { context ));

ljharb avatar Dec 29 '16 08:12 ljharb

Hi,

Thank you for your reply.

It is not working. I have done as you suggested but, getting below error and it is not creating wrapper due to it.

//Error Msg ` TypeError: document.createElement is not a function

  at Object.ReactTestUtils.renderIntoDocument (node_modules\react-dom\lib\ReactTestUtils.js:73:24)
  at renderWithOptions (node_modules\enzyme\build\react-compat.js:187:26)
  at new ReactWrapper (node_modules\enzyme\build\ReactWrapper.js:94:59)
  at mount (node_modules\enzyme\build\mount.js:19:10)
  at Object.<anonymous> (src\Components\__test__\RatingButton.test.js:85:67)

` Please suggest other solution if available.

Thank you, Nikunj.

nikunjgami1991 avatar Dec 29 '16 09:12 nikunjgami1991

If you're using mount, you must have a DOM available - usually by running in a browser, or using jsdom. Do you? If not, you'll want to use shallow instead.

ljharb avatar Dec 29 '16 09:12 ljharb

Yeah!!!!!!!!! It worked for me. Thanks you.

By mistake I have commented code for generating DOM using jsdom. So, it was not working.

Now I have one question that I am getting below warning message.

//Warning Message console.error node_modules\react\node_modules\fbjs\lib\warning.js:36 Warning: You are manually calling a React.PropTypes validation function for the 0prop on<>. This is deprecated and will not work in production with the next major versio n. You may be seeing this warning due to a third-party PropTypes library. See https://fb.me/react-warning-dont-call-proptypes for details.

Link that has been mentioned is not working.

can you please suggest why this warning is appearing?

nikunjgami1991 avatar Dec 29 '16 09:12 nikunjgami1991

That warning is part of React 15.3 and later - see the link it gives for details.

ljharb avatar Dec 29 '16 09:12 ljharb

ok. Thank you for your help.

nikunjgami1991 avatar Dec 29 '16 09:12 nikunjgami1991