Skip to content

Instantly share code, notes, and snippets.

@luzeduardo
Created September 15, 2018 00:00
Show Gist options
  • Save luzeduardo/9e658d4d425b057177ec6f6068fa755d to your computer and use it in GitHub Desktop.
Save luzeduardo/9e658d4d425b057177ec6f6068fa755d to your computer and use it in GitHub Desktop.
import React from 'react'
import Counter from '../Counter'
import {shallow} from 'enzyme'
import { wrap } from 'module';
describe('<Counter />', () => {
it('should start with zero', () => {
const wrapper = shallow(<Counter />)
expect(wrapper.state().counter).toBe(0)
})
it('should start counter with initial props value when defined', () => {
const wrapper = shallow(<Counter initial={3}/>)
expect(wrapper.state().counter).toBe(3)
})
it('should call componentWillReceiveProps when start with initial props value', ()=> {
const spy = jest.spyOn(Counter.prototype, 'componentWillReceiveProps')
const wrapper = shallow(<Counter initial={2}/>)
wrapper.setProps({
initial: 1
})
wrapper.setProps({
initial: 2
})
expect(spy).toHaveBeenCalled()
expect(Counter.prototype.componentWillReceiveProps.mock.calls.length).toBe(2)
})
it('should get correct state after increment', () => {
const wrapper = shallow(<Counter initial={3}/>)
wrapper.instance().increment()
wrapper.update()
expect(wrapper.state().counter).toBe(4)
})
it('should render inside counter class', () => {
const wrapper = shallow(<Counter/>)
expect(wrapper.find('.counter').text()).toBe("0")
})
it('should call correctly on click', () => {
const wrapper = shallow(<Counter initial={12}/>)
expect(wrapper.find('button[name="increment"]').length).toBe(1)
wrapper.find('button[name="increment"]').simulate('click')
wrapper.update()
expect(wrapper.state().counter).toBe(13)
})
it('should call expected arrow function method on button click', () => {
const wrapper = shallow(<Counter initial={2}/>)
wrapper.instance().reset = jest.fn()
wrapper.instance().forceUpdate()
wrapper.find('button[name="reset"]').simulate('click')
expect(wrapper.instance().reset).toHaveBeenCalled()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment