pagination.spec.js 10 KB

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