test: add switch test

pull/1845/head
tangjinzhou 2020-02-22 20:37:50 +08:00
parent 68014c67d9
commit ed2b541e87
3 changed files with 35 additions and 1 deletions

View File

@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Switch should has click wave effect 1`] = `<button type="button" role="switch" class="ant-switch ant-switch-checked" aria-checked="true"><span class="ant-switch-inner"></span></button>`;

View File

@ -1,6 +1,28 @@
import Switch from '..'; import Switch from '..';
import { mount } from '@vue/test-utils';
import focusTest from '../../../tests/shared/focusTest'; import focusTest from '../../../tests/shared/focusTest';
import { resetWarned } from '../../_util/warning';
import mountTest from '../../../tests/shared/mountTest';
describe('Switch', () => { describe('Switch', () => {
focusTest(Switch); focusTest(Switch);
mountTest(Switch);
it('should has click wave effect', async () => {
const wrapper = mount(Switch);
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(() => {});
mount(Switch, { propsData: { value: '' } });
expect(errorSpy).toHaveBeenCalledWith(
'Warning: [antdv: Switch] `value` is not validate prop, do you mean `checked`?',
);
errorSpy.mockRestore();
});
}); });

View File

@ -1,13 +1,15 @@
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import { getOptionProps, getComponentFromProp, getListeners } from '../_util/props-util'; import hasProp, { getOptionProps, getComponentFromProp, getListeners } from '../_util/props-util';
import VcSwitch from '../vc-switch'; import VcSwitch from '../vc-switch';
import Wave from '../_util/wave'; import Wave from '../_util/wave';
import Icon from '../icon'; import Icon from '../icon';
import { ConfigConsumerProps } from '../config-provider'; import { ConfigConsumerProps } from '../config-provider';
import Base from '../base'; import Base from '../base';
import warning from '../_util/warning';
const Switch = { const Switch = {
name: 'ASwitch', name: 'ASwitch',
__ANT_SWITCH: true,
model: { model: {
prop: 'checked', prop: 'checked',
event: 'change', event: 'change',
@ -36,6 +38,13 @@ const Switch = {
this.$refs.refSwitchNode.blur(); this.$refs.refSwitchNode.blur();
}, },
}, },
created() {
warning(
hasProp(this, 'checked') || !hasProp(this, 'value'),
'Switch',
'`value` is not validate prop, do you mean `checked`?',
);
},
render() { render() {
const { prefixCls: customizePrefixCls, size, loading, disabled, ...restProps } = getOptionProps( const { prefixCls: customizePrefixCls, size, loading, disabled, ...restProps } = getOptionProps(