fix: radio emit two change event #1280
parent
4218f19021
commit
b6656797fc
|
@ -71,10 +71,13 @@ export default {
|
||||||
if (!hasProp(this, 'value')) {
|
if (!hasProp(this, 'value')) {
|
||||||
this.stateValue = value;
|
this.stateValue = value;
|
||||||
}
|
}
|
||||||
|
// nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280
|
||||||
|
this.$nextTick(() => {
|
||||||
if (value !== lastValue) {
|
if (value !== lastValue) {
|
||||||
this.$emit('input', value);
|
this.$emit('input', value);
|
||||||
this.$emit('change', ev);
|
this.$emit('change', ev);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
render() {
|
render() {
|
||||||
|
|
|
@ -80,6 +80,8 @@ describe('Radio', () => {
|
||||||
wrapper.vm.$refs.radioGroup.stateValue = 'B';
|
wrapper.vm.$refs.radioGroup.stateValue = 'B';
|
||||||
// wrapper.setData({ value: 'B' })
|
// wrapper.setData({ value: 'B' })
|
||||||
radios.at(0).trigger('change');
|
radios.at(0).trigger('change');
|
||||||
|
});
|
||||||
|
await asyncExpect(() => {
|
||||||
expect(onChange.mock.calls.length).toBe(1);
|
expect(onChange.mock.calls.length).toBe(1);
|
||||||
});
|
});
|
||||||
await asyncExpect(() => {
|
await asyncExpect(() => {
|
||||||
|
@ -89,6 +91,8 @@ describe('Radio', () => {
|
||||||
// controlled component
|
// controlled component
|
||||||
wrapper.setProps({ value: 'A' });
|
wrapper.setProps({ value: 'A' });
|
||||||
radios.at(1).trigger('change');
|
radios.at(1).trigger('change');
|
||||||
|
});
|
||||||
|
await asyncExpect(() => {
|
||||||
expect(onChange.mock.calls.length).toBe(2);
|
expect(onChange.mock.calls.length).toBe(2);
|
||||||
});
|
});
|
||||||
await asyncExpect(() => {
|
await asyncExpect(() => {
|
||||||
|
@ -131,7 +135,9 @@ describe('Radio', () => {
|
||||||
wrapper.vm.$refs.radioGroup.stateValue = 'B';
|
wrapper.vm.$refs.radioGroup.stateValue = 'B';
|
||||||
radios.at(0).trigger('change');
|
radios.at(0).trigger('change');
|
||||||
expect(onChange.mock.calls.length).toBe(1);
|
expect(onChange.mock.calls.length).toBe(1);
|
||||||
|
await asyncExpect(() => {
|
||||||
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
|
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
// controlled component
|
// controlled component
|
||||||
wrapper.setProps({ value: 'A' });
|
wrapper.setProps({ value: 'A' });
|
||||||
|
@ -139,7 +145,7 @@ describe('Radio', () => {
|
||||||
expect(onChange.mock.calls.length).toBe(2);
|
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 onChange = jest.fn();
|
||||||
const props = {};
|
const props = {};
|
||||||
const wrapper = mount(
|
const wrapper = mount(
|
||||||
|
@ -153,13 +159,16 @@ describe('Radio', () => {
|
||||||
// uncontrolled component
|
// uncontrolled component
|
||||||
wrapper.vm.$refs.radioGroup.stateValue = 'B';
|
wrapper.vm.$refs.radioGroup.stateValue = 'B';
|
||||||
radios.at(0).trigger('change');
|
radios.at(0).trigger('change');
|
||||||
|
await asyncExpect(() => {
|
||||||
expect(onChange.mock.calls.length).toBe(1);
|
expect(onChange.mock.calls.length).toBe(1);
|
||||||
|
});
|
||||||
// controlled component
|
// controlled component
|
||||||
wrapper.setProps({ value: 'A' });
|
wrapper.setProps({ value: 'A' });
|
||||||
radios.at(1).trigger('change');
|
radios.at(1).trigger('change');
|
||||||
|
await asyncExpect(() => {
|
||||||
expect(onChange.mock.calls.length).toBe(2);
|
expect(onChange.mock.calls.length).toBe(2);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// it('should only trigger once when in group with options', () => {
|
// it('should only trigger once when in group with options', () => {
|
||||||
// const onChange = jest.fn();
|
// const onChange = jest.fn();
|
||||||
|
|
Loading…
Reference in New Issue