You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// __tests__/login.jsimportReactfrom'react'import{render,fireEvent}from'@testing-library/react'importLoginfrom'../login'test('calls onSubmit with the username and password when submit is clicked',()=>{consthandleSubmit=jest.fn()const{getByLabelText, getByText}=render(<LoginonSubmit={handleSubmit}/>)constuser={username: 'michelle',password: 'smith'}fireEvent.change(getByLabelText(/username/i),{target: {value: user.username},})fireEvent.change(getByLabelText(/password/i),{target: {value: user.password},})fireEvent.click(getByText(/submit/i))expect(handleSubmit).toHaveBeenCalledTimes(1)expect(handleSubmit).toHaveBeenCalledWith(user)})test('shows an error message when submit is clicked and no username is provided',()=>{consthandleSubmit=jest.fn()const{getByLabelText, getByText, getByRole}=render(<LoginonSubmit={handleSubmit}/>,)fireEvent.change(getByLabelText(/password/i),{target: {value: 'anything'}})fireEvent.click(getByText(/submit/i))consterrorMessage=getByRole('alert')expect(errorMessage).toHaveTextContent(/usernameisrequired/i)expect(handleSubmit).not.toHaveBeenCalled()})test('shows an error message when submit is clicked and no password is provided',()=>{consthandleSubmit=jest.fn()const{getByLabelText, getByText, getByRole}=render(<LoginonSubmit={handleSubmit}/>,)fireEvent.change(getByLabelText(/username/i),{target: {value: 'anything'}})fireEvent.click(getByText(/submit/i))consterrorMessage=getByRole('alert')expect(errorMessage).toHaveTextContent(/passwordisrequired/i)expect(handleSubmit).not.toHaveBeenCalled()})
Apply AHA (Avoid Hasty Abstractions)
importReactfrom'react'import{render,fireEvent}from'@testing-library/react'importLoginfrom'../login'// here we have a bunch of setup functions that compose together for our test cases// I only recommend doing this when you have a lot of tests that do the same thing.// I'm including it here only as an example. These tests don't necessitate this// much abstraction. Read more: https://kcd.im/aha-testingfunctionsetup(){consthandleSubmit=jest.fn()constutils=render(<LoginonSubmit={handleSubmit}/>)constuser={username: 'michelle',password: 'smith'}constchangeUsernameInput=value=>fireEvent.change(utils.getByLabelText(/username/i),{target: {value}})constchangePasswordInput=value=>fireEvent.change(utils.getByLabelText(/password/i),{target: {value}})constclickSubmit=()=>fireEvent.click(utils.getByText(/submit/i))return{
...utils,
handleSubmit,
user,
changeUsernameInput,
changePasswordInput,
clickSubmit,}}functionsetupSuccessCase(){constutils=setup()utils.changeUsernameInput(utils.user.username)utils.changePasswordInput(utils.user.password)utils.clickSubmit()returnutils}functionsetupWithNoPassword(){constutils=setup()utils.changeUsernameInput(utils.user.username)utils.clickSubmit()consterrorMessage=utils.getByRole('alert')return{...utils, errorMessage}}functionsetupWithNoUsername(){constutils=setup()utils.changePasswordInput(utils.user.password)utils.clickSubmit()consterrorMessage=utils.getByRole('alert')return{...utils, errorMessage}}test('calls onSubmit with the username and password',()=>{const{handleSubmit, user}=setupSuccessCase()expect(handleSubmit).toHaveBeenCalledTimes(1)expect(handleSubmit).toHaveBeenCalledWith(user)})test('shows an error message when submit is clicked and no username is provided',()=>{const{handleSubmit, errorMessage}=setupWithNoUsername()expect(errorMessage).toHaveTextContent(/usernameisrequired/i)expect(handleSubmit).not.toHaveBeenCalled()})test('shows an error message when password is not provided',()=>{const{handleSubmit, errorMessage}=setupWithNoPassword()expect(errorMessage).toHaveTextContent(/passwordisrequired/i)expect(handleSubmit).not.toHaveBeenCalled()})
What about cleanup?
importReactfrom'react'import{render,fireEvent,cleanup}from'@testing-library/react'importLoginfrom'../login'afterEach(()=>cleanup())test('example 1',()=>{consthandleSubmit=jest.fn()const{getByLabelText}=render(<LoginonSubmit={handleSubmit}/>)fireEvent.change(getByLabelText(/username/i),{target: {value: 'mary'}})fireEvent.change(getByLabelText(/password/i),{target: {value: 'little lamb'},})// more test here})test('example 2',()=>{consthandleSubmit=jest.fn()const{getByLabelText}=render(<LoginonSubmit={handleSubmit}/>)fireEvent.change(getByLabelText(/username/i),{target: {value: 'mary'}})// more test here})