pagination.test.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import Pagination from '../index';
  2. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  3. import { genAfterEach, genBeforeEach, sleep } from '../../_test_/utils';
  4. const defaultPageSize = 10;
  5. // Pagination中第一层结构为LocaleConsumer,如果用shallow不好使
  6. function getPagination(props) {
  7. return mount(<Pagination {...props} />, { attachTo: document.getElementById('container') });
  8. }
  9. describe('Pagination', () => {
  10. // 主要验证渲染的pageItem数量,以及最后一页的页码
  11. it('Pagination total & pageSize', () => {
  12. let maxItemCount = 9;
  13. const total30 = mount(<Pagination total={30} />);
  14. const total70 = mount(<Pagination total={70} />);
  15. const total200 = mount(<Pagination total={200} />);
  16. const size50 = mount(<Pagination total={200} pageSize={50} />);
  17. function getPageChild(wrapper) {
  18. return wrapper.find('ul').children();
  19. }
  20. expect(getPageChild(total30)).toHaveLength(5);
  21. expect(getPageChild(total70)).toHaveLength(maxItemCount);
  22. expect(getPageChild(total200)).toHaveLength(maxItemCount);
  23. expect(getPageChild(size50)).toHaveLength(Math.ceil(200/50)+2);
  24. });
  25. it('Pagination with custom className & style', () => {
  26. const wrapper = mount(<Pagination className='test' style={{color: 'red'}} />);
  27. expect(wrapper.exists('.test')).toEqual(true);
  28. // 此处写ul.test 是因为如果直接find (.test)会找到两个,第一个是react instance,第二个才是真正的dom组件
  29. // 限定ul.test可以直接拿到真正的dom组件
  30. expect(wrapper.find('ul.test')).toHaveStyle('color', 'red');
  31. });
  32. it('render mini pagination', () => {
  33. const wrapper = mount(<Pagination size='small' total={90}></Pagination>);
  34. expect(wrapper.exists(`.${BASE_CLASS_PREFIX}-page-small`)).toEqual(true);
  35. });
  36. it('Pagination showTotal & showSizeChanger', () => {
  37. const wrapper = mount(<Pagination showTotal total={80} showSizeChanger/>);
  38. expect(wrapper.exists(`.${BASE_CLASS_PREFIX}-page-total`)).toEqual(true);
  39. expect(wrapper.exists(`.${BASE_CLASS_PREFIX}-page-switch`)).toEqual(true);
  40. });
  41. it('Pagination with defaultCurrentPage', () => {
  42. const wrapper = mount(<Pagination total={80} showTotal defaultCurrentPage={3}></Pagination>);
  43. expect(wrapper.state('currentPage')).toEqual(3);
  44. expect(wrapper.find('.semi-page').children().at(4).text()).toEqual('3')
  45. });
  46. it('auto disabled prev/next btn when first/last page', () => {
  47. const firstPage = mount(<Pagination total={30} />);
  48. const lastPage = mount(<Pagination total={200} defaultCurrentPage={20} />);
  49. const onlyOnePage = mount(<Pagination total={9} />);
  50. let DISABLED_CLASS = `.${BASE_CLASS_PREFIX}-page-item-disabled`;
  51. expect(firstPage.find('ul').children().first().exists(DISABLED_CLASS)).toEqual(true);
  52. expect(firstPage.find('ul').children().last().exists(DISABLED_CLASS)).toEqual(false);
  53. expect(lastPage.find('ul').children().first().exists(DISABLED_CLASS)).toEqual(false);
  54. expect(lastPage.find('ul').children().last().exists(DISABLED_CLASS)).toEqual(true);
  55. expect(onlyOnePage.find('ul').children().first().exists(DISABLED_CLASS)).toEqual(true);
  56. expect(onlyOnePage.find('ul').children().last().exists(DISABLED_CLASS)).toEqual(true);
  57. });
  58. it('hideOnSingePage', () => {
  59. let props = {
  60. total: 10,
  61. hideOnSingePage: true,
  62. };
  63. const pag = getPagination(props);
  64. expect(pag.exists('.semi-age')).toEqual(false);
  65. });
  66. it('nextText & prevText', () => {
  67. let props = {
  68. total: 200,
  69. nextText: 'next',
  70. prevText: 'prev',
  71. };
  72. const pag = getPagination(props);
  73. expect(pag.find('.semi-page-next').text()).toEqual('next');
  74. expect(pag.find('.semi-page-prev').text()).toEqual('prev');
  75. });
  76. it('showTotal & showSizeChanger', () => {
  77. let props = {
  78. total: 200,
  79. showTotal: true,
  80. showSizeChanger: true,
  81. };
  82. const pag = getPagination(props);
  83. expect(pag.exists('.semi-page-total')).toEqual(true);
  84. expect(pag.find('.semi-page-total').text()).toEqual('共 20 页')
  85. expect(pag.exists('.semi-page-switch')).toEqual(true);
  86. })
  87. it('dynamic change pageSize', () => {
  88. // let map = {
  89. // 10: 40,
  90. // 40: 100,
  91. // 100: 20,
  92. // 100: 10
  93. // }
  94. let props = {
  95. total: 200,
  96. showSizeChanger: true,
  97. pageSize: 10
  98. }
  99. const pag = getPagination(props);
  100. // pageSize 10 -> 40
  101. pag.setProps({ pageSize: 40 });
  102. pag.update();
  103. expect(pag.state().pageSize).toEqual(40);
  104. expect(pag.find('.semi-select-selection-text').children(0).text()).toEqual('40 条/页');
  105. expect(pag.find('.semi-page-item').children().length).toEqual((200/40) + 2);
  106. // pageSize 40 -> 100
  107. pag.setProps({ pageSize: 100 });
  108. pag.update();
  109. expect(pag.state().pageSize).toEqual(100);
  110. expect(pag.find('.semi-select-selection-text').children(0).text()).toEqual('100 条/页');
  111. expect(pag.find('.semi-page-item').children().length).toEqual((200/100) + 2);
  112. // pageSize 100 -> 20
  113. pag.setProps({ pageSize: 20 });
  114. pag.update();
  115. expect(pag.state().pageSize).toEqual(20);
  116. expect(pag.find('.semi-select-selection-text').children(0).text()).toEqual('20 条/页');
  117. // show ..., always 9
  118. expect(pag.find('.semi-page-item').children().length).toEqual(9);
  119. });
  120. it('onPageChange', () => {
  121. let tagetPage = 7;
  122. let onPageChange = value => {
  123. // debugger
  124. // console.log(value);
  125. };
  126. let spyOnPageChange = sinon.spy(onPageChange);
  127. const pag = mount(<Pagination total={70} onPageChange={spyOnPageChange} />);
  128. pag.find('li').at(7).simulate('click');
  129. expect(spyOnPageChange.calledOnce).toBe(true);
  130. expect(spyOnPageChange.calledWithMatch(tagetPage)).toBe(true);
  131. });
  132. // TODO select没有留出props控制是否自动打开,这块不太好测
  133. it('pageSizeOpts', () => {
  134. let props = {
  135. pageSizeOpts: [10, 20, 30],
  136. showSizeChanger: true,
  137. };
  138. const pag = getPagination(props);
  139. });
  140. // it('popoverPosition & popoverZIndex', () => {
  141. // let props = {
  142. // popoverPosition: 'right',
  143. // popoverZIndex: 888,
  144. // total: 200,
  145. // };
  146. // const pag = getPagination(props);
  147. // let rest = pag.find('.semi-page-item').children().at(5);
  148. // rest.simulate('mouseEnter');
  149. // });
  150. // TODO 不太好测,事件不好模拟触发
  151. // it('onPageSizeChange', async () => {
  152. // let onPageSizeChange = value => {};
  153. // let spyOnPageSizeChange = sinon.spy(onPageSizeChange);
  154. // let props = {
  155. // onPageSizeChange: spyOnPageSizeChange,
  156. // showSizeChanger: true,
  157. // total: 200,
  158. // }
  159. // const pag = getPagination(props);
  160. // const select = pag.find('.semi-select');
  161. // select.simulate('click');
  162. // // await sleep(200);
  163. // const optionList = document.querySelector('.semi-select-option-list');
  164. // done();
  165. // // pagination.find('li').at(7).simulate('click');
  166. // // expect(spyOnPageChange.calledOnce).toBe(true);
  167. // // expect(spyOnPageChange.calledWithMatch(tagetPage)).toBe(true);
  168. // });
  169. it('uncontrol mode - onChange', () => {
  170. let tagetPage = 20;
  171. let onChange = value => {};
  172. let spyOnChange = sinon.spy(onChange);
  173. let props = {
  174. onChange: spyOnChange,
  175. total: 200,
  176. };
  177. const pagination = getPagination(props);
  178. pagination.find('.semi-page-item').at(7).simulate('click');
  179. expect(spyOnChange.calledWithMatch(tagetPage)).toBe(true);
  180. });
  181. it('currentPage & onChange in controlled mode', () => {
  182. let onChange = value => {};
  183. let spyOnChange = sinon.spy(onChange);
  184. let props = {
  185. currentPage: 3,
  186. total: 200,
  187. onChange: spyOnChange
  188. };
  189. const pag = getPagination(props);
  190. pag.find('.semi-page-item').at(7).simulate('click');
  191. expect(spyOnChange.calledWithMatch(20)).toBe(true);
  192. expect(pag.state().currentPage).toEqual(3);
  193. pag.setProps({ currentPage: 4 });
  194. expect(pag.state().currentPage).toEqual(4);
  195. expect(pag.find('.semi-page-item-active').text()).toEqual("3");
  196. });
  197. it('showQuickJumper', () => {
  198. let spyOnChange = sinon.spy(() => {});
  199. let props = {
  200. total: 200,
  201. onChange: spyOnChange,
  202. showQuickJumper: true,
  203. };
  204. let quickJumpPage = 5;
  205. const pag = mount(<Pagination {...props} />);
  206. const jumper = pag.find('.semi-page .semi-page-quickjump-input-number.semi-input-number input');
  207. jumper.simulate('focus');
  208. jumper.simulate('change', { target: { value: quickJumpPage }});
  209. jumper.simulate('blur');
  210. expect(spyOnChange.calledWithMatch(quickJumpPage)).toBe(true);
  211. expect(pag.state().currentPage).toEqual(quickJumpPage);
  212. jumper.simulate('focus');
  213. jumper.simulate('change', { target: { value: props.total / 10 + 1 }});
  214. jumper.simulate('blur');
  215. expect(pag.state().currentPage).toEqual(props.total / 10);
  216. jumper.simulate('focus');
  217. jumper.simulate('change', { target: { value: NaN }});
  218. jumper.simulate('keypress', { key: 'Enter' });
  219. expect(pag.state().currentPage).toEqual(props.total / 10);
  220. jumper.simulate('focus');
  221. jumper.simulate('change', { target: { value: -1 }});
  222. jumper.simulate('keypress', { key: 'Enter' });
  223. expect(pag.state().currentPage).toEqual(1);
  224. pag.unmount();
  225. });
  226. })