Skip to content

Commit c38a7c2

Browse files
committed
test: tests for paginate helper
1 parent 8f2fa19 commit c38a7c2

File tree

5 files changed

+109
-11
lines changed

5 files changed

+109
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"pug": "^3.0.3",
5858
"reflect-metadata": "^0.2.0",
5959
"rxjs": "^7.8.1",
60+
"type-fest": "^4.25.0",
6061
"uuid": "^9.0.1"
6162
},
6263
"devDependencies": {
@@ -95,7 +96,6 @@
9596
"ts-loader": "^9.4.3",
9697
"ts-node": "^10.9.1",
9798
"tsconfig-paths": "^4.2.0",
98-
"type-fest": "^4.25.0",
9999
"typescript": "^5.1.3"
100100
},
101101
"prisma": {

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/prisma/helpers/pagination.spec.ts

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
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+
});
29

310
describe('calcSkip', () => {
411
it('should return 0 when page is 1', () => {
@@ -82,3 +89,92 @@ describe('getMeta', () => {
8289
});
8390
});
8491
});
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+
});

src/lib/prisma/helpers/pagination.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment */
22
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
3-
import type { Prisma, PrismaClient } from '@prisma/client';
3+
import type { ModelDelegates } from '@app/lib/prisma/types';
4+
import type { Prisma } from '@prisma/client';
45

56
export interface PaginatedResult<T> {
67
data: T[];
@@ -14,11 +15,6 @@ export interface PaginatedResult<T> {
1415
};
1516
}
1617

17-
// see: https://github.com/prisma/prisma/issues/6980
18-
type ModelDelegates = {
19-
[K in Prisma.ModelName]: PrismaClient[Uncapitalize<K>];
20-
};
21-
2218
export type PaginateOptions = { page?: number; perPage?: number };
2319
export type PaginateFunction = <T, K extends Prisma.ModelName>(
2420
model: ModelDelegates[K],

src/lib/prisma/types/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { Prisma, PrismaClient } from '@prisma/client';
2+
3+
// see: https://github.com/prisma/prisma/issues/6980
4+
export type ModelDelegates = {
5+
[K in Prisma.ModelName]: PrismaClient[Uncapitalize<K>];
6+
};

0 commit comments

Comments
 (0)