Skip to content

Commit 76f1265

Browse files
author
Ganjar Setia M
authored
test: Improve code, coverage for ProgressBar & Forms (#230)
1 parent e48528f commit 76f1265

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

tests/unit/views/base/Forms.spec.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,53 @@
11
import Vue from 'vue'
2-
import { shallowMount } from '@vue/test-utils'
2+
import { mount } from '@vue/test-utils'
33
import CoreuiVue from '@coreui/vue'
44
import Forms from '@/views/base/Forms'
55

66
Vue.use(CoreuiVue)
77

88
describe('Forms.vue', () => {
9+
// use mount. So the method applied to child components can be tested
10+
const wrapper = mount(Forms)
11+
912
it('has a name', () => {
1013
expect(Forms.name).toBe('Forms')
1114
})
1215
it('is Forms', () => {
13-
const wrapper = shallowMount(Forms)
1416
expect(wrapper.findComponent(Forms)).toBeTruthy()
1517
})
1618
// render random chackboxes
1719
// test('renders correctly', () => {
1820
// const wrapper = shallowMount(Forms)
1921
// expect(wrapper.element).toMatchSnapshot()
2022
// })
23+
24+
// the test action is in Forms.vue, with valid & invalid input
25+
// so just render it, it will executed
26+
it('should execute validator() on mount', (done) => {
27+
expect(wrapper).toBeDefined()
28+
done()
29+
})
30+
31+
// simulate input to make it invalid. This will test <CInput :is-valid="validator" />
32+
it('should not pass validator()', (done) => {
33+
// need to find best selector for the input, this one from rendered HTML
34+
const input = wrapper.findAll('input').at(71)
35+
input.setValue('Hai')
36+
37+
wrapper.vm.$nextTick(() => {
38+
expect(input.classes()).toContain('is-invalid')
39+
// use callback, because its error when using async await
40+
done()
41+
})
42+
})
43+
44+
it('should pass validator()', (done) => {
45+
const input = wrapper.find('div > div:nth-child(3) > div:nth-child(2) > div > div > form > div:nth-child(2) > input')
46+
input.setValue('Hello World')
47+
48+
wrapper.vm.$nextTick(() => {
49+
expect(input.classes()).toContain('is-valid')
50+
done()
51+
})
52+
})
2153
})
+22-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue from 'vue'
2-
import { mount, shallowMount } from '@vue/test-utils';
2+
import { shallowMount } from '@vue/test-utils';
33
import CoreuiVue from '@coreui/vue'
44
import ProgressBars from '@/views/base/ProgressBars'
55

@@ -8,6 +8,9 @@ Vue.use(CoreuiVue)
88
jest.useFakeTimers()
99

1010
describe('ProgressBars.vue', () => {
11+
// mount it once, to make test efficient & faster
12+
const wrapper = shallowMount(ProgressBars)
13+
1114
it('has a name', () => {
1215
expect(ProgressBars.name).toBe('ProgressBars')
1316
})
@@ -18,23 +21,34 @@ describe('ProgressBars.vue', () => {
1821
expect(typeof ProgressBars.data).toMatch('function')
1922
})
2023
it('is Vue instance', () => {
21-
const wrapper = mount(ProgressBars)
2224
expect(wrapper.vm).toBeTruthy()
2325
})
2426
it('is ProgressBars', () => {
25-
const wrapper = mount(ProgressBars)
2627
expect(wrapper.findComponent(ProgressBars)).toBeTruthy()
2728
})
2829
test('renders correctly', () => {
29-
const wrapper = shallowMount(ProgressBars)
30+
// mock Math.random() to always return 1
31+
jest.spyOn(global.Math, 'random').mockReturnValue(1)
32+
3033
expect(wrapper.element).toMatchSnapshot()
31-
})
32-
it('should be destroyed', () => {
33-
const wrapper = mount(ProgressBars)
34-
wrapper.destroy()
34+
35+
// restore Math.random()
36+
jest.spyOn(global.Math, 'random').mockRestore()
3537
})
3638
it('should have methods', () => {
3739
expect(typeof ProgressBars.methods.clicked ).toEqual('function')
3840
expect(ProgressBars.methods.clicked()).toBeUndefined()
3941
})
42+
it('should execute mounted', () => {
43+
// mock interval 2000 ms
44+
jest.advanceTimersByTime(2000);
45+
46+
expect(setInterval).toHaveBeenCalled();
47+
expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 2000)
48+
})
49+
50+
// this test should be the last
51+
it('should be destroyed', () => {
52+
wrapper.destroy()
53+
})
4054
})

0 commit comments

Comments
 (0)