|
| 1 | +import { calculatePagination } from '../index' |
| 2 | + |
| 3 | +it('should return no pagination if not enough items are given', () => { |
| 4 | + const page = 1 |
| 5 | + const size = 5 |
| 6 | + const total = 2 |
| 7 | + const pagination = calculatePagination(page, size, total) |
| 8 | + |
| 9 | + expect(pagination).toBeNull() |
| 10 | +}) |
| 11 | +it('should return only a few pages', () => { |
| 12 | + const page = 2 |
| 13 | + const size = 5 |
| 14 | + const total = 15 |
| 15 | + const pagination = calculatePagination(page, size, total) |
| 16 | + |
| 17 | + expect(pagination?.pages).toHaveLength(3) |
| 18 | + |
| 19 | + expect(pagination.pages[0].number).toBe(1) |
| 20 | + expect(pagination.pages[0].isCurrentPage).toBeFalsy() |
| 21 | + expect(pagination.pages[1].number).toBe(2) |
| 22 | + expect(pagination.pages[1].isCurrentPage).toBeTruthy() |
| 23 | + expect(pagination.pages[2].number).toBe(3) |
| 24 | + expect(pagination.pages[2].isCurrentPage).toBeFalsy() |
| 25 | +}) |
| 26 | +it('should return the maximum number of pages', () => { |
| 27 | + const page = 5 |
| 28 | + const size = 5 |
| 29 | + const total = 50 |
| 30 | + const maxPages = 5 |
| 31 | + const pagination = calculatePagination(page, size, total, maxPages) |
| 32 | + |
| 33 | + expect(pagination?.pages).toHaveLength(5) |
| 34 | + |
| 35 | + expect(pagination.pages[0].number).toBe(3) |
| 36 | + expect(pagination.pages[0].isCurrentPage).toBeFalsy() |
| 37 | + expect(pagination.pages[1].number).toBe(4) |
| 38 | + expect(pagination.pages[1].isCurrentPage).toBeFalsy() |
| 39 | + expect(pagination.pages[2].number).toBe(5) |
| 40 | + expect(pagination.pages[2].isCurrentPage).toBeTruthy() |
| 41 | + expect(pagination.pages[3].number).toBe(6) |
| 42 | + expect(pagination.pages[3].isCurrentPage).toBeFalsy() |
| 43 | + expect(pagination.pages[4].number).toBe(7) |
| 44 | + expect(pagination.pages[4].isCurrentPage).toBeFalsy() |
| 45 | +}) |
| 46 | +it('should disable the previous link', () => { |
| 47 | + const page = 1 |
| 48 | + const size = 5 |
| 49 | + const total = 10 |
| 50 | + const pagination = calculatePagination(page, size, total) |
| 51 | + |
| 52 | + expect(pagination.previous.number).toBe(1) |
| 53 | + expect(pagination.previous.isDisabled).toBeTruthy() |
| 54 | + |
| 55 | + expect(pagination.next.number).toBe(2) |
| 56 | + expect(pagination.next.isDisabled).toBeFalsy() |
| 57 | + |
| 58 | + expect(pagination?.pages[0].isCurrentPage).toBeTruthy() |
| 59 | + expect(pagination?.pages[1].isCurrentPage).toBeFalsy() |
| 60 | +}) |
| 61 | +it('should disable the next link', () => { |
| 62 | + const page = 2 |
| 63 | + const size = 5 |
| 64 | + const total = 10 |
| 65 | + const pagination = calculatePagination(page, size, total) |
| 66 | + |
| 67 | + expect(pagination.previous.number).toBe(1) |
| 68 | + expect(pagination.previous.isDisabled).toBeFalsy() |
| 69 | + |
| 70 | + expect(pagination.next.number).toBe(2) |
| 71 | + expect(pagination.next.isDisabled).toBeTruthy() |
| 72 | + |
| 73 | + expect(pagination?.pages[0].isCurrentPage).toBeFalsy() |
| 74 | + expect(pagination?.pages[1].isCurrentPage).toBeTruthy() |
| 75 | +}) |
0 commit comments