profile
viewpoint

Ask questions Error on unit test : Cannot read property 'Symbol(Symbol.iterator)' of undefined

@gaearon i have a component that has a useState hooks as below :

component is so big , because of that , i just write some of the problem .

function Login(props){
/*...*.
    const [, dispatch] = useLoginStateValue();
    
return(
/*...*/
)

}

and i have a test for this component

jest.mock("../../Services/login")

it("mock login request testing", (done) => {
    act(() => {
        const wrapper = mount(
            <MuiThemeProvider theme={dark}>
                <Login theme={{isResponsive: true}}/>
            </MuiThemeProvider>);

        wrapper.setState({email: "@@22WWee"})
        wrapper.setState({password: "test@test.com"})
        wrapper.find('button#authenticate').simulate('click');

        setTimeout(() => {
            wrapper.update();
            wrapper.setState({alert: "Bad credentials"})
            expect(wrapper.instance().state.alert).toEqual("Bad credentials")
            done()
        })
    })
})

when i run test , it shows error : Error: Uncaught [TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined]

after removing const [, dispatch] = useLoginStateValue(); everything is ok , how can i use dispatch , and LoginStateValue without error in test ?

facebook/react

Answer questions threepointone

My guess is that useLoginStateValue doesn't return an array. Would recommend using a community resource for questions like this https://reactjs.org/community/support.html

useful!
source:https://uonfu.com/
Github User Rank List