Skip to content

Commit 3d83df3

Browse files
committed
test: add checkbox & switch test
1 parent c8167d6 commit 3d83df3

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
import { mount } from '@vue/test-utils'
3+
import Checkbox from '..'
4+
import focusTest from '../../../tests/shared/focusTest'
5+
6+
describe('Checkbox', () => {
7+
focusTest(Checkbox)
8+
9+
it('responses hover events', () => {
10+
const onMouseEnter = jest.fn()
11+
const onMouseLeave = jest.fn()
12+
13+
const wrapper = mount(Checkbox, {
14+
listeners: {
15+
mouseenter: onMouseEnter,
16+
mouseleave: onMouseLeave,
17+
},
18+
})
19+
20+
wrapper.trigger('mouseenter')
21+
expect(onMouseEnter).toHaveBeenCalled()
22+
23+
wrapper.trigger('mouseleave')
24+
expect(onMouseLeave).toHaveBeenCalled()
25+
})
26+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { mount } from '@vue/test-utils'
2+
import Checkbox from '../index'
3+
4+
describe('CheckboxGroup', () => {
5+
it('should work basically', () => {
6+
const onChange = jest.fn()
7+
const wrapper = mount(
8+
{
9+
render () {
10+
return <Checkbox.Group options={['Apple', 'Pear', 'Orange']} onChange={onChange} />
11+
},
12+
}
13+
)
14+
wrapper.findAll('.ant-checkbox-input').at(0).trigger('change')
15+
expect(onChange).toBeCalledWith(['Apple'])
16+
wrapper.findAll('.ant-checkbox-input').at(1).trigger('change')
17+
expect(onChange).toBeCalledWith(['Apple', 'Pear'])
18+
wrapper.findAll('.ant-checkbox-input').at(2).trigger('change')
19+
expect(onChange).toBeCalledWith(['Apple', 'Pear', 'Orange'])
20+
wrapper.findAll('.ant-checkbox-input').at(1).trigger('change')
21+
expect(onChange).toBeCalledWith(['Apple', 'Orange'])
22+
})
23+
24+
it('does not trigger onChange callback of both Checkbox and CheckboxGroup when CheckboxGroup is disabled', () => {
25+
const onChangeGroup = jest.fn()
26+
27+
const options = [
28+
{ label: 'Apple', value: 'Apple' },
29+
{ label: 'Pear', value: 'Pear' },
30+
]
31+
32+
const groupWrapper = mount(
33+
{
34+
render () {
35+
return <Checkbox.Group options={options} onChange={onChangeGroup} disabled />
36+
},
37+
}
38+
)
39+
groupWrapper.findAll('.ant-checkbox-input').at(0).trigger('change')
40+
expect(onChangeGroup).not.toBeCalled()
41+
groupWrapper.findAll('.ant-checkbox-input').at(1).trigger('change')
42+
expect(onChangeGroup).not.toBeCalled()
43+
})
44+
45+
it('does not prevent onChange callback from Checkbox when CheckboxGroup is not disabled', () => {
46+
const onChangeGroup = jest.fn()
47+
48+
const options = [
49+
{ label: 'Apple', value: 'Apple' },
50+
{ label: 'Orange', value: 'Orange', disabled: true },
51+
]
52+
53+
const groupWrapper = mount(
54+
{
55+
render () {
56+
return <Checkbox.Group options={options} onChange={onChangeGroup} />
57+
},
58+
}
59+
)
60+
groupWrapper.findAll('.ant-checkbox-input').at(0).trigger('change')
61+
expect(onChangeGroup).toBeCalledWith(['Apple'])
62+
groupWrapper.findAll('.ant-checkbox-input').at(1).trigger('change')
63+
expect(onChangeGroup).toBeCalledWith(['Apple'])
64+
})
65+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Switch from '..'
2+
import focusTest from '../../../tests/shared/focusTest'
3+
4+
describe('Switch', () => {
5+
focusTest(Switch)
6+
})

tests/setup.js

+14
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,17 @@ Vue.component('transition-group', {
3131
},
3232
})
3333

34+
Vue.prototype.$emit = function () {
35+
const vm = this
36+
const args = [].slice.call(arguments, 0)
37+
const filterEvent = []
38+
const eventName = args[0]
39+
if (args.length && vm.$listeners[eventName]) {
40+
if (filterEvent.includes(eventName)) {
41+
vm.$emit(eventName, ...args.slice(1))
42+
} else {
43+
vm.$listeners[eventName](...args.slice(1))
44+
}
45+
}
46+
}
47+

0 commit comments

Comments
 (0)