it('기본 영역을 확인하자 : 숫자 나오는 부분', () => {
const count = wrapper.find('.count-area').text();
expect(count).toBe('0');
});
// tests/unit/example/button.spec.js
import { shallowMount, createLocalVue } from '@vue/test-utils';
import ButtonPage from '@/components/example/ButtonPage';
import Vuex from 'vuex';
import { exampleStore } from '@/store/modules/example';
const localVue = createLocalVue();
localVue.use(Vuex);
describe('ButtonClick', () => {
let wrapper;
let mockSomeMethod;
let store;
beforeEach(() => {
store = new Vuex.Store({
modules: {
exampleStore,
},
});
mockSomeMethod = jest.spyOn(ButtonPage.methods, 'someMethod');
wrapper = shallowMount(ButtonPage, { store, localVue });
});
...
});
<template>
<div>
<div class="count-show">
<span class="count-area">{{ counter }}</span>
</div>
<div>
<button class="increment-btn" @click="someMethod('name')">
Click Me
</button>
</div>
</div>
</template>
<script>
import { mapGetters } from 'vuex';
export default {
data() {
return {};
},
computed: {
...mapGetters({ counter: 'GET_COUNTER' }),
},
methods: {
someMethod() {
console.log('someMethod');
},
doSomething(number) {
return number % 2 == 0 ? true : false;
},
},
};
</script>
버튼 클릭 > actions > mutations > state
it('버튼을 클릭하면 counter가 +1 증가되어야한다', () => {
const btn = wrapper.find('.increment-btn');
btn.trigger('click');
expect(store.state.exampleStore.counter).toEqual(1);
});
<template>
<div>
<div class="count-show">
<span class="count-area">{{ counter }}</span>
</div>
<div>
<button class="increment-btn" @click="someMethod('name')">
Click Me
</button>
</div>
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
export default {
data() {
return {};
},
computed: {
...mapGetters({ counter: 'GET_COUNTER' }),
},
methods: {
...mapActions({ increment: 'SET_COUNTER' }),
someMethod() {
this.increment();
console.log('someMethod');
},
doSomething(number) {
return number % 2 == 0 ? true : false;
},
},
};
</script>
beforeEach(() => {
exampleStore.state.counter = 0;
store = new Vuex.Store({
modules: {
exampleStore,
},
});
mockSomeMethod = jest.spyOn(ButtonPage.methods, 'someMethod');
wrapper = shallowMount(ButtonPage, { store, localVue });
});