ant-design/components/radio/__tests__/group.test.tsx

264 lines
8.1 KiB
TypeScript
Raw Normal View History

import type { RefAttributes } from 'react';
chore: migrate to vitest (#42506) * chore: migrate to vitest * chore: update ci * fix: test correctly * test: support puppeteer * chore: update coverage * chore: update include/exclude * chore: update config * test: update incorrect tests * chore: update script * chore: update * fix: should close browser at the ended * chore: improve * fix: test cause tsc error * fix: eslint error * chore: exclude correctly * test: update snap and fix some tests * chore: update test config * fix: countup.js * fix: incorrect test * chore: update reference * test: update * fix: countup.js * fix: timeout * chore: update site test * fix: fixed countup version * chore: remove unsed code * test: update * test: update demo timeout * test: update timeout * chore: update image test * chore: update threads * fix: image/svg+xml test failed * chore: limits threads * test: update test coverage include * chore: remove jest files * chore: rename jest to vi * chore: update document * chore: fix missing @types/jsdom * chore: update coverage * chore: update snap * fix:watermark test cases are incorrect * feat: update ignore comment * test: fix test case * test: reset body scrollTop * test: clean up * test: use vi * test: update snapshot * test: update snapshot * test: fix dropdown test failed * fix: toHaveStyle cause test fail * test: improve test case * test: fix * fix: color failed, refer to https://github.com/jsdom/jsdom/pull/3560 * test: fix * test: fix * test: fix circular import * test: revert * ci: coverage failed * test: fix c8 ignore comment * chore: incorrect config * chore: fix ignore ci * test: revert svg+xml * test: fix realTimers * feat: rc-trigger should be remove * test: fix some failed test * chore: remove unused deps and configure eslint-plugin-vitest * test: update snap * chore: remove jest * test: fix lint error --------- Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: afc163 <afc163@gmail.com>
2023-06-07 11:54:50 +08:00
import React from 'react';
2023-10-12 09:43:35 +08:00
import type { RadioGroupProps } from '..';
import Radio from '..';
chore: migrate to vitest (#42506) * chore: migrate to vitest * chore: update ci * fix: test correctly * test: support puppeteer * chore: update coverage * chore: update include/exclude * chore: update config * test: update incorrect tests * chore: update script * chore: update * fix: should close browser at the ended * chore: improve * fix: test cause tsc error * fix: eslint error * chore: exclude correctly * test: update snap and fix some tests * chore: update test config * fix: countup.js * fix: incorrect test * chore: update reference * test: update * fix: countup.js * fix: timeout * chore: update site test * fix: fixed countup version * chore: remove unsed code * test: update * test: update demo timeout * test: update timeout * chore: update image test * chore: update threads * fix: image/svg+xml test failed * chore: limits threads * test: update test coverage include * chore: remove jest files * chore: rename jest to vi * chore: update document * chore: fix missing @types/jsdom * chore: update coverage * chore: update snap * fix:watermark test cases are incorrect * feat: update ignore comment * test: fix test case * test: reset body scrollTop * test: clean up * test: use vi * test: update snapshot * test: update snapshot * test: fix dropdown test failed * fix: toHaveStyle cause test fail * test: improve test case * test: fix * fix: color failed, refer to https://github.com/jsdom/jsdom/pull/3560 * test: fix * test: fix * test: fix circular import * test: revert * ci: coverage failed * test: fix c8 ignore comment * chore: incorrect config * chore: fix ignore ci * test: revert svg+xml * test: fix realTimers * feat: rc-trigger should be remove * test: fix some failed test * chore: remove unused deps and configure eslint-plugin-vitest * test: update snap * chore: remove jest * test: fix lint error --------- Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: afc163 <afc163@gmail.com>
2023-06-07 11:54:50 +08:00
import { fireEvent, render } from '../../../tests/utils';
2019-08-26 22:53:20 +08:00
describe('Radio Group', () => {
function createRadioGroup(props?: RadioGroupProps & RefAttributes<HTMLDivElement>) {
return (
<Radio.Group {...props}>
<Radio value="A">A</Radio>
<Radio value="B">B</Radio>
<Radio value="C">C</Radio>
</Radio.Group>
);
}
function createRadioGroupByOption(props?: RadioGroupProps & RefAttributes<HTMLDivElement>) {
2017-03-28 11:56:38 +08:00
const options = [
{ label: 'A', value: 'A' },
{ label: 'B', value: 'B' },
{ label: 'C', value: 'C' },
];
return <Radio.Group {...props} options={options} />;
2017-03-28 11:56:38 +08:00
}
2016-12-14 14:48:09 +08:00
it('responses hover events', () => {
const onMouseEnter = jest.fn();
const onMouseLeave = jest.fn();
2016-12-14 14:48:09 +08:00
const { container } = render(
<Radio.Group onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
2016-12-14 14:48:09 +08:00
<Radio />
</Radio.Group>,
2016-12-14 14:48:09 +08:00
);
fireEvent.mouseEnter(container.querySelector('div')!);
2016-12-14 14:48:09 +08:00
expect(onMouseEnter).toHaveBeenCalled();
fireEvent.mouseLeave(container.querySelector('div')!);
2016-12-14 14:48:09 +08:00
expect(onMouseLeave).toHaveBeenCalled();
});
it('fire change events when value changes', () => {
const onChange = jest.fn();
const { container, rerender } = render(
createRadioGroup({
onChange,
2018-12-07 20:02:01 +08:00
}),
);
const radios = container.querySelectorAll('input');
// controlled component
rerender(
createRadioGroup({
onChange,
value: 'A',
}),
);
fireEvent.click(radios[1]);
expect(onChange.mock.calls.length).toBe(1);
});
it('both of radio and radioGroup will trigger onchange event when they exists', () => {
const onChange = jest.fn();
const onChangeRadioGroup = jest.fn();
const RadioGroup: React.FC<
RadioGroupProps & { onChangeRadioGroup: RadioGroupProps['onChange'] }
> = (props) => (
<Radio.Group onChange={props.onChangeRadioGroup}>
<Radio value="A" onChange={props.onChange}>
A
</Radio>
<Radio value="B" onChange={props.onChange}>
B
</Radio>
<Radio value="C" onChange={props.onChange}>
C
</Radio>
</Radio.Group>
);
const { container, rerender } = render(
<RadioGroup onChangeRadioGroup={onChangeRadioGroup} onChange={onChange} />,
);
const radios = container.querySelectorAll('input');
// controlled component
rerender(<RadioGroup value="A" onChangeRadioGroup={onChangeRadioGroup} onChange={onChange} />);
fireEvent.click(radios[1]);
expect(onChange.mock.calls.length).toBe(1);
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
});
it('Trigger onChange when both of radioButton and radioGroup exists', () => {
const onChange = jest.fn();
const RadioGroup: React.FC<RadioGroupProps> = (props) => (
<Radio.Group {...props}>
<Radio.Button value="A">A</Radio.Button>
<Radio.Button value="B">B</Radio.Button>
<Radio.Button value="C">C</Radio.Button>
</Radio.Group>
);
const { container, rerender } = render(<RadioGroup onChange={onChange} />);
const radios = container.querySelectorAll('input');
// controlled component
rerender(<RadioGroup value="A" onChange={onChange} />);
fireEvent.click(radios[1]);
expect(onChange.mock.calls.length).toBe(1);
});
it('should only trigger once when in group with options', () => {
const onChange = jest.fn();
const options = [{ label: 'Bamboo', value: 'Bamboo' }];
const { container } = render(<Radio.Group options={options} onChange={onChange} />);
fireEvent.click(container.querySelector('input')!);
expect(onChange).toHaveBeenCalledTimes(1);
});
2018-12-07 20:02:01 +08:00
it("won't fire change events when value not changes", () => {
const onChange = jest.fn();
const { container, rerender } = render(
createRadioGroup({
onChange,
2018-12-07 20:02:01 +08:00
}),
);
const radios = container.querySelectorAll('input');
// controlled component
rerender(
createRadioGroup({
onChange,
value: 'A',
}),
);
fireEvent.click(radios[0]);
expect(onChange.mock.calls.length).toBe(0);
});
2017-03-28 11:56:38 +08:00
it('optional should correct render', () => {
const { container } = render(createRadioGroupByOption());
const radios = container.querySelectorAll('input');
2017-03-28 11:56:38 +08:00
expect(radios.length).toBe(3);
});
it('all children should have a name property', () => {
const GROUP_NAME = 'GROUP_NAME';
const { container } = render(createRadioGroup({ name: GROUP_NAME }));
container.querySelectorAll<HTMLInputElement>('input[type="radio"]').forEach((el) => {
expect(el.name).toEqual(GROUP_NAME);
});
});
it('passes prefixCls down to radio', () => {
const options = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Orange', value: 'Orange', style: { fontSize: 12 } },
];
const { container } = render(<Radio.Group prefixCls="my-radio" options={options} />);
expect(container.firstChild).toMatchSnapshot();
});
it('should forward ref', () => {
let radioGroupRef: HTMLDivElement;
const { container } = render(
createRadioGroupByOption({
ref(ref: HTMLDivElement) {
radioGroupRef = ref;
},
}),
);
expect(radioGroupRef!).toBe(container.querySelector<HTMLDivElement>('.ant-radio-group'));
});
it('should support data-* or aria-* props', () => {
const { container } = render(
createRadioGroup({
'data-radio-group-id': 'radio-group-id',
'aria-label': 'radio-group',
} as RadioGroupProps),
);
expect((container.firstChild as HTMLDivElement)?.getAttribute('data-radio-group-id')).toBe(
'radio-group-id',
);
expect((container.firstChild as HTMLDivElement)?.getAttribute('aria-label')).toBe(
'radio-group',
);
});
it('Radio type should not be override', () => {
const onChange = jest.fn();
const { container } = render(
<Radio.Group onChange={onChange}>
<Radio value={1} type="1">
A
</Radio>
<Radio value={2} type="2">
B
</Radio>
<Radio value={3} type="3">
C
</Radio>
<Radio value={4} type="4">
D
</Radio>
</Radio.Group>,
);
const radios = container.querySelectorAll('input');
fireEvent.click(radios[0]);
expect(onChange).toHaveBeenCalled();
expect(radios[1].type).toBe('radio');
});
describe('value is null or undefined', () => {
it('use `defaultValue` when `value` is undefined', () => {
const options = [{ label: 'Bamboo', value: 'bamboo' }];
const { container } = render(
<Radio.Group defaultValue="bamboo" value={undefined} options={options} />,
);
expect(container.querySelectorAll('.ant-radio-wrapper-checked').length).toBe(1);
});
[undefined, null].forEach((newValue) => {
it(`should set value back when value change back to ${newValue}`, () => {
const options = [{ label: 'Bamboo', value: 'bamboo' }];
const { container, rerender } = render(<Radio.Group value="bamboo" options={options} />);
expect(container.querySelectorAll('.ant-radio-wrapper-checked').length).toBe(1);
rerender(<Radio.Group value={newValue} options={options} />);
expect(container.querySelectorAll('.ant-radio-wrapper-checked').length).toBe(0);
});
});
});
it('onBlur & onFocus should work', () => {
const handleBlur = jest.fn();
const handleFocus = jest.fn();
const { container } = render(
<Radio.Group options={['1', '2', '3']} onBlur={handleBlur} onFocus={handleFocus} />,
);
fireEvent.focus(container.firstChild!);
expect(handleFocus).toHaveBeenCalledTimes(1);
fireEvent.blur(container.firstChild!);
expect(handleBlur).toHaveBeenCalledTimes(1);
});
2023-10-12 09:43:35 +08:00
it('options support id', () => {
const { container } = render(
<Radio.Group options={[{ label: 'bamboo', id: 'bamboo', value: 'bamboo' }]} />,
);
expect(container.querySelector('#bamboo')).toBeTruthy();
});
2016-12-14 14:48:09 +08:00
});