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