pagination.spec.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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('will work without total & page-count', (done) => {
  93. const vm = createTest(Pagination, {
  94. pageSize: 25,
  95. currentPage: 2
  96. });
  97. vm.$el.querySelector('.btn-prev').click();
  98. setTimeout(() => {
  99. vm.internalCurrentPage.should.be.equal(1);
  100. vm.$el.querySelector('.btn-prev').click();
  101. vm.internalCurrentPage.should.be.equal(1);
  102. done();
  103. }, 20);
  104. });
  105. it('currentPage', () => {
  106. vm = createTest(Pagination, {
  107. pageSize: 20,
  108. total: 200,
  109. currentPage: 3
  110. });
  111. expect(vm.$el.querySelector('li.number.active')).to.have.property('textContent').to.equal('3');
  112. });
  113. it('currentPage: NaN', () => {
  114. vm = createTest(Pagination, {
  115. pageSize: 20,
  116. total: 200,
  117. currentPage: NaN
  118. });
  119. expect(vm.$el.querySelector('li.number.active')).to.have.property('textContent').to.equal('1');
  120. expect(vm.$el.querySelectorAll('li.number')).to.length(7);
  121. });
  122. it('set currentPage & total', (done) => {
  123. vm = createVue({
  124. template: `
  125. <el-pagination
  126. @current-change="handleChange"
  127. :current-page="currentPage"
  128. :page-size="10"
  129. :total="100" />
  130. `,
  131. methods: {
  132. handleChange(val) {
  133. this.currentPage = val;
  134. this.page = val;
  135. },
  136. resetTotal() {
  137. this.total = 30;
  138. this.currentPage = 1;
  139. }
  140. },
  141. data() {
  142. return {
  143. currentPage: 10
  144. };
  145. }
  146. }, true);
  147. expect(vm.$el.querySelector('li.number.active')).to.have.property('textContent').to.equal('10');
  148. vm.resetTotal();
  149. setTimeout(() => {
  150. expect(vm.$el.querySelector('li.number.active')).to.have.property('textContent').to.equal('1');
  151. done();
  152. }, 50);
  153. });
  154. it('pageSizes', () => {
  155. vm = createTest(Pagination, {
  156. pageSizes: [10, 15, 35, 50],
  157. pageSize: 35,
  158. total: 1000,
  159. layout: 'sizes, prev, pager, next'
  160. });
  161. expect(vm.$el.querySelector('.el-select-dropdown__item.selected')).to.property('textContent').include('35');
  162. expect([].slice.call(vm.$el.querySelectorAll('.el-select-dropdown__item'))
  163. .map(node => parseInt(node.textContent, 10)))
  164. .to.deep.equal([10, 15, 35, 50]);
  165. });
  166. it('pageSizes:not found pageSize', () => {
  167. vm = createTest(Pagination, {
  168. pageSizes: [10, 15, 35, 50],
  169. pageSize: 24,
  170. total: 1000,
  171. layout: 'sizes, prev, pager, next'
  172. });
  173. expect(vm.$el.querySelector('.el-select-dropdown__item.selected')).to.property('textContent').include('10');
  174. });
  175. it('layout is empty', () => {
  176. vm = createTest(Pagination, {
  177. layout: ''
  178. });
  179. expect(vm.$el.textContent).to.empty;
  180. });
  181. it('jumper: change value', (done) => {
  182. vm = createVue({
  183. template: `
  184. <el-pagination
  185. @current-change="handleChange"
  186. :page-size="10"
  187. layout="pager, jumper"
  188. :total="100" />
  189. `,
  190. methods: {
  191. handleChange(val) {
  192. this.page = val;
  193. }
  194. },
  195. data() {
  196. return {
  197. page: 1,
  198. inputer: null
  199. };
  200. },
  201. mounted() {
  202. this.inputer = this.$children[0].$children[1].$children[0];
  203. }
  204. }, true);
  205. const input = vm.inputer;
  206. const changeValue = (value) => {
  207. input.$emit('input', value);
  208. input.setCurrentValue(value);
  209. input.$emit('change', value);
  210. };
  211. changeValue(1);
  212. setTimeout(() => {
  213. expect(input.value).to.equal(1);
  214. // 多次输入不在min-max区间内的数字
  215. changeValue(0);
  216. setTimeout(() => {
  217. expect(input.value).to.equal(1);
  218. changeValue(0);
  219. setTimeout(() => {
  220. expect(input.value).to.equal(1);
  221. changeValue(1000);
  222. setTimeout(() => {
  223. expect(input.value).to.equal(10);
  224. changeValue(1000);
  225. setTimeout(() => {
  226. expect(input.value).to.equal(10);
  227. done();
  228. }, 50);
  229. }, 50);
  230. }, 50);
  231. }, 50);
  232. }, 50);
  233. });
  234. it('event:current-change', (done) => {
  235. vm = createVue({
  236. template: `
  237. <el-pagination
  238. :total="1000"
  239. @current-change="change = true" />
  240. `,
  241. data() {
  242. return { change: false };
  243. }
  244. });
  245. const next = vm.$el.querySelector('button.btn-next');
  246. const prev = vm.$el.querySelector('button.btn-prev');
  247. expect(vm.change).to.false;
  248. // click 9
  249. let count = 9;
  250. while (--count) {
  251. next.click();
  252. }
  253. prev.click();
  254. setTimeout(() => {
  255. expect(vm.change).to.true;
  256. done();
  257. }, 50);
  258. });
  259. it('event:size-change', done => {
  260. vm = createVue({
  261. template: `
  262. <el-pagination
  263. :total="100"
  264. layout="sizes, prev, pager, next"
  265. @size-change="trigger = true"
  266. :pageSize="10" />
  267. `,
  268. data() {
  269. return { trigger: false };
  270. }
  271. }, true);
  272. expect(vm.trigger).to.false;
  273. setTimeout(_ => {
  274. vm.$el.querySelectorAll('li.el-select-dropdown__item')[1].click();
  275. setTimeout(_ => {
  276. expect(vm.trigger).to.true;
  277. done();
  278. }, 50);
  279. }, 50);
  280. });
  281. it('pageSize > total', () => {
  282. vm = createVue({
  283. template: `
  284. <el-pagination
  285. @current-change="handleChange"
  286. :page-size="1000"
  287. :total="0" />
  288. `,
  289. methods: {
  290. handleChange(val) {
  291. this.page = val;
  292. }
  293. },
  294. data() {
  295. return { page: 1 };
  296. }
  297. });
  298. const input = vm.$el.querySelector('.el-pagination__jump input');
  299. input.value = 2;
  300. triggerEvent(input, 'change');
  301. expect(vm.page).to.equal(1);
  302. input.value = '我好帅';
  303. triggerEvent(input, 'change');
  304. expect(vm.page).to.equal(1);
  305. });
  306. describe('click pager', () => {
  307. it('click ul', () => {
  308. vm = createTest(Pagination, {
  309. total: 1000
  310. }, true);
  311. vm.$el.querySelector('.el-pager').click();
  312. expect(vm.internalCurrentPage).to.equal(1);
  313. });
  314. it('click li', () => {
  315. vm = createTest(Pagination, {
  316. total: 1000
  317. }, true);
  318. vm.$el.querySelectorAll('.el-pager li.number')[1].click();
  319. expect(vm.internalCurrentPage).to.equal(2);
  320. });
  321. it('click next icon-more', () => {
  322. vm = createTest(Pagination, {
  323. total: 1000
  324. }, true);
  325. vm.$el.querySelector('.el-pager .more').click();
  326. expect(vm.internalCurrentPage).to.equal(6);
  327. });
  328. it('click prev icon-more', done => {
  329. vm = createTest(Pagination, {
  330. total: 1000
  331. }, true);
  332. vm.$el.querySelector('.btn-quicknext.more').click();
  333. setTimeout(_ => {
  334. expect(vm.$el.querySelector('.btn-quickprev.more')).to.exist;
  335. vm.$el.querySelector('.btn-quickprev.more').click();
  336. expect(vm.internalCurrentPage).to.equal(1);
  337. done();
  338. }, 50);
  339. });
  340. it('click last page', done => {
  341. vm = createTest(Pagination, {
  342. total: 1000
  343. }, true);
  344. const nodes = vm.$el.querySelectorAll('li.number');
  345. nodes[nodes.length - 1].click();
  346. setTimeout(_ => {
  347. expect(vm.$el.querySelector('.btn-quickprev.more')).to.exist;
  348. expect(vm.$el.querySelector('.btn-quicknext.more')).to.not.exist;
  349. done();
  350. }, 50);
  351. });
  352. });
  353. });