| 
1 |  | -import { calcSkip, getMeta } from '@app/lib/prisma/helpers/pagination';  | 
 | 1 | +import { calcSkip, getMeta, paginate } from '@app/lib/prisma/helpers/pagination';  | 
 | 2 | +import type { ModelDelegates } from '@app/lib/prisma/types';  | 
 | 3 | +import type { ValueOf } from 'type-fest';  | 
 | 4 | + | 
 | 5 | +const makeModelMock = () => ({  | 
 | 6 | +  count: jest.fn(),  | 
 | 7 | +  findMany: jest.fn(),  | 
 | 8 | +});  | 
2 | 9 | 
 
  | 
3 | 10 | describe('calcSkip', () => {  | 
4 | 11 |   it('should return 0 when page is 1', () => {  | 
@@ -82,3 +89,92 @@ describe('getMeta', () => {  | 
82 | 89 |     });  | 
83 | 90 |   });  | 
84 | 91 | });  | 
 | 92 | + | 
 | 93 | +describe('paginate', () => {  | 
 | 94 | +  let model: {  | 
 | 95 | +    count: jest.Mock;  | 
 | 96 | +    findMany: jest.Mock;  | 
 | 97 | +  };  | 
 | 98 | + | 
 | 99 | +  beforeEach(() => {  | 
 | 100 | +    model = makeModelMock();  | 
 | 101 | +  });  | 
 | 102 | + | 
 | 103 | +  it('should calculate skip correctly', () => {  | 
 | 104 | +    expect(calcSkip(1, 20)).toBe(0);  | 
 | 105 | +    expect(calcSkip(2, 20)).toBe(20);  | 
 | 106 | +    expect(calcSkip(3, 20)).toBe(40);  | 
 | 107 | +  });  | 
 | 108 | + | 
 | 109 | +  it('should calculate metadata correctly', () => {  | 
 | 110 | +    const meta = getMeta(100, 2, 20);  | 
 | 111 | +    expect(meta).toEqual({  | 
 | 112 | +      total: 100,  | 
 | 113 | +      lastPage: 5,  | 
 | 114 | +      currentPage: 2,  | 
 | 115 | +      perPage: 20,  | 
 | 116 | +      prev: 1,  | 
 | 117 | +      next: 3,  | 
 | 118 | +    });  | 
 | 119 | +  });  | 
 | 120 | + | 
 | 121 | +  it('should paginate results correctly', async () => {  | 
 | 122 | +    model.count.mockResolvedValue(100);  | 
 | 123 | +    model.findMany.mockResolvedValue(['item1', 'item2']);  | 
 | 124 | + | 
 | 125 | +    const result = await paginate(model as unknown as ValueOf<ModelDelegates>, {}, { page: 2, perPage: 20 });  | 
 | 126 | + | 
 | 127 | +    expect(model.count).toHaveBeenCalledWith({ where: undefined });  | 
 | 128 | +    expect(model.findMany).toHaveBeenCalledWith({  | 
 | 129 | +      take: 20,  | 
 | 130 | +      skip: 20,  | 
 | 131 | +    });  | 
 | 132 | +    expect(result).toEqual({  | 
 | 133 | +      data: ['item1', 'item2'],  | 
 | 134 | +      meta: {  | 
 | 135 | +        total: 100,  | 
 | 136 | +        lastPage: 5,  | 
 | 137 | +        currentPage: 2,  | 
 | 138 | +        perPage: 20,  | 
 | 139 | +        prev: 1,  | 
 | 140 | +        next: 3,  | 
 | 141 | +      },  | 
 | 142 | +    });  | 
 | 143 | +  });  | 
 | 144 | + | 
 | 145 | +  it('should handle default values for page and perPage', async () => {  | 
 | 146 | +    model.count.mockResolvedValue(50);  | 
 | 147 | +    model.findMany.mockResolvedValue(['item1', 'item2', 'item3', 'item4']);  | 
 | 148 | + | 
 | 149 | +    const result = await paginate(model as unknown as ValueOf<ModelDelegates>, {}, {});  | 
 | 150 | + | 
 | 151 | +    expect(result.meta.currentPage).toBe(1);  | 
 | 152 | +    expect(result.meta.perPage).toBe(20);  | 
 | 153 | +  });  | 
 | 154 | + | 
 | 155 | +  it('should handle pagination when there is only one page', async () => {  | 
 | 156 | +    model.count.mockResolvedValue(10);  | 
 | 157 | +    model.findMany.mockResolvedValue(['item1', 'item2']);  | 
 | 158 | + | 
 | 159 | +    const result = await paginate(model as unknown as ValueOf<ModelDelegates>, {}, { page: 1, perPage: 10 });  | 
 | 160 | + | 
 | 161 | +    expect(result.meta).toEqual({  | 
 | 162 | +      total: 10,  | 
 | 163 | +      lastPage: 1,  | 
 | 164 | +      currentPage: 1,  | 
 | 165 | +      perPage: 10,  | 
 | 166 | +      prev: null,  | 
 | 167 | +      next: null,  | 
 | 168 | +    });  | 
 | 169 | +  });  | 
 | 170 | + | 
 | 171 | +  it('should return empty data if no items found', async () => {  | 
 | 172 | +    model.count.mockResolvedValue(0);  | 
 | 173 | +    model.findMany.mockResolvedValue([]);  | 
 | 174 | + | 
 | 175 | +    const result = await paginate(model as unknown as ValueOf<ModelDelegates>, {}, { page: 1, perPage: 10 });  | 
 | 176 | + | 
 | 177 | +    expect(result.data).toEqual([]);  | 
 | 178 | +    expect(result.meta.total).toBe(0);  | 
 | 179 | +  });  | 
 | 180 | +});  | 
0 commit comments