radioGroup.test.jsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import React from 'react';
  2. import { mount, render } from 'enzyme';
  3. import Radio from '../index';
  4. import RadioGroup from '../radioGroup';
  5. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  6. // eslint-disable-next-line max-lines-per-function
  7. describe('RadioGroup', () => {
  8. function createRadioGroup(props) {
  9. return (
  10. <RadioGroup {...props}>
  11. <Radio value="A">A</Radio>
  12. <Radio value="B">B</Radio>
  13. <Radio value="C">C</Radio>
  14. </RadioGroup>
  15. );
  16. }
  17. function createRadioGroupByOption(props) {
  18. const options = [
  19. { label: 'A', value: 'A' },
  20. { label: 'B', value: 'B' },
  21. { label: 'C', value: 'C' },
  22. ];
  23. return <RadioGroup {...props} options={options} />;
  24. }
  25. it('fire change events when value changes', () => {
  26. const onChange = jest.fn();
  27. const wrapper = mount(
  28. createRadioGroup({
  29. onChange,
  30. })
  31. );
  32. const radios = wrapper.find('input');
  33. // uncontrolled component
  34. wrapper.setState({ value: 'B' });
  35. radios.at(0).simulate('change');
  36. expect(onChange.mock.calls.length).toBe(1);
  37. // controlled component
  38. wrapper.setProps({ value: 'A' });
  39. radios.at(1).simulate('change');
  40. expect(onChange.mock.calls.length).toBe(2);
  41. });
  42. it('both of radio and radioGroup will trigger onchange event when they exists', () => {
  43. const onChange = jest.fn();
  44. const onChangeRadioGroup = jest.fn();
  45. const wrapper = mount(
  46. <RadioGroup onChange={onChangeRadioGroup}>
  47. <Radio value="A" onChange={onChange}>
  48. A
  49. </Radio>
  50. <Radio value="B" onChange={onChange}>
  51. B
  52. </Radio>
  53. <Radio value="C" onChange={onChange}>
  54. C
  55. </Radio>
  56. </RadioGroup>
  57. );
  58. const radios = wrapper.find('input');
  59. // uncontrolled component
  60. wrapper.setState({ value: 'B' });
  61. radios.at(0).simulate('change');
  62. expect(onChange.mock.calls.length).toBe(1);
  63. expect(onChangeRadioGroup.mock.calls.length).toBe(1);
  64. // controlled component
  65. wrapper.setProps({ value: 'A' });
  66. radios.at(1).simulate('change');
  67. expect(onChange.mock.calls.length).toBe(2);
  68. });
  69. it('should only trigger once when in group with options', () => {
  70. const onChange = jest.fn();
  71. const options = [{ label: 'Bamboo', value: 'Bamboo' }];
  72. const wrapper = mount(<RadioGroup options={options} onChange={onChange} />);
  73. wrapper.find('input').simulate('change');
  74. expect(onChange).toHaveBeenCalledTimes(1);
  75. });
  76. it("won't fire change events when value not changes", () => {
  77. const onChange = jest.fn();
  78. const wrapper = mount(
  79. createRadioGroup({
  80. onChange,
  81. })
  82. );
  83. const radios = wrapper.find('input');
  84. // uncontrolled component
  85. wrapper.setState({ value: 'B' });
  86. radios.at(1).simulate('change');
  87. expect(onChange.mock.calls.length).toBe(0);
  88. // controlled component
  89. wrapper.setProps({ value: 'A' });
  90. radios.at(0).simulate('change');
  91. expect(onChange.mock.calls.length).toBe(0);
  92. });
  93. it('optional should correct render', () => {
  94. const wrapper = mount(createRadioGroupByOption());
  95. const radios = wrapper.find('input');
  96. expect(radios.length).toBe(3);
  97. });
  98. it('all children should have a name property', () => {
  99. const GROUP_NAME = 'radiogroup';
  100. const wrapper = mount(createRadioGroup({ name: GROUP_NAME }));
  101. wrapper.find('input[type="radio"]').forEach(el => {
  102. expect(el.props().name).toEqual(GROUP_NAME);
  103. });
  104. });
  105. it('radioGroup button style', () => {
  106. const radio = mount(
  107. createRadioGroup({ type: 'button' })
  108. );
  109. expect(radio.exists(`.${BASE_CLASS_PREFIX}-radio-buttonRadioGroup`)).toEqual(true);
  110. expect(radio.exists(`.${BASE_CLASS_PREFIX}-radio-inner-buttonRadio`)).toEqual(true);
  111. });
  112. it('radioGroup card style', () => {
  113. const radioGroup = mount(
  114. createRadioGroup({ type: 'card' })
  115. );
  116. expect(radioGroup.exists(`.${BASE_CLASS_PREFIX}-radio-cardRadioGroup`)).toEqual(true);
  117. radioGroup.unmount();
  118. const disabledRadioGroup = mount(
  119. createRadioGroup({ type: 'card', disabled: true, defaultValue: 'A' })
  120. );
  121. expect(
  122. disabledRadioGroup
  123. .find(`.${BASE_CLASS_PREFIX}-radio-cardRadioGroup`)
  124. .at(0)
  125. .exists(`.${BASE_CLASS_PREFIX}-radio-cardRadioGroup_checked_disabled`)
  126. ).toEqual(true);
  127. expect(
  128. disabledRadioGroup
  129. .find(`.${BASE_CLASS_PREFIX}-radio-cardRadioGroup`)
  130. .at(1)
  131. .exists(`.${BASE_CLASS_PREFIX}-radio-cardRadioGroup_checked_disabled`)
  132. ).toEqual(false);
  133. disabledRadioGroup.unmount();
  134. });
  135. it('radioGroup pure card style', () => {
  136. const radioGroup = mount(
  137. createRadioGroup({ type: 'pureCard' })
  138. );
  139. expect(radioGroup.exists(`.${BASE_CLASS_PREFIX}-radio-cardRadioGroup`)).toEqual(true);
  140. expect(radioGroup.exists(`.${BASE_CLASS_PREFIX}-radio-inner-pureCardRadio`)).toEqual(true);
  141. radioGroup.unmount();
  142. const disabledRadioGroup = mount(
  143. createRadioGroup({ type: 'pureCard', disabled: true, defaultValue: 'A' })
  144. );
  145. expect(
  146. disabledRadioGroup
  147. .find(`.${BASE_CLASS_PREFIX}-radio-cardRadioGroup`)
  148. .at(0)
  149. .exists(`.${BASE_CLASS_PREFIX}-radio-cardRadioGroup_checked_disabled`)
  150. ).toEqual(true);
  151. expect(
  152. disabledRadioGroup
  153. .find(`.${BASE_CLASS_PREFIX}-radio-cardRadioGroup`)
  154. .at(1)
  155. .exists(`.${BASE_CLASS_PREFIX}-radio-cardRadioGroup_checked_disabled`)
  156. ).toEqual(false);
  157. disabledRadioGroup.unmount();
  158. });
  159. it('The buttonSize of the button type radio', () => {
  160. const smallRadio = mount(
  161. createRadioGroup({ type: 'button', buttonSize: 'small' })
  162. );
  163. const middleRadio = mount(
  164. createRadioGroup({ type: 'button', buttonSize: 'middle' })
  165. );
  166. const largeRadio = mount(
  167. createRadioGroup({ type: 'button', buttonSize: 'large' })
  168. );
  169. expect(smallRadio.exists(`.${BASE_CLASS_PREFIX}-radio-addon-buttonRadio-small`)).toEqual(true);
  170. expect(middleRadio.exists(`.${BASE_CLASS_PREFIX}-radio-addon-buttonRadio-middle`)).toEqual(true);
  171. expect(largeRadio.exists(`.${BASE_CLASS_PREFIX}-radio-addon-buttonRadio-large`)).toEqual(true);
  172. });
  173. it('does not trigger Maximum update exceeded when setting radio-group\'s value to NaN', () => {
  174. const radioGroup = mount(
  175. createRadioGroup({ value: NaN }),
  176. );
  177. expect(radioGroup.exists(`${BASE_CLASS_PREFIX}-radio-checked`)).toEqual(false);
  178. });
  179. });