2019-01-12 03:33:27 +00:00
|
|
|
import Switch from '..';
|
2020-03-07 11:45:13 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
2019-01-12 03:33:27 +00:00
|
|
|
import focusTest from '../../../tests/shared/focusTest';
|
2020-03-07 11:45:13 +00:00
|
|
|
import { resetWarned } from '../../_util/warning';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2021-04-21 06:25:35 +00:00
|
|
|
import { ref } from 'vue';
|
2021-07-07 13:35:41 +00:00
|
|
|
import { asyncExpect } from '@/tests/utils';
|
2018-05-20 13:42:23 +00:00
|
|
|
|
|
|
|
describe('Switch', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
focusTest(Switch);
|
2020-03-07 11:45:13 +00:00
|
|
|
mountTest(Switch);
|
|
|
|
|
|
|
|
it('should has click wave effect', async () => {
|
2021-04-11 01:57:20 +00:00
|
|
|
const wrapper = mount({
|
2021-04-21 06:25:35 +00:00
|
|
|
setup() {
|
|
|
|
const checked = ref(false);
|
|
|
|
return () => {
|
|
|
|
return (
|
|
|
|
<Switch
|
|
|
|
checked={checked.value}
|
|
|
|
onChange={() => (checked.value = !checked.value)}
|
|
|
|
></Switch>
|
|
|
|
);
|
|
|
|
};
|
2021-04-11 01:57:20 +00:00
|
|
|
},
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
wrapper.find('.ant-switch').trigger('click');
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('warning if set `value`', () => {
|
|
|
|
resetWarned();
|
|
|
|
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2021-04-11 01:57:20 +00:00
|
|
|
mount({
|
|
|
|
render() {
|
|
|
|
return <Switch value="" />;
|
|
|
|
},
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antdv: Switch] `value` is not validate prop, do you mean `checked`?',
|
|
|
|
);
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
2021-07-07 13:35:41 +00:00
|
|
|
|
|
|
|
it('customize checked value should work', async () => {
|
|
|
|
resetWarned();
|
|
|
|
const checked = ref(1);
|
|
|
|
const onUpdate = val => (checked.value = val);
|
|
|
|
const wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Switch
|
|
|
|
{...{ 'onUpdate:checked': onUpdate }}
|
|
|
|
checked={checked.value}
|
2021-07-07 13:51:03 +00:00
|
|
|
unCheckedValue={1}
|
2021-07-07 13:35:41 +00:00
|
|
|
checkedValue={2}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await asyncExpect(() => {
|
|
|
|
wrapper.find('button').trigger('click');
|
|
|
|
});
|
|
|
|
expect(checked.value).toBe(2);
|
|
|
|
|
|
|
|
await asyncExpect(() => {
|
|
|
|
wrapper.find('button').trigger('click');
|
|
|
|
});
|
|
|
|
expect(checked.value).toBe(1);
|
|
|
|
});
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|