Я изучаю Jest с этим руководством .В чем преимущество использования функции beforeEach
в Jest?
Я хочу обнаружить диспетчеризацию действий.Я думаю, что два из следующих кодов будут иметь одинаковое поведение.
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('dispatches "increment" when plus button is pressed', () => {
wrapper.find('button#plus-btn').trigger('click')
expect(actions.increment).toHaveBeenCalled()
})
it('dispatches "decrement" when minus button is pressed', () => {
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('dispatches "increment" when plus button is pressed', () => {
const wrapper = shallowMount(Counter, { store, localVue })
wrapper.find('button#plus-btn').trigger('click')
expect(actions.increment).toHaveBeenCalled()
})
it('dispatches "decrement" when minus button is pressed', () => {
const wrapper = shallowMount(Counter, { store, localVue })
wrapper.find('button#minus-btn').trigger('click')
expect(actions.decrement).toHaveBeenCalled()
})
})