From b6656797fc0b2dbcc69eebb86670eb86bb32d457 Mon Sep 17 00:00:00 2001 From: tanjinzhou <415800467@qq.com> Date: Wed, 16 Oct 2019 14:47:49 +0800 Subject: [PATCH] fix: radio emit two change event #1280 --- components/radio/Group.jsx | 11 +++++++---- components/radio/__tests__/group.test.js | 19 ++++++++++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/components/radio/Group.jsx b/components/radio/Group.jsx index 371dbc40f..2309351f1 100644 --- a/components/radio/Group.jsx +++ b/components/radio/Group.jsx @@ -71,10 +71,13 @@ export default { if (!hasProp(this, 'value')) { this.stateValue = value; } - if (value !== lastValue) { - this.$emit('input', value); - this.$emit('change', ev); - } + // nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280 + this.$nextTick(() => { + if (value !== lastValue) { + this.$emit('input', value); + this.$emit('change', ev); + } + }); }, }, render() { diff --git a/components/radio/__tests__/group.test.js b/components/radio/__tests__/group.test.js index 896e71f96..f892cf74c 100644 --- a/components/radio/__tests__/group.test.js +++ b/components/radio/__tests__/group.test.js @@ -80,6 +80,8 @@ describe('Radio', () => { wrapper.vm.$refs.radioGroup.stateValue = 'B'; // wrapper.setData({ value: 'B' }) radios.at(0).trigger('change'); + }); + await asyncExpect(() => { expect(onChange.mock.calls.length).toBe(1); }); await asyncExpect(() => { @@ -89,6 +91,8 @@ describe('Radio', () => { // controlled component wrapper.setProps({ value: 'A' }); radios.at(1).trigger('change'); + }); + await asyncExpect(() => { expect(onChange.mock.calls.length).toBe(2); }); await asyncExpect(() => { @@ -131,7 +135,9 @@ describe('Radio', () => { wrapper.vm.$refs.radioGroup.stateValue = 'B'; radios.at(0).trigger('change'); expect(onChange.mock.calls.length).toBe(1); - expect(onChangeRadioGroup.mock.calls.length).toBe(1); + await asyncExpect(() => { + expect(onChangeRadioGroup.mock.calls.length).toBe(1); + }); // controlled component wrapper.setProps({ value: 'A' }); @@ -139,7 +145,7 @@ describe('Radio', () => { expect(onChange.mock.calls.length).toBe(2); }); - it('Trigger onChange when both of radioButton and radioGroup exists', () => { + it('Trigger onChange when both of radioButton and radioGroup exists', async () => { const onChange = jest.fn(); const props = {}; const wrapper = mount( @@ -153,12 +159,15 @@ describe('Radio', () => { // uncontrolled component wrapper.vm.$refs.radioGroup.stateValue = 'B'; radios.at(0).trigger('change'); - expect(onChange.mock.calls.length).toBe(1); - + await asyncExpect(() => { + expect(onChange.mock.calls.length).toBe(1); + }); // controlled component wrapper.setProps({ value: 'A' }); radios.at(1).trigger('change'); - expect(onChange.mock.calls.length).toBe(2); + await asyncExpect(() => { + expect(onChange.mock.calls.length).toBe(2); + }); }); // it('should only trigger once when in group with options', () => {