I am currently immersing myself in the world of Jest by following this informative guide. Can you explain the benefits of utilizing the beforeEach
function in Jest?
I have a particular interest in identifying action dispatches. I believe that two segments of code below will yield identical outcomes.
describe('dispatch actions', () => {
const localVue = createLocalVue()
localVue.use(Vuex)
let actions = { increment: jest.fn(), decrement: jest.fn() }
let store = new Vuex.Store({ state: {}, actions })
const wrapper = shallowMount(Counter, { store, localVue })
it('calls "increment" when plus button is clicked', () => {
wrapper.find('button#plus-btn').trigger('click')
expect(actions.increment).toHaveBeenCalled()
})
it('calls "decrement" when minus button is clicked', () => {
wrapper.find('button#minus-btn').trigger('click')
expect(actions.decrement).toHaveBeenCalled()
})
})
describe('dispatch actions', () => {
const localVue = createLocalVue()
localVue.use(Vuex)
let actions
let store
beforeEach(() => {
actions = {
increment: jest.fn(),
decrement: jest.fn()
}
store = new Vuex.Store({
state: {},
actions
})
})
it('triggers "increment" upon clicking the plus button', () => {
const wrapper = shallowMount(Counter, { store, localVue })
wrapper.find('button#plus-btn').trigger('click')
expect(actions.increment).toHaveBeenCalled()
})
it('triggers "decrement" upon clicking the minus button', () => {
const wrapper = shallowMount(Counter, { store, localVue })
wrapper.find('button#minus-btn').trigger('click')
expect(actions.decrement).toHaveBeenCalled()
})
})