Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
781 views
in Technique[技术] by (71.8m points)

reactjs - How can I fix the Jest test to find (render) the node in order to .simulate("click") when ThemeProvider is used from react jss?

This is a very simple question but I just could not figure it out. I am trying to run a very basic test on Jest on a component but I get the following error:

Method “simulate” is meant to be run on 1 node. 0 found instead.

Here is how the test is written:

const theme = {
    palette: {
        primary: "#f5f5f5",
        secondary: "#2b4450",
        tertiary: "#871111"
    },
    spacing: ["0px", "4px", "8px", "16px", "32px", "64px"],
}; 

 const wrapper = shallow(<ThemeProvider theme={theme}>
 <MonthPicker //other props passed in />
  </ThemeProvider>);
    
        test("renders correct heading that changes with next/previous", () => {
        wrapper.find('#button-next').simulate('click');
        expect(mockSetDateObject).toHaveBeenCalled();
    
        wrapper.find('#button-previous').simulate('click');
        expect(mockSetDateObject).toHaveBeenCalled();
    
        const h2 = wrapper.find('#month-label').text();
        expect(h2).toEqual("January 2021");
    
        expect(toJson(wrapper)).toMatchSnapshot();
    });

The tests worked perfectly before ThemeProvider was added to the code and the test will throw an error if I remove it and the theme passed in to it. I am not sure if this error is occurring due to shallow testing or am I missing a step before .find()? Any help would be much appreciated.

question from:https://stackoverflow.com/questions/66063143/how-can-i-fix-the-jest-test-to-find-render-the-node-in-order-to-simulatecli

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...