ant-design-vue/components/radio/__tests__/group.test.js

255 lines
7.0 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import { mount } from '@vue/test-utils';
3.0 ready (#4523) * refactor: transfer、tooltip (#4306) * refactor(transfer): use composition api (#4135) * refactor(transfer): use composition api * fix: remove console * refactor(tooltip): use composition api (#4059) * refactor(tooltip): use composition api * chore: useConfigInject * fix: remove useless * style: format code * refactor: transfer * refactor: tooltip Co-authored-by: ajuner <106791576@qq.com> * Refactor mentions (#4341) * refactor(mentions): use compositionAPI (#4313) * refactor: mentions * refactor: mentions Co-authored-by: ajuner <106791576@qq.com> * Refactor progress (#4358) * fix: timepicker error border not show #4331 * fix(UploadDragger): fix UploadDrager no export (#4334) * refactor(switch): support customize checked value #4329 (#4332) * refactor(switch): support customize checked value #4329 * test: add test case * refactor: update props name * refactor: update ts * refactor: optimize * style: uncheckedValue to unCheckedValue * test: update snap * feat: udpate switch ts * docs: remove ie11 * fix: tree-select throw error when use slot title * fix: TypeScript definition of Table interface for typescript 4.3.5 (#4353) * fix type for typescript 4.3.5 * Update interface.ts close #4296 * fix: dropdown submenu style error #4351 close #4351 * fix(notification): 完善notification类型 (#4346) * refactor(progress): use composition API (#4355) * refactor(progress): use composition API * refactor(vc-progress): update * refactor: progress * refactor: progress * fix: timepicker error border not show #4331 * fix(UploadDragger): fix UploadDrager no export (#4334) * refactor(switch): support customize checked value #4329 (#4332) * refactor(switch): support customize checked value #4329 * test: add test case * refactor: update props name * refactor: update ts * refactor: optimize * style: uncheckedValue to unCheckedValue * test: update snap * feat: udpate switch ts * docs: remove ie11 * fix: tree-select throw error when use slot title * fix: TypeScript definition of Table interface for typescript 4.3.5 (#4353) * fix type for typescript 4.3.5 * Update interface.ts close #4296 * fix: dropdown submenu style error #4351 close #4351 * fix(notification): 完善notification类型 (#4346) * refactor(progress): use composition API (#4355) * refactor(progress): use composition API * refactor(vc-progress): update * refactor: progress * refactor: progress Co-authored-by: Jarvis <35361626+fanhaoyuan@users.noreply.github.com> Co-authored-by: John <John60676@qq.com> Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com> Co-authored-by: zanllp <qc@zanllp.cn> * docs: add changelog * refactor: tree * refactor: tree * style: lint * refactor: tree * 热factor: tree * refactor: tree * refactor: tree * refactor: tree * refactor: directory tree * refactor: tree * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * style: lint format * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * fix: upload ts error * fix: update tree title render & switchIcon * test: update tree test * feat: add VirtualScroll tree * refactor: datePicker & calendar & trigger (#4522) * style: update * test: update calendar test * test: update test * test: update test * refactor: slider * feat: update slider css * refactor: slider to ts * refactor: slider to ts * perf: update default itemHeight * test: update * fix: uddate ts type * fix: update skeleton * fix: update skeleton * refactor: update vc-pagination * refactor: pagination * refactor: timeline * refactor: steps * refactor: collapse * refactor: collapse * refactor: popconfirm * refactor: popover * refactor: dropdown * doc: merge doc * chore: vite for dev (#4602) * style: js to jsx * doc: add site * style: lint * style: format ts type * doc: update * style: format code * style: format site * doc: update * style: dmeo * style: format scripts * chore: remove sub-modules * chore: update vite * site: add site build * test: update snap * doc(select): add tip (#4606) * refactor: table (#4641) * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * fix: column not pass to cell * doc: uppate table * fix: update bodyCell headerCell * doc: remove examples * refactor: table * fix: table title not work * fix: table selection * fix: table checkStrictly * refactor: table * fix: table template error * feat: table support summary * test: update snap * perf: table * docs(table): fix ajax demo (#4639) * test: update table * refactor: remove old table * doc: update table doc * doc: update doc * doc: update select * doc: update summary Co-authored-by: John <John60676@qq.com> * doc: update doc * fix: menu arrow not work * test: update * doc: add next site * style: format * doc: update * doc: update site script * fix: expand icon not fixed * feat: use renderSlot * test: update table snap * feat: confirm support reactively * feat: configProvider.config * feat: message support configprovider.config * feat: notification support configprovider.config * doc: update doc * fix: typescript compile error * style: add import eslint * doc: update demo * chore: set transpileOnly true * style: fix eslint error * test: update snap * doc: update * test: mock date * test: update snap * chore: remove gulp-typescript (#4675) * feat: V3 form (#4678) * chore: update husky * perf: update formItem * perf: useInjectFormItemContext * fix: table ts error * doc: add Customized Form Controls demo * feat: export useInjectFormItemContext * doc: update form doc * doc: update doc * doc: update doc * feat: autocomplete support option slot * doc: update * feat: add form item rest * style: remove omit.js * refactor: autocomplete * doc: add changelog to site * doc: update site anchor * doc: update doc layout * test: update table test * doc: update * chore: udpate gulp script * chore: udpate gulp script * doc: add changelog * doc: update * test: ignore some test wait vue-test-utils * fix: form id error #4582 close #4582 * doc: add select Responsive demo * doc: remove temp doc Co-authored-by: ajuner <106791576@qq.com> Co-authored-by: Jarvis <35361626+fanhaoyuan@users.noreply.github.com> Co-authored-by: John <John60676@qq.com> Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com> Co-authored-by: zanllp <qc@zanllp.cn> Co-authored-by: Amour1688 <lcz_1996@foxmail.com>
2021-09-25 08:51:32 +00:00
import { asyncExpect } from '../../../tests/utils';
2019-01-12 03:33:27 +00:00
import Radio from '../Radio';
import RadioGroup from '../Group';
2018-06-17 09:25:54 +00:00
describe('Radio', () => {
2020-08-03 08:47:12 +00:00
function createRadioGroup(props) {
2018-06-17 09:25:54 +00:00
return {
props: ['value'],
2019-01-12 03:33:27 +00:00
render() {
const groupProps = { ...props };
2018-06-17 09:25:54 +00:00
if (this.value !== undefined) {
2019-01-12 03:33:27 +00:00
groupProps.value = this.value;
2018-06-17 09:25:54 +00:00
}
return (
2020-08-03 08:47:12 +00:00
<RadioGroup ref="radioGroup" {...groupProps}>
2019-01-12 03:33:27 +00:00
<Radio value="A">A</Radio>
<Radio value="B">B</Radio>
<Radio value="C">C</Radio>
2018-06-17 09:25:54 +00:00
</RadioGroup>
2019-01-12 03:33:27 +00:00
);
2018-06-17 09:25:54 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-06-17 09:25:54 +00:00
}
2019-01-12 03:33:27 +00:00
function createRadioGroupByOption() {
2018-06-17 09:25:54 +00:00
const options = [
{ label: 'A', value: 'A' },
{ label: 'B', value: 'B' },
{ label: 'C', value: 'C' },
2019-01-12 03:33:27 +00:00
];
2018-06-17 09:25:54 +00:00
return {
2019-01-12 03:33:27 +00:00
render() {
return <RadioGroup options={options} />;
2018-06-17 09:25:54 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-06-17 09:25:54 +00:00
}
it('responses hover events', () => {
2019-01-12 03:33:27 +00:00
const onMouseEnter = jest.fn();
const onMouseLeave = jest.fn();
2018-06-17 09:25:54 +00:00
const wrapper = mount({
2019-01-12 03:33:27 +00:00
render() {
2018-06-17 09:25:54 +00:00
return (
2019-01-12 03:33:27 +00:00
<RadioGroup onMouseenter={onMouseEnter} onMouseleave={onMouseLeave}>
2018-06-17 09:25:54 +00:00
<Radio />
</RadioGroup>
2019-01-12 03:33:27 +00:00
);
2018-06-17 09:25:54 +00:00
},
2019-01-12 03:33:27 +00:00
});
2018-06-17 09:25:54 +00:00
2020-08-03 08:47:12 +00:00
wrapper.findAll('div')[0].trigger('mouseenter');
2019-01-12 03:33:27 +00:00
expect(onMouseEnter).toHaveBeenCalled();
2018-06-17 09:25:54 +00:00
2020-08-03 08:47:12 +00:00
wrapper.findAll('div')[0].trigger('mouseleave');
2019-01-12 03:33:27 +00:00
expect(onMouseLeave).toHaveBeenCalled();
});
2018-06-17 09:25:54 +00:00
it('fire change events when value changes', async () => {
2019-01-12 03:33:27 +00:00
const onChange = jest.fn();
2020-08-03 08:47:12 +00:00
const props = { onChange };
const wrapper = mount(createRadioGroup(props), { sync: false });
2019-01-12 03:33:27 +00:00
let radios = null;
2018-06-17 09:25:54 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
radios = wrapper.findAll('input');
2018-06-17 09:25:54 +00:00
// uncontrolled component
2019-01-12 03:33:27 +00:00
wrapper.vm.$refs.radioGroup.stateValue = 'B';
2018-06-17 09:25:54 +00:00
// wrapper.setData({ value: 'B' })
2020-08-03 08:47:12 +00:00
radios[0].trigger('change');
2019-01-12 03:33:27 +00:00
expect(onChange.mock.calls.length).toBe(1);
});
2018-06-17 09:25:54 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.html()).toMatchSnapshot();
});
2018-06-17 09:25:54 +00:00
await asyncExpect(() => {
// controlled component
2019-01-12 03:33:27 +00:00
wrapper.setProps({ value: 'A' });
2020-08-03 08:47:12 +00:00
radios[1].trigger('change');
2019-01-12 03:33:27 +00:00
expect(onChange.mock.calls.length).toBe(2);
});
2018-06-17 09:25:54 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.html()).toMatchSnapshot();
});
});
2018-06-17 09:25:54 +00:00
2019-09-18 12:03:13 +00:00
it('both of radio and radioGroup will trigger onchange event when they exists', async () => {
const onChange = jest.fn();
const onChangeRadioGroup = jest.fn();
const wrapper = mount(
{
props: ['value'],
render() {
const groupProps = {};
if (this.value !== undefined) {
groupProps.value = this.value;
}
return (
<RadioGroup ref="radioGroup" {...groupProps} onChange={onChangeRadioGroup}>
<Radio value="A" onChange={onChange}>
A
</Radio>
<Radio value="B" onChange={onChange}>
B
</Radio>
<Radio value="C" onChange={onChange}>
C
</Radio>
</RadioGroup>
);
},
},
{ sync: false },
);
const radios = wrapper.findAll('input');
// uncontrolled component
wrapper.vm.$refs.radioGroup.stateValue = 'B';
2020-08-03 08:47:12 +00:00
radios[0].trigger('change');
2019-09-18 12:03:13 +00:00
expect(onChange.mock.calls.length).toBe(1);
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
2019-09-18 12:03:13 +00:00
// controlled component
wrapper.setProps({ value: 'A' });
2020-08-03 08:47:12 +00:00
radios[1].trigger('change');
2019-09-18 12:03:13 +00:00
expect(onChange.mock.calls.length).toBe(2);
});
2019-10-16 06:47:49 +00:00
it('Trigger onChange when both of radioButton and radioGroup exists', async () => {
2019-09-18 12:03:13 +00:00
const onChange = jest.fn();
2020-08-03 08:47:12 +00:00
const props = { onChange };
const wrapper = mount(createRadioGroup(props), { sync: false });
2019-09-18 12:03:13 +00:00
const radios = wrapper.findAll('input');
// uncontrolled component
wrapper.vm.$refs.radioGroup.stateValue = 'B';
2020-08-03 08:47:12 +00:00
radios[0].trigger('change');
expect(onChange.mock.calls.length).toBe(1);
asyncExpect(() => {
// controlled component
wrapper.setProps({ value: 'A' });
2020-08-03 08:47:12 +00:00
radios[1].trigger('change');
2019-10-16 06:47:49 +00:00
expect(onChange.mock.calls.length).toBe(2);
});
2019-09-18 12:03:13 +00:00
});
// it('should only trigger once when in group with options', () => {
// const onChange = jest.fn();
// const options = [{ label: 'Bamboo', value: 'Bamboo' }];
// const wrapper = mount(<RadioGroup options={options} onChange={onChange} />);
// wrapper.find('input').trigger('change');
// expect(onChange).toHaveBeenCalledTimes(1);
// });
2018-06-17 09:25:54 +00:00
// it('won\'t fire change events when value not changes', async () => {
// const onChange = jest.fn()
// const wrapper = mount(
// createRadioGroup({}, {
// change: onChange,
// }),
// { sync: false }
// )
// const radios = wrapper.findAll('input')
// await asyncExpect(() => {
// // uncontrolled component
// wrapper.vm.$refs.radioGroup.stateValue = 'B'
2020-08-03 08:47:12 +00:00
// radios[1].trigger('change')
2018-06-17 09:25:54 +00:00
// expect(onChange.mock.calls.length).toBe(0)
// })
// await asyncExpect(() => {
// }, 0)
// // // controlled component
// // wrapper.setProps({ value: 'A' })
2020-08-03 08:47:12 +00:00
// // radios[0].trigger('change')
2018-06-17 09:25:54 +00:00
// // expect(onChange.mock.calls.length).toBe(0)
// })
it('optional should correct render', () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(createRadioGroupByOption());
const radios = wrapper.findAll('input');
2018-06-17 09:25:54 +00:00
2019-01-12 03:33:27 +00:00
expect(radios.length).toBe(3);
});
2018-06-17 09:25:54 +00:00
it('all children should have a name property', () => {
2019-01-12 03:33:27 +00:00
const GROUP_NAME = 'radiogroup';
const wrapper = mount(createRadioGroup({ name: GROUP_NAME }));
expect(wrapper.html()).toMatchSnapshot();
expect(
2020-08-03 08:47:12 +00:00
wrapper.findAll('input[type="radio"]').forEach(el => {
2019-01-12 03:33:27 +00:00
expect(el.element.name).toEqual(GROUP_NAME);
}),
);
});
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
2018-09-05 13:28:54 +00:00
it('passes prefixCls down to radio', () => {
2019-12-21 10:37:33 +00:00
const options = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Orange', value: 'Orange' },
];
2019-01-12 03:33:27 +00:00
const wrapper = mount(RadioGroup, {
2020-07-25 07:46:49 +00:00
props: {
2019-01-12 03:33:27 +00:00
prefixCls: 'my-radio',
options,
},
});
expect(wrapper.html()).toMatchSnapshot();
});
it('when onChange do not change the value, change event can be also triggered.', async () => {
const onChange = jest.fn();
const onChangeRadioGroup = () => {
onChange();
wrapper.setProps({ value: 'A' });
};
const wrapper = mount(
{
props: ['value'],
render() {
const value = this.value || 'A';
return (
<RadioGroup ref="radioGroup" value={value} onChange={onChangeRadioGroup}>
<Radio value="A">A</Radio>
<Radio value="B">B</Radio>
<Radio value="C">C</Radio>
</RadioGroup>
);
},
},
{ sync: false },
);
const radios = wrapper.findAll('input');
await asyncExpect(() => {
radios[1].trigger('click');
expect(onChange.mock.calls.length).toBe(1);
});
await asyncExpect(() => {
radios[1].trigger('click');
expect(onChange.mock.calls.length).toBe(2);
});
});
2019-01-12 03:33:27 +00:00
});