1
0

rating.test.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import React from 'react';
  2. import { mount } from 'enzyme';
  3. import Rating from '../index';
  4. import { ConfigProvider } from '../../index';
  5. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  6. const getRating = props => {
  7. return mount(<Rating {...props} />);
  8. };
  9. describe('Rating', () => {
  10. it('custom className & style', () => {
  11. let props = {
  12. className: 'test',
  13. style: {
  14. color: 'red',
  15. },
  16. };
  17. const R = getRating(props);
  18. expect(R.exists(`.${BASE_CLASS_PREFIX}-rating.test`)).toEqual(true);
  19. expect(R.find(`.${BASE_CLASS_PREFIX}-rating.test`)).toHaveStyle('color', 'red');
  20. });
  21. it('custom count', () => {
  22. const R1 = mount(<Rating count={10} />);
  23. expect(R1.find(`.${BASE_CLASS_PREFIX}-rating`).children().length).toEqual(11);
  24. });
  25. it('different sizes', () => {
  26. const sR = getRating({ size: 'small' });
  27. const dR = getRating();
  28. expect(sR.exists(`.${BASE_CLASS_PREFIX}-rating-star-small`)).toEqual(true);
  29. expect(dR.exists(`.${BASE_CLASS_PREFIX}-rating-star-default`)).toEqual(true);
  30. });
  31. it('defaultValue', () => {
  32. const R4 = getRating({ defaultValue: 4 });
  33. expect(R4.state().value).toEqual(4);
  34. expect(R4.find(`.${BASE_CLASS_PREFIX}-rating-star-full`).length).toEqual(4);
  35. });
  36. it('disabled', () => {
  37. const DR = mount(<Rating disabled defaultValue={3} />);
  38. expect(DR.exists(`.${BASE_CLASS_PREFIX}-rating-disabled`)).toEqual(true);
  39. });
  40. it('allowHalf', () => {
  41. let props = {
  42. allowHalf: true,
  43. defaultValue: 3.5,
  44. };
  45. const R35 = getRating(props);
  46. expect(R35.state().value).toEqual(3.5);
  47. expect(R35.find(`.${BASE_CLASS_PREFIX}-rating-star-full`).length).toEqual(3);
  48. expect(R35.find(`.${BASE_CLASS_PREFIX}-rating-star-half`).length).toEqual(1);
  49. });
  50. it('custom character', () => {
  51. let props = {
  52. character: '赞',
  53. };
  54. const R = getRating(props);
  55. expect(
  56. R.find(`.${BASE_CLASS_PREFIX}-rating-star-second`)
  57. .at(0)
  58. .getDOMNode().textContent
  59. ).toEqual('赞');
  60. });
  61. it('onChange callback', () => {
  62. let onChange = v => {
  63. // debugger;
  64. };
  65. let spyOnChange = sinon.spy(onChange);
  66. let props = {
  67. onChange: spyOnChange,
  68. defaultValue: 3,
  69. };
  70. const R = getRating(props);
  71. let stars = R.find('div[role="radio"]');
  72. const event = {};
  73. stars.at(0).simulate('click', event);
  74. expect(R.state().value).toEqual(1);
  75. expect(spyOnChange.calledOnce).toBe(true);
  76. expect(spyOnChange.calledWithMatch(1)).toBe(true);
  77. });
  78. it('controlled value', () => {
  79. const R = getRating({ value: 2 });
  80. expect(R.state().value).toEqual(2);
  81. expect(R.find(`.${BASE_CLASS_PREFIX}-rating-star-full`).length).toEqual(2);
  82. R.setProps({ value: 5 });
  83. expect(R.state().value).toEqual(5);
  84. expect(R.find(`.${BASE_CLASS_PREFIX}-rating-star-full`).length).toEqual(5);
  85. });
  86. it('onHoverChange', () => {
  87. let onHoverChange = v => {};
  88. let spyHoverChange = sinon.spy(onHoverChange);
  89. let props = {
  90. onHoverChange: spyHoverChange,
  91. };
  92. const R = getRating(props);
  93. let stars = R.find('div[role="radio"]');
  94. const event = {};
  95. stars.at(1).simulate('mouseMove', event);
  96. expect(spyHoverChange.calledWithMatch(2)).toBe(true);
  97. let ul = R.find('ul');
  98. ul.simulate('mouseLeave', {});
  99. expect(spyHoverChange.callCount).toBe(2);
  100. R.unmount();
  101. });
  102. it('onHoverChange + allowHalf', () => {
  103. let onHoverChange = v => {};
  104. let spyHoverChange = sinon.spy(onHoverChange);
  105. let props = {
  106. onHoverChange: spyHoverChange,
  107. allowHalf: true
  108. };
  109. const R = getRating(props);
  110. let stars = R.find(`.${BASE_CLASS_PREFIX}-rating-star-wrapper`);
  111. const event = {};
  112. stars.at(1).simulate('mouseMove', event);
  113. expect(spyHoverChange.calledWithMatch(2)).toBe(true);
  114. let ul = R.find('ul');
  115. ul.simulate('mouseLeave', {});
  116. expect(spyHoverChange.callCount).toBe(2);
  117. R.unmount();
  118. });
  119. it('allowClear', () => {
  120. let props = {
  121. allowClear: true,
  122. defaultValue: 2,
  123. };
  124. const R = getRating(props);
  125. expect(R.state().value).toEqual(2);
  126. let stars = R.find('div[role="radio"]');
  127. const event = {};
  128. stars.at(1).simulate('click', event);
  129. expect(R.state().value).toEqual(0);
  130. });
  131. it('tooltips', () => {
  132. let tooltips = ['terrible', 'bad', 'normal', 'good', 'wonderful'];
  133. let props = {
  134. tooltips,
  135. };
  136. const R = getRating(props);
  137. let stars = R.find('div[role="radio"]');
  138. const event = {};
  139. stars.at(1).simulate('mouseMove', event);
  140. expect(R.find(`.${BASE_CLASS_PREFIX}-tooltip-wrapper`).getDOMNode().textContent).toEqual(tooltips[1]);
  141. });
  142. it('onFocus & onBlur', () => {
  143. let onFocus = () => {};
  144. let onBlur = () => {};
  145. let spyOnFocus = sinon.spy(onFocus);
  146. let spyOnBlur = sinon.spy(onBlur);
  147. let props = {
  148. defaultValue: 2,
  149. onFocus: spyOnFocus,
  150. onBlur: spyOnBlur
  151. };
  152. const R = getRating(props);
  153. let ul = R.find('ul')
  154. ul.simulate('focus', {});
  155. expect(spyOnFocus.calledOnce).toBe(true);
  156. ul.simulate('blur', {});
  157. expect(spyOnBlur.calledOnce).toBe(true);
  158. });
  159. it('onKeyDown', () => {
  160. let onKeyDown = () => {};
  161. let spyOnKeydown = sinon.spy(onKeyDown);
  162. let props = {
  163. defaultValue: 2,
  164. onKeyDown: spyOnKeydown
  165. };
  166. const R = getRating(props);
  167. let ul = R.find('ul');
  168. ul.simulate('keyDown', { key: 'ArrowLeft' });
  169. expect(R.state().value).toEqual(1);
  170. ul.simulate('keyDown', { key: 'ArrowRight' });
  171. expect(R.state().value).toEqual(2);
  172. expect(spyOnKeydown.callCount).toEqual(2);
  173. let allowHalfProps = {
  174. defaultValue: 2.5,
  175. allowHalf: true,
  176. };
  177. const HalfR = getRating(allowHalfProps);
  178. let halfUl = HalfR.find('ul');
  179. halfUl.simulate('keyDown', { key: 'ArrowLeft' });
  180. expect(HalfR.state().value).toEqual(2);
  181. halfUl.simulate('keyDown', { key: 'ArrowRight' });
  182. halfUl.simulate('keyDown', { key: 'ArrowRight' });
  183. expect(HalfR.state().value).toEqual(3);
  184. });
  185. it('rtl mode', () => {
  186. // default
  187. let context = {
  188. direction: 'rtl'
  189. }
  190. let props = {
  191. defaultValue: 2,
  192. className: 'test'
  193. };
  194. const RWithWrapper = mount(<ConfigProvider direction='rtl'><Rating {...props}/></ConfigProvider>);
  195. let ul = RWithWrapper.find('ul');
  196. ul.simulate('keyDown', { key: 'ArrowLeft' });
  197. let R = RWithWrapper.find(Rating);
  198. expect(R.state().value).toEqual(3);
  199. ul.simulate('keyDown', { key: 'ArrowRight' });
  200. expect(R.state().value).toEqual(2);
  201. // allowHalf
  202. let allowHalfProps = {
  203. defaultValue: 2.5,
  204. allowHalf: true,
  205. };
  206. let HalfRWithWrapper = mount(<ConfigProvider direction='rtl'><Rating {...allowHalfProps}/></ConfigProvider>);
  207. let halfUl = HalfRWithWrapper.find('ul');
  208. let HalfR = HalfRWithWrapper.find(Rating);
  209. let stars = HalfR.find('div[role="radio"]');
  210. halfUl.simulate('keyDown', { key: 'ArrowLeft' });
  211. expect(HalfR.state().value).toEqual(3);
  212. halfUl.simulate('keyDown', { key: 'ArrowRight' });
  213. halfUl.simulate('keyDown', { key: 'ArrowRight' });
  214. expect(HalfR.state().value).toEqual(2);
  215. })
  216. it('click much times', () => {
  217. const R = getRating({});
  218. let stars = R.find('div[role="radio"]');
  219. const event = {};
  220. stars.at(1).simulate('click', event);
  221. expect(R.find(`.${BASE_CLASS_PREFIX}-rating-star-full`).length).toEqual(2);
  222. stars.at(1).simulate('click', event);
  223. expect(R.find(`.${BASE_CLASS_PREFIX}-rating-star-full`).length).toEqual(0);
  224. stars.at(1).simulate('click', event);
  225. expect(R.find(`.${BASE_CLASS_PREFIX}-rating-star-full`).length).toEqual(2);
  226. });
  227. });