rating.test.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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(10);
  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-first`)
  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('controled 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('div[role="radio"]');
  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('sync hoverValue when click', () => {
  132. let props = {
  133. allowClear: true,
  134. defaultValue: 2,
  135. };
  136. const R = getRating(props);
  137. expect(R.state().value).toEqual(2);
  138. let stars = R.find('div[role="radio"]');
  139. const event = {};
  140. stars.at(1).simulate('click', event);
  141. expect(R.state().hoverValue).toEqual(0);
  142. expect(R.state().value).toEqual(0);
  143. stars.at(1).simulate('click', event);
  144. expect(R.state().hoverValue).toEqual(2);
  145. expect(R.state().value).toEqual(2);
  146. });
  147. it('tooltips', () => {
  148. let tooltips = ['terrible', 'bad', 'normal', 'good', 'wonderful'];
  149. let props = {
  150. tooltips,
  151. };
  152. const R = getRating(props);
  153. let stars = R.find('div[role="radio"]');
  154. const event = {};
  155. stars.at(1).simulate('mouseMove', event);
  156. expect(R.find(`.${BASE_CLASS_PREFIX}-tooltip-wrapper`).getDOMNode().textContent).toEqual(tooltips[1]);
  157. });
  158. it('onFocus & onBlur', () => {
  159. let onFocus = () => {};
  160. let onBlur = () => {};
  161. let spyOnFocus = sinon.spy(onFocus);
  162. let spyOnBlur = sinon.spy(onBlur);
  163. let props = {
  164. defaultValue: 2,
  165. onFocus: spyOnFocus,
  166. onBlur: spyOnBlur
  167. };
  168. const R = getRating(props);
  169. let ul = R.find('ul')
  170. ul.simulate('focus', {});
  171. expect(spyOnFocus.calledOnce).toBe(true);
  172. ul.simulate('blur', {});
  173. expect(spyOnBlur.calledOnce).toBe(true);
  174. });
  175. it('autoFocus & ref.focus() & ref.blur()', () => {
  176. let onFocus = () => {};
  177. let spyOnFocus = sinon.spy(onFocus);
  178. let props = {
  179. autoFocus: true,
  180. };
  181. const R = getRating(props);
  182. expect(document.activeElement.tagName).toEqual('UL');
  183. R.instance().blur();
  184. expect(document.activeElement.tagName).toEqual('BODY');
  185. R.instance().focus();
  186. expect(document.activeElement.tagName).toEqual('UL');
  187. });
  188. it('onKeyDown', () => {
  189. let onKeyDown = () => {};
  190. let spyOnKeydown = sinon.spy(onKeyDown);
  191. let props = {
  192. defaultValue: 2,
  193. onKeyDown: spyOnKeydown
  194. };
  195. const R = getRating(props);
  196. let ul = R.find('ul');
  197. let keyCodeLeft = 37;
  198. let keyCodeRight = 39;
  199. ul.simulate('keyDown', { keyCode: keyCodeLeft });
  200. expect(R.state().value).toEqual(1);
  201. ul.simulate('keyDown', { keyCode: keyCodeRight });
  202. expect(R.state().value).toEqual(2);
  203. expect(spyOnKeydown.callCount).toEqual(2);
  204. let allowHalfProps = {
  205. defaultValue: 2.5,
  206. allowHalf: true,
  207. };
  208. const HalfR = getRating(allowHalfProps);
  209. let halfUl = HalfR.find('ul');
  210. halfUl.simulate('keyDown', { keyCode: keyCodeLeft });
  211. expect(HalfR.state().value).toEqual(2);
  212. halfUl.simulate('keyDown', { keyCode: keyCodeRight });
  213. halfUl.simulate('keyDown', { keyCode: keyCodeRight });
  214. expect(HalfR.state().value).toEqual(3);
  215. });
  216. it('rtl mode', () => {
  217. // default
  218. let context = {
  219. direction: 'rtl'
  220. }
  221. let props = {
  222. defaultValue: 2,
  223. className: 'test'
  224. };
  225. const RWithWrapper = mount(<ConfigProvider direction='rtl'><Rating {...props}/></ConfigProvider>);
  226. let ul = RWithWrapper.find('ul');
  227. let keyCodeLeft = 37;
  228. let keyCodeRight = 39;
  229. ul.simulate('keyDown', { keyCode: keyCodeLeft });
  230. let R = RWithWrapper.find(Rating);
  231. expect(R.state().value).toEqual(3);
  232. ul.simulate('keyDown', { keyCode: keyCodeRight });
  233. expect(R.state().value).toEqual(2);
  234. // allowHalf
  235. let allowHalfProps = {
  236. defaultValue: 2.5,
  237. allowHalf: true,
  238. };
  239. let HalfRWithWrapper = mount(<ConfigProvider direction='rtl'><Rating {...allowHalfProps}/></ConfigProvider>);
  240. let halfUl = HalfRWithWrapper.find('ul');
  241. let HalfR = HalfRWithWrapper.find(Rating);
  242. let stars = HalfR.find('div[role="radio"]');
  243. halfUl.simulate('keyDown', { keyCode: keyCodeLeft });
  244. expect(HalfR.state().value).toEqual(3);
  245. halfUl.simulate('keyDown', { keyCode: keyCodeRight });
  246. halfUl.simulate('keyDown', { keyCode: keyCodeRight });
  247. expect(HalfR.state().value).toEqual(2);
  248. })
  249. });