// tests/unit/example/button.spec.js
import { shallowMount } from '@vue/test-utils';
import ButtonPage from '@/components/example/ButtonPage';
describe('ButtonClick', () => {
it('Page 스냅샷을 찍자', () => {
const wrapper = shallowMount(ButtonPage);
expect(wrapper.html()).expect(wrapper.html()).toMatchSnapshot();
});
});
<template>
<div></div>
</template>
<script>
export default {
data() {
return {};
},
};
</script>
it('기본 영역을 확인하자 : 숫자 나오는 부분', () => {
const wrapper = shallowMount(ButtonPage);
const count = wrapper.find('.count-area').text();
expect(count).toBe('0');
});
<template>
<div>
<div class="count-show">
<span class="count-area">{{ counter }}</span>
</div>
</div>
</template>
<script>
export default {
data() {
return {
counter: 0,
};
},
};
</script>
it('기본 영역을 확인하자 : 버튼 렌더 부분', () => {
const wrapper = shallowMount(ButtonPage);
const btn = wrapper.find('.increment-btn');
expect(btn.text()).toBe('Click Me');
});
<template>
<div>
<div class="count-show">
<span class="count-area">{{ counter }}</span>
</div>
<div>
<button class="increment-btn">Click Me</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
counter: 0,
};
},
};
</script>
it('함수의 로직은 정상 작동 하는가', () => {
const wrapper = shallowMount(ButtonPage);
let res = wrapper.vm.doSomething(2);
expect(res).toBe(true);
});
<template>
<div>
<div class="count-show">
<span class="count-area">{{ counter }}</span>
</div>
<div>
<button class="increment-btn">
Click Me
</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
counter: 0,
};
},
methods: {
doSomething(number) {
return number % 2 == 0 ? true : false;
},
},
};
</script>
it('버튼의 함수가 제대로 호출 되는가', () => {
const mockMethod = jest.spyOn(ButtonPage.methods, 'someMethod');
const wrapper = shallowMount(ButtonPage);
const btn = wrapper.find('.increment-btn');
btn.trigger('click');
expect(mockMethod).toHaveBeenCalled();
});
<template>
<div>
<div class="count-show">
<span class="count-area">{{ counter }}</span>
</div>
<div>
<button class="increment-btn" @click="someMethod">Click Me</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
counter: 0,
};
},
methods: {
someMethod() {
console.log('someMethod');
},
},
};
</script>
click에 someMethod를 붙여주었다.
이제 성공할 것이다.
이 함수가 특정 param을 받거나 data를 전달해야하는 경우도 테스트 해보자.
someMethod("name") 등을 받는다고 해보자.
it('버튼의 함수가 인자와 함께 호출 되는가', () => {
const mockMethod = jest.spyOn(ButtonPage.methods, 'someMethod');
const wrapper = shallowMount(ButtonPage);
const btn = wrapper.find('.increment-btn');
btn.trigger('click');
expect(mockMethod).toBeCalledWith('name');
});
<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>
export default {
data() {
return {
counter: 0,
};
},
methods: {
someMethod() {
console.log('someMethod');
},
},
};
</script>
// tests/unit/example/button.spec.js
import { shallowMount } from '@vue/test-utils';
import ButtonPage from '@/components/example/ButtonPage';
describe('ButtonClick', () => {
it('Page 스냅샷을 찍자', () => {
const wrapper = shallowMount(ButtonPage);
expect(wrapper.html()).toMatchSnapshot();
});
it('기본 영역을 확인하자 : 숫자 나오는 부분', () => {
const wrapper = shallowMount(ButtonPage);
const count = wrapper.find('.count-area').text();
expect(count).toBe('0');
});
it('기본 영역을 확인하자 : 버튼 렌더 부분', () => {
const wrapper = shallowMount(ButtonPage);
const btn = wrapper.find('.increment-btn');
expect(btn.text()).toBe('Click Me');
});
it('함수의 로직은 정상 작동 하는가', () => {
const wrapper = shallowMount(ButtonPage);
let res = wrapper.vm.doSomething(2);
expect(res).toBe(true);
});
it('버튼의 함수가 제대로 호출 되는가', () => {
const mockMethod = jest.spyOn(ButtonPage.methods, 'someMethod');
const wrapper = shallowMount(ButtonPage);
const btn = wrapper.find('.increment-btn');
btn.trigger('click');
expect(mockMethod).toHaveBeenCalled();
});
it('버튼의 함수가 인자와 함께 호출 되는가', () => {
const mockMethod = jest.spyOn(ButtonPage.methods, 'someMethod');
const wrapper = shallowMount(ButtonPage);
const btn = wrapper.find('.increment-btn');
btn.trigger('click');
expect(mockMethod).toBeCalledWith('name');
});
});
// tests/unit/example/button.spec.js
import { shallowMount } from '@vue/test-utils';
import ButtonPage from '@/components/example/ButtonPage';
describe('ButtonClick', () => {
let wrapper;
beforeEach(() => {
wrapper = shallowMount(ButtonPage);
});
...
it('Page 스냅샷을 찍자', () => {
expect(wrapper.html()).toMatchSnapshot();
});
mport { shallowMount } from '@vue/test-utils';
import ButtonPage from '@/components/example/ButtonPage';
describe('ButtonClick', () => {
let wrapper;
let mockSomeMethod;
beforeEach(() => {
mockSomeMethod = jest.spyOn(ButtonPage.methods, 'someMethod');
wrapper = shallowMount(ButtonPage);
});
it('버튼의 함수가 제대로 호출 되는가', () => {
const btn = wrapper.find('.increment-btn');
btn.trigger('click');
expect(mockSomeMethod).toHaveBeenCalled();
});
잘 봤습니다!!