Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  35] [ 6]  / answers: 1 / hits: 19890  / 5 Years ago, wed, march 20, 2019, 12:00:00

I'm not great at testing, and new to Jest and Enzyme. I have a Login component that consists of two TextInput components for username and password and a Button component. I am testing each component individually.



I would just like to test that a username and password was returned by onLogin.



Here is the component:



export const onLogin = (user, password) => {
console.log('User', user, 'Password', password)
return [user, password];
};

function Login() {
const [user, setUser] = useState();
const [password, setPassword] = useState();

return (
<LoginWrapper>
<Branding brand={brand.brandName} />
<FormWrapper onSubmit={(e) => { e.preventDefault(); onLogin(user, password) }}>
<Stack>
<TextInput
className=username
type=text
label=Username
onChange={e => setUser(e.target.value)}
/>
</Stack>
<Stack>
<TextInput
className=password
type=password
label=Password
onChange={e => {setPassword(e.target.value); console.log('user', user)}}
/>
</Stack>
<Stack padding=0 align=right>
<Button type=submit>Login</Button>
</Stack>
</FormWrapper>
</LoginWrapper>
);
}

export default Login;


My test:



describe(<Login />, () => {
it(renders text input correctly, () => {
const tree = renderer.create(<ThemeProvider theme={themes.default}><Login /></ThemeProvider>).toJSON();
expect(tree).toMatchSnapshot();
});

it(calls onLogin when button clicked, () => {
const onSubmitMock = jest.fn();

const component = Enzyme.mount(
<ThemeProvider theme={themes.default}><Login onSubmit={onSubmitMock} /></ThemeProvider>
);

component.find(input.username).simulate('change', { target: { value: 'myUser' } })
component.find(input.password).simulate('change', { target: { value: 'myPassword' } })
component.find(form).simulate(submit);

console.log(onClickMock.mock, onSubmitMock.mock)
expect(onSubmitMock).toBeCalled()
});
});


Results:




Expected mock function to have been called, but it was not called.



More From » reactjs

 Answers
36

Your testing approach is correct except for:




  1. In your test, you are mocking a callback function and passing it as a property onSubmit to your component. Then you need to call this function from your component when you submit the form.

  2. Instead, you are calling the onLogin function on your component that does not have any
    repercussion.



In order to fix this, declare the properties on your component function as a parameter, and call props.onSubmit on your form submit.



function Login(props) {
const [user, setUser] = useState();
const [password, setPassword] = useState();

return (
<LoginWrapper>
<Branding brand={brand.brandName} />
<FormWrapper onSubmit={(e) => { e.preventDefault(); props.onSubmit(user, password) }}>
<Stack>
<TextInput
className=username
type=text
label=Username
onChange={(e) => setUser(e.target.value)}
/>
</Stack>
<Stack>
<TextInput
className=password
type=password
label=Password
onChange={(e) => { setPassword(e.target.value) }}
/>
</Stack>
<Stack padding=0 align=right>
<Button type=submit>Login</Button>
</Stack>
</FormWrapper>
</LoginWrapper>
);
}


[#52386] Saturday, March 16, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jayla

Total Points: 14
Total Questions: 96
Total Answers: 123

Location: Greenland
Member since Fri, Jul 31, 2020
4 Years ago
;