From ed2b541e87ebd09a99d0c58a6d61bfe2152a0e66 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Sat, 22 Feb 2020 20:37:50 +0800 Subject: [PATCH] test: add switch test --- .../__snapshots__/index.test.js.snap | 3 +++ components/switch/__tests__/index.test.js | 22 +++++++++++++++++++ components/switch/index.jsx | 11 +++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 components/switch/__tests__/__snapshots__/index.test.js.snap diff --git a/components/switch/__tests__/__snapshots__/index.test.js.snap b/components/switch/__tests__/__snapshots__/index.test.js.snap new file mode 100644 index 000000000..3f314cbec --- /dev/null +++ b/components/switch/__tests__/__snapshots__/index.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Switch should has click wave effect 1`] = ``; diff --git a/components/switch/__tests__/index.test.js b/components/switch/__tests__/index.test.js index a91d79901..642a711f1 100644 --- a/components/switch/__tests__/index.test.js +++ b/components/switch/__tests__/index.test.js @@ -1,6 +1,28 @@ import Switch from '..'; +import { mount } from '@vue/test-utils'; import focusTest from '../../../tests/shared/focusTest'; +import { resetWarned } from '../../_util/warning'; +import mountTest from '../../../tests/shared/mountTest'; describe('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(); + }); }); diff --git a/components/switch/index.jsx b/components/switch/index.jsx index 8fd9db16c..fae809549 100644 --- a/components/switch/index.jsx +++ b/components/switch/index.jsx @@ -1,13 +1,15 @@ 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 Wave from '../_util/wave'; import Icon from '../icon'; import { ConfigConsumerProps } from '../config-provider'; import Base from '../base'; +import warning from '../_util/warning'; const Switch = { name: 'ASwitch', + __ANT_SWITCH: true, model: { prop: 'checked', event: 'change', @@ -36,6 +38,13 @@ const Switch = { this.$refs.refSwitchNode.blur(); }, }, + created() { + warning( + hasProp(this, 'checked') || !hasProp(this, 'value'), + 'Switch', + '`value` is not validate prop, do you mean `checked`?', + ); + }, render() { const { prefixCls: customizePrefixCls, size, loading, disabled, ...restProps } = getOptionProps(