LoginSignup
3
3

More than 3 years have passed since last update.

Enzymeでwindow.alertのunit testを実装してみる

Posted at

window.alertにjest.fn()を代入してtestを進めることで実現可能

it('trigger window.alert on click button', () => {
  window.alert = jest.fn()
  const wrapper = shallow(<Button onClick={window.alert('hogehoge')} />)
  wrapper.simulate('click')
  expect(window.alert.mock.calls.length).toBe(1)
  expect(window.alert).toHaveBeenCalledWith('hogehoge')
})

参考

Unit testing your React application with Jest and Enzyme

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3