pagination.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. import { createTest, createVue, triggerEvent, destroyVM } from '../util';
  2. import Pagination from 'packages/pagination';
  3. describe('Pagination', () => {
  4. let vm;
  5. afterEach(() => {
  6. destroyVM(vm);
  7. });
  8. it('create', () => {
  9. vm = createTest(Pagination);
  10. const elm = vm.$el;
  11. // prev
  12. expect(elm.querySelector('button.btn-prev')).to.exist;
  13. // pager
  14. expect(elm.querySelector('ul.el-pager')).to.exist;
  15. // next
  16. expect(elm.querySelector('button.btn-next')).to.exist;
  17. // jumper
  18. expect(elm.querySelector('.el-pagination__jump')).to.exist;
  19. // ->
  20. expect(elm.querySelector('.el-pagination__rightwrapper')).to.exist;
  21. });
  22. it('set layout', () => {
  23. vm = createTest(Pagination, {
  24. layout: 'prev, pager, next'
  25. });
  26. const elm = vm.$el;
  27. // prev
  28. expect(elm.querySelector('button.btn-prev')).to.exist;
  29. // pager
  30. expect(elm.querySelector('ul.el-pager')).to.exist;
  31. // next
  32. expect(elm.querySelector('button.btn-next')).to.exist;
  33. // not found jumper
  34. expect(elm.querySelector('.el-pagination__jump')).to.not.exist;
  35. // not found ->
  36. expect(elm.querySelector('.el-pagination__rightwrapper')).to.not.exist;
  37. // not found total
  38. expect(elm.querySelector('.el-pagination__total')).to.not.exist;
  39. });
  40. it('layout: all in right, need clear float', () => {
  41. vm = createTest(Pagination, {
  42. layout: '->, prev, pager, next',
  43. total: 100
  44. }, true);
  45. const elm = vm.$el;
  46. let right_div = elm.querySelector('.el-pagination__rightwrapper');
  47. expect(elm.clientHeight > 0 && right_div.clientHeight > 0).to.equal(true);
  48. // elm 将来 padding 可能会变化, 所以使用 >= 来判定
  49. expect(elm.clientHeight >= right_div.clientHeight).to.equal(true);
  50. });
  51. it('custom slot', () => {
  52. vm = createVue({
  53. template: `
  54. <el-pagination
  55. layout="slot, prev, pager, next"
  56. :page-size="25"
  57. :total="100">
  58. <span class="slot-test">slot test</span>
  59. </el-pagination>
  60. `
  61. });
  62. expect(vm.$el.querySelector('.slot-test')).to.exist;
  63. });
  64. it('small', () => {
  65. vm = createTest(Pagination, {
  66. small: true
  67. });
  68. expect(vm.$el.classList.contains('el-pagination--small')).to.true;
  69. });
  70. it('pageSize', () => {
  71. vm = createTest(Pagination, {
  72. pageSize: 25,
  73. total: 100
  74. });
  75. expect(vm.$el.querySelectorAll('li.number')).to.length(4);
  76. });
  77. it('pageSize: NaN', () => {
  78. vm = createTest(Pagination, {
  79. pageSize: NaN,
  80. total: 100
  81. });
  82. const pagers = vm.$el.querySelectorAll('li.number');
  83. expect(pagers).to.length(7);
  84. });
  85. it('pageCount', () => {
  86. const vm = createTest(Pagination, {
  87. pageSize: 25,
  88. pageCount: 4
  89. });
  90. expect(vm.$el.querySelectorAll('li.number')).to.length(4);
  91. });
  92. it('pagerCount', () => {
  93. const vm = createTest(Pagination, {
  94. pageSize: 25,
  95. total: 1000,
  96. pagerCount: 21
  97. });
  98. expect(vm.$el.querySelectorAll('li.number')).to.length(21);
  99. });
  100. it('will work without total & page-count', (done) => {
  101. const vm = createTest(Pagination, {
  102. pageSize: 25,
  103. currentPage: 2
  104. });
  105. vm.$el.querySelector('.btn-prev').click();
  106. setTimeout(() => {
  107. vm.internalCurrentPage.should.be.equal(1);
  108. vm.$el.querySelector('.btn-prev').click();
  109. vm.internalCurrentPage.should.be.equal(1);
  110. done();
  111. }, 20);
  112. });
  113. it('currentPage', () => {
  114. vm = createTest(Pagination, {
  115. pageSize: 20,
  116. total: 200,
  117. currentPage: 3
  118. });
  119. expect(vm.$el.querySelector('li.number.active')).to.have.property('textContent').to.equal('3');
  120. });
  121. it('currentPage: NaN', () => {
  122. vm = createTest(Pagination, {
  123. pageSize: 20,
  124. total: 200,
  125. currentPage: NaN
  126. });
  127. expect(vm.$el.querySelector('li.number.active')).to.have.property('textContent').to.equal('1');
  128. expect(vm.$el.querySelectorAll('li.number')).to.length(7);
  129. });
  130. it('set currentPage & total', (done) => {
  131. vm = createVue({
  132. template: `
  133. <el-pagination
  134. @current-change="handleChange"
  135. :current-page="currentPage"
  136. :page-size="10"
  137. :total="100" />
  138. `,
  139. methods: {
  140. handleChange(val) {
  141. this.currentPage = val;
  142. this.page = val;
  143. },
  144. resetTotal() {
  145. this.total = 30;
  146. this.currentPage = 1;
  147. }
  148. },
  149. data() {
  150. return {
  151. currentPage: 10
  152. };
  153. }
  154. }, true);
  155. expect(vm.$el.querySelector('li.number.active')).to.have.property('textContent').to.equal('10');
  156. vm.resetTotal();
  157. setTimeout(() => {
  158. expect(vm.$el.querySelector('li.number.active')).to.have.property('textContent').to.equal('1');
  159. done();
  160. }, 50);
  161. });
  162. it('pageSizes', () => {
  163. vm = createTest(Pagination, {
  164. pageSizes: [10, 15, 35, 50],
  165. pageSize: 35,
  166. total: 1000,
  167. layout: 'sizes, prev, pager, next'
  168. });
  169. expect(vm.$el.querySelector('.el-select-dropdown__item.selected')).to.property('textContent').include('35');
  170. expect([].slice.call(vm.$el.querySelectorAll('.el-select-dropdown__item'))
  171. .map(node => parseInt(node.textContent, 10)))
  172. .to.deep.equal([10, 15, 35, 50]);
  173. });
  174. it('pageSizes:not found pageSize', () => {
  175. vm = createTest(Pagination, {
  176. pageSizes: [10, 15, 35, 50],
  177. pageSize: 24,
  178. total: 1000,
  179. layout: 'sizes, prev, pager, next'
  180. });
  181. expect(vm.$el.querySelector('.el-select-dropdown__item.selected')).to.property('textContent').include('10');
  182. });
  183. it('layout is empty', () => {
  184. vm = createTest(Pagination, {
  185. layout: ''
  186. });
  187. expect(vm.$el.textContent).to.empty;
  188. });
  189. it('jumper: change value', (done) => {
  190. vm = createVue({
  191. template: `
  192. <el-pagination
  193. @current-change="handleChange"
  194. :page-size="10"
  195. layout="pager, jumper"
  196. :total="100" />
  197. `,
  198. methods: {
  199. handleChange(val) {
  200. this.page = val;
  201. }
  202. },
  203. data() {
  204. return {
  205. page: 1,
  206. inputer: null
  207. };
  208. },
  209. mounted() {
  210. this.inputer = this.$children[0].$children[1].$children[0];
  211. }
  212. }, true);
  213. const input = vm.inputer;
  214. const changeValue = (value) => {
  215. input.$emit('input', value);
  216. input.$emit('change', value);
  217. };
  218. changeValue(1);
  219. setTimeout(() => {
  220. expect(input.value).to.equal(1);
  221. // 多次输入不在min-max区间内的数字
  222. changeValue(0);
  223. setTimeout(() => {
  224. expect(input.value).to.equal(1);
  225. changeValue(0);
  226. setTimeout(() => {
  227. expect(input.value).to.equal(1);
  228. changeValue(1000);
  229. setTimeout(() => {
  230. expect(input.value).to.equal(10);
  231. changeValue(1000);
  232. setTimeout(() => {
  233. expect(input.value).to.equal(10);
  234. done();
  235. }, 50);
  236. }, 50);
  237. }, 50);
  238. }, 50);
  239. }, 50);
  240. });
  241. it('event:current-change', (done) => {
  242. vm = createVue({
  243. template: `
  244. <el-pagination
  245. :total="1000"
  246. @current-change="change = true" />
  247. `,
  248. data() {
  249. return { change: false };
  250. }
  251. });
  252. const next = vm.$el.querySelector('button.btn-next');
  253. const prev = vm.$el.querySelector('button.btn-prev');
  254. expect(vm.change).to.false;
  255. // click 9
  256. let count = 9;
  257. while (--count) {
  258. next.click();
  259. }
  260. prev.click();
  261. setTimeout(() => {
  262. expect(vm.change).to.true;
  263. done();
  264. }, 50);
  265. });
  266. it('event:current-change after current page is manually updated', (done) => {
  267. vm = createVue({
  268. template: `
  269. <el-pagination
  270. :total="15"
  271. :current-page.sync="currentPage"
  272. @current-change="emitCount++" />
  273. `,
  274. data() {
  275. return {
  276. emitCount: 0,
  277. currentPage: 1
  278. };
  279. }
  280. });
  281. const next = vm.$el.querySelector('button.btn-next');
  282. next.click();
  283. setTimeout(() => {
  284. expect(vm.emitCount).to.equal(1);
  285. vm.currentPage = 1;
  286. setTimeout(() => {
  287. expect(vm.emitCount).to.equal(1);
  288. next.click();
  289. setTimeout(() => {
  290. expect(vm.emitCount).to.equal(2);
  291. done();
  292. }, 50);
  293. }, 50);
  294. }, 50);
  295. });
  296. it('event:size-change', done => {
  297. vm = createVue({
  298. template: `
  299. <el-pagination
  300. :total="100"
  301. layout="sizes, prev, pager, next"
  302. @size-change="trigger = true"
  303. :pageSize="10" />
  304. `,
  305. data() {
  306. return { trigger: false };
  307. }
  308. }, true);
  309. expect(vm.trigger).to.false;
  310. setTimeout(_ => {
  311. vm.$el.querySelectorAll('li.el-select-dropdown__item')[1].click();
  312. setTimeout(_ => {
  313. expect(vm.trigger).to.true;
  314. done();
  315. }, 50);
  316. }, 50);
  317. });
  318. it('event: prev and next click', done => {
  319. vm = createVue({
  320. template: `
  321. <el-pagination
  322. :total="100"
  323. layout="sizes, prev, pager, next"
  324. @prev-click="trigger = true"
  325. @next-click="trigger = true"
  326. :pageSize="10" />
  327. `,
  328. data() {
  329. return { trigger: false };
  330. }
  331. }, true);
  332. const prev = vm.$el.querySelector('.btn-prev');
  333. const next = vm.$el.querySelector('.btn-next');
  334. prev.click();
  335. setTimeout(_ => {
  336. expect(vm.trigger).to.false;
  337. next.click();
  338. setTimeout(_ => {
  339. expect(vm.trigger).to.true;
  340. done();
  341. }, 50);
  342. }, 50);
  343. });
  344. it('pageSize > total', () => {
  345. vm = createVue({
  346. template: `
  347. <el-pagination
  348. @current-change="handleChange"
  349. :page-size="1000"
  350. :total="0" />
  351. `,
  352. methods: {
  353. handleChange(val) {
  354. this.page = val;
  355. }
  356. },
  357. data() {
  358. return { page: 1 };
  359. }
  360. });
  361. const input = vm.$el.querySelector('.el-pagination__jump input');
  362. input.value = 2;
  363. triggerEvent(input, 'change');
  364. expect(vm.page).to.equal(1);
  365. input.value = '我好帅';
  366. triggerEvent(input, 'change');
  367. expect(vm.page).to.equal(1);
  368. });
  369. it('hideOnSinglePage', () => {
  370. vm = createVue({
  371. template: `
  372. <el-pagination
  373. hide-on-single-page
  374. :total="1" />
  375. `
  376. });
  377. expect(vm.$el.nodeType).to.be.equal(window.Node.COMMENT_NODE);
  378. });
  379. describe('click pager', () => {
  380. it('click ul', () => {
  381. vm = createTest(Pagination, {
  382. total: 1000
  383. }, true);
  384. vm.$el.querySelector('.el-pager').click();
  385. expect(vm.internalCurrentPage).to.equal(1);
  386. });
  387. it('click li', () => {
  388. vm = createTest(Pagination, {
  389. total: 1000
  390. }, true);
  391. vm.$el.querySelectorAll('.el-pager li.number')[1].click();
  392. expect(vm.internalCurrentPage).to.equal(2);
  393. });
  394. it('click next icon-more', () => {
  395. vm = createTest(Pagination, {
  396. total: 1000
  397. }, true);
  398. vm.$el.querySelector('.el-pager .more').click();
  399. expect(vm.internalCurrentPage).to.equal(6);
  400. });
  401. it('click prev icon-more', done => {
  402. vm = createTest(Pagination, {
  403. total: 1000
  404. }, true);
  405. vm.$el.querySelector('.btn-quicknext.more').click();
  406. setTimeout(_ => {
  407. expect(vm.$el.querySelector('.btn-quickprev.more')).to.exist;
  408. vm.$el.querySelector('.btn-quickprev.more').click();
  409. expect(vm.internalCurrentPage).to.equal(1);
  410. done();
  411. }, 50);
  412. });
  413. it('click last page', done => {
  414. vm = createTest(Pagination, {
  415. total: 1000
  416. }, true);
  417. const nodes = vm.$el.querySelectorAll('li.number');
  418. nodes[nodes.length - 1].click();
  419. setTimeout(_ => {
  420. expect(vm.$el.querySelector('.btn-quickprev.more')).to.exist;
  421. expect(vm.$el.querySelector('.btn-quicknext.more')).to.not.exist;
  422. done();
  423. }, 50);
  424. });
  425. });
  426. });