test: Improve code, coverage for ProgressBar & Forms (#230)

This commit is contained in:
Ganjar Setia M
2020-09-08 15:04:25 +07:00
committed by GitHub
parent e48528fb3d
commit 76f1265702
2 changed files with 56 additions and 10 deletions
+34 -2
View File
@@ -1,16 +1,18 @@
import Vue from 'vue' import Vue from 'vue'
import { shallowMount } from '@vue/test-utils' import { mount } from '@vue/test-utils'
import CoreuiVue from '@coreui/vue' import CoreuiVue from '@coreui/vue'
import Forms from '@/views/base/Forms' import Forms from '@/views/base/Forms'
Vue.use(CoreuiVue) Vue.use(CoreuiVue)
describe('Forms.vue', () => { describe('Forms.vue', () => {
// use mount. So the method applied to child components can be tested
const wrapper = mount(Forms)
it('has a name', () => { it('has a name', () => {
expect(Forms.name).toBe('Forms') expect(Forms.name).toBe('Forms')
}) })
it('is Forms', () => { it('is Forms', () => {
const wrapper = shallowMount(Forms)
expect(wrapper.findComponent(Forms)).toBeTruthy() expect(wrapper.findComponent(Forms)).toBeTruthy()
}) })
// render random chackboxes // render random chackboxes
@@ -18,4 +20,34 @@ describe('Forms.vue', () => {
// const wrapper = shallowMount(Forms) // const wrapper = shallowMount(Forms)
// expect(wrapper.element).toMatchSnapshot() // expect(wrapper.element).toMatchSnapshot()
// }) // })
// the test action is in Forms.vue, with valid & invalid input
// so just render it, it will executed
it('should execute validator() on mount', (done) => {
expect(wrapper).toBeDefined()
done()
})
// simulate input to make it invalid. This will test <CInput :is-valid="validator" />
it('should not pass validator()', (done) => {
// need to find best selector for the input, this one from rendered HTML
const input = wrapper.findAll('input').at(71)
input.setValue('Hai')
wrapper.vm.$nextTick(() => {
expect(input.classes()).toContain('is-invalid')
// use callback, because its error when using async await
done()
})
})
it('should pass validator()', (done) => {
const input = wrapper.find('div > div:nth-child(3) > div:nth-child(2) > div > div > form > div:nth-child(2) > input')
input.setValue('Hello World')
wrapper.vm.$nextTick(() => {
expect(input.classes()).toContain('is-valid')
done()
})
})
}) })
+22 -8
View File
@@ -1,5 +1,5 @@
import Vue from 'vue' import Vue from 'vue'
import { mount, shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import CoreuiVue from '@coreui/vue' import CoreuiVue from '@coreui/vue'
import ProgressBars from '@/views/base/ProgressBars' import ProgressBars from '@/views/base/ProgressBars'
@@ -8,6 +8,9 @@ Vue.use(CoreuiVue)
jest.useFakeTimers() jest.useFakeTimers()
describe('ProgressBars.vue', () => { describe('ProgressBars.vue', () => {
// mount it once, to make test efficient & faster
const wrapper = shallowMount(ProgressBars)
it('has a name', () => { it('has a name', () => {
expect(ProgressBars.name).toBe('ProgressBars') expect(ProgressBars.name).toBe('ProgressBars')
}) })
@@ -18,23 +21,34 @@ describe('ProgressBars.vue', () => {
expect(typeof ProgressBars.data).toMatch('function') expect(typeof ProgressBars.data).toMatch('function')
}) })
it('is Vue instance', () => { it('is Vue instance', () => {
const wrapper = mount(ProgressBars)
expect(wrapper.vm).toBeTruthy() expect(wrapper.vm).toBeTruthy()
}) })
it('is ProgressBars', () => { it('is ProgressBars', () => {
const wrapper = mount(ProgressBars)
expect(wrapper.findComponent(ProgressBars)).toBeTruthy() expect(wrapper.findComponent(ProgressBars)).toBeTruthy()
}) })
test('renders correctly', () => { test('renders correctly', () => {
const wrapper = shallowMount(ProgressBars) // mock Math.random() to always return 1
jest.spyOn(global.Math, 'random').mockReturnValue(1)
expect(wrapper.element).toMatchSnapshot() expect(wrapper.element).toMatchSnapshot()
})
it('should be destroyed', () => { // restore Math.random()
const wrapper = mount(ProgressBars) jest.spyOn(global.Math, 'random').mockRestore()
wrapper.destroy()
}) })
it('should have methods', () => { it('should have methods', () => {
expect(typeof ProgressBars.methods.clicked ).toEqual('function') expect(typeof ProgressBars.methods.clicked ).toEqual('function')
expect(ProgressBars.methods.clicked()).toBeUndefined() expect(ProgressBars.methods.clicked()).toBeUndefined()
}) })
it('should execute mounted', () => {
// mock interval 2000 ms
jest.advanceTimersByTime(2000);
expect(setInterval).toHaveBeenCalled();
expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 2000)
})
// this test should be the last
it('should be destroyed', () => {
wrapper.destroy()
})
}) })