|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +/// <reference types="@types/jest" /> |
| 3 | + |
| 4 | +import { type ModelMeta } from '@zenstackhq/runtime'; |
| 5 | +import { loadSchema, run } from '@zenstackhq/testtools'; |
| 6 | +import makeHandler from '../../src/api/rest'; |
| 7 | + |
| 8 | +describe('REST server tests - pagination over 100', () => { |
| 9 | + let prisma: any; |
| 10 | + let zodSchemas: any; |
| 11 | + let modelMeta: ModelMeta; |
| 12 | + let handler: (any: any) => Promise<{ status: number; body: any }>; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + run('npx prisma migrate reset --force'); |
| 16 | + run('npx prisma db push'); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('REST TEMP', () => { |
| 20 | + const schema = ` |
| 21 | + model User { |
| 22 | + myId String @id @default(cuid()) |
| 23 | + createdAt DateTime @default (now()) |
| 24 | + updatedAt DateTime @updatedAt |
| 25 | + email String @unique @email |
| 26 | + } |
| 27 | + `; |
| 28 | + |
| 29 | + beforeAll(async () => { |
| 30 | + const params = await loadSchema(schema); |
| 31 | + |
| 32 | + prisma = params.prisma; |
| 33 | + zodSchemas = params.zodSchemas; |
| 34 | + modelMeta = params.modelMeta; |
| 35 | + |
| 36 | + const _handler = makeHandler({ endpoint: 'http://localhost/api' }); |
| 37 | + handler = (args) => |
| 38 | + _handler({ ...args, zodSchemas, modelMeta, url: new URL(`http://localhost/${args.path}`) }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns only 15 items when limit set to 5', async () => { |
| 42 | + // Create users first |
| 43 | + for (const i of Array(150).keys()) { |
| 44 | + await prisma.user.create({ |
| 45 | + data: { |
| 46 | + myId: `user${i}`, |
| 47 | + email: `user${i}@abc.com`, |
| 48 | + }, |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + const r = await handler({ |
| 53 | + method: 'get', |
| 54 | + path: '/user', |
| 55 | + query: { ['page[limit]']: '15' }, |
| 56 | + prisma, |
| 57 | + }); |
| 58 | + expect(r.body.data).toHaveLength(15); |
| 59 | + //expect(r.body.meta.total).toBe(150); |
| 60 | + // expect(r.body.links).toMatchObject({ |
| 61 | + // first: 'http://localhost/api/user?page%5Blimit%5D=5', |
| 62 | + // last: 'http://localhost/api/user?page%5Boffset%5D=145', |
| 63 | + // prev: null, |
| 64 | + // next: 'http://localhost/api/user?page%5Boffset%5D=5&page%5Blimit%5D=5', |
| 65 | + // }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns 125 items when limit set to 125', async () => { |
| 69 | + // Create users first |
| 70 | + for (const i of Array(150).keys()) { |
| 71 | + await prisma.user.create({ |
| 72 | + data: { |
| 73 | + myId: `user${i}`, |
| 74 | + email: `user${i}@abc.com`, |
| 75 | + }, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + const r = await handler({ |
| 80 | + method: 'get', |
| 81 | + path: '/user', |
| 82 | + query: { ['page[limit]']: '125' }, |
| 83 | + prisma, |
| 84 | + }); |
| 85 | + expect(r.body.data).toHaveLength(125); |
| 86 | + //expect(r.body.meta.total).toBe(150); |
| 87 | + // expect(r.body.links).toMatchObject({ |
| 88 | + // first: 'http://localhost/api/user?page%5Blimit%5D=5', |
| 89 | + // last: 'http://localhost/api/user?page%5Boffset%5D=145', |
| 90 | + // prev: null, |
| 91 | + // next: 'http://localhost/api/user?page%5Boffset%5D=5&page%5Blimit%5D=5', |
| 92 | + // }); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments