Skip to content

Commit e0a0e99

Browse files
committed
test(server-nest): add ApiHandlerService test cases
1 parent a20e743 commit e0a0e99

1 file changed

Lines changed: 217 additions & 3 deletions

File tree

packages/server/tests/adapter/nestjs.test.ts

Lines changed: 217 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Test } from '@nestjs/testing';
22
import { loadSchema } from '@zenstackhq/testtools';
3-
import { ZenStackModule, ENHANCED_PRISMA } from '../../src/nestjs';
3+
import { ZenStackModule, ENHANCED_PRISMA, ApiHandlerService } from '../../src/nestjs';
4+
import { HttpAdapterHost, REQUEST } from '@nestjs/core';
5+
import RESTApiHandler from '../../src/api/rest';
46

5-
describe('NestJS adapter tests', () => {
6-
const schema = `
7+
const schema = `
78
model User {
89
id Int @id @default(autoincrement())
910
posts Post[]
@@ -21,6 +22,7 @@ describe('NestJS adapter tests', () => {
2122
}
2223
`;
2324

25+
describe('NestJS adapter tests', () => {
2426
it('anonymous', async () => {
2527
const { prisma, enhanceRaw } = await loadSchema(schema);
2628

@@ -209,3 +211,215 @@ describe('NestJS adapter tests', () => {
209211
await expect(postSvc.findAll()).resolves.toHaveLength(2);
210212
});
211213
});
214+
215+
describe('ApiHandlerService tests', () => {
216+
it('with default option', async () => {
217+
const { prisma, enhanceRaw } = await loadSchema(schema);
218+
219+
await prisma.user.create({
220+
data: {
221+
posts: {
222+
create: [
223+
{ title: 'post1', published: true },
224+
{ title: 'post2', published: false },
225+
],
226+
},
227+
},
228+
});
229+
230+
const moduleRef = await Test.createTestingModule({
231+
imports: [
232+
ZenStackModule.registerAsync({
233+
useFactory: (prismaService) => ({ getEnhancedPrisma: () => enhanceRaw(prismaService) }),
234+
inject: ['PrismaService'],
235+
extraProviders: [
236+
{
237+
provide: 'PrismaService',
238+
useValue: prisma,
239+
},
240+
],
241+
}),
242+
],
243+
providers: [
244+
{
245+
provide: REQUEST,
246+
useValue: {}
247+
},
248+
{
249+
provide: HttpAdapterHost,
250+
useValue: {
251+
httpAdapter: {
252+
getRequestHostname: jest.fn().mockReturnValue('localhost'),
253+
getRequestUrl: jest.fn().mockReturnValue('/post/findMany'),
254+
getRequestMethod: jest.fn().mockReturnValue('GET'),
255+
}
256+
}
257+
},
258+
ApiHandlerService,
259+
],
260+
}).compile();
261+
262+
const service = await moduleRef.resolve<ApiHandlerService>(ApiHandlerService);
263+
expect(await service.handleRequest()).toEqual({
264+
data: [{
265+
id: 1,
266+
title: 'post1',
267+
published: true,
268+
authorId: 1,
269+
}]
270+
})
271+
})
272+
273+
it('with rest api handler', async () => {
274+
const { prisma, enhanceRaw, modelMeta, zodSchemas } = await loadSchema(schema);
275+
276+
await prisma.user.create({
277+
data: {
278+
posts: {
279+
create: [
280+
{ title: 'post1', published: true },
281+
{ title: 'post2', published: false },
282+
],
283+
},
284+
},
285+
});
286+
287+
const moduleRef = await Test.createTestingModule({
288+
imports: [
289+
ZenStackModule.registerAsync({
290+
useFactory: (prismaService) => ({ getEnhancedPrisma: () => enhanceRaw(prismaService) }),
291+
inject: ['PrismaService'],
292+
extraProviders: [
293+
{
294+
provide: 'PrismaService',
295+
useValue: prisma,
296+
},
297+
],
298+
}),
299+
],
300+
providers: [
301+
{
302+
provide: REQUEST,
303+
useValue: {}
304+
},
305+
{
306+
provide: HttpAdapterHost,
307+
useValue: {
308+
httpAdapter: {
309+
getRequestHostname: jest.fn().mockReturnValue('localhost'),
310+
getRequestUrl: jest.fn().mockReturnValue('/post'),
311+
getRequestMethod: jest.fn().mockReturnValue('GET'),
312+
}
313+
}
314+
},
315+
ApiHandlerService,
316+
],
317+
}).compile();
318+
319+
const service = await moduleRef.resolve<ApiHandlerService>(ApiHandlerService);
320+
expect(await service.handleRequest({
321+
handler: RESTApiHandler({
322+
endpoint: 'http://localhost',
323+
}),
324+
modelMeta,
325+
zodSchemas,
326+
})).toEqual({
327+
jsonapi: {
328+
version: "1.1"
329+
},
330+
data: [{
331+
type: 'post',
332+
id: 1,
333+
attributes: {
334+
title: 'post1',
335+
published: true,
336+
authorId: 1,
337+
},
338+
links: {
339+
self: 'http://localhost/post/1',
340+
},
341+
relationships: {
342+
author: {
343+
data: {
344+
id: 1,
345+
type: 'user',
346+
},
347+
links: {
348+
related: 'http://localhost/post/1/author',
349+
self: 'http://localhost/post/1/relationships/author',
350+
}
351+
}
352+
}
353+
}],
354+
links: {
355+
first: "http://localhost/post?page%5Blimit%5D=100",
356+
last: "http://localhost/post?page%5Boffset%5D=0",
357+
next: null,
358+
prev: null,
359+
self: "http://localhost/post"
360+
},
361+
meta: {
362+
total: 1
363+
}
364+
})
365+
})
366+
367+
it('option baseUrl', async () => {
368+
const { prisma, enhanceRaw } = await loadSchema(schema);
369+
370+
await prisma.user.create({
371+
data: {
372+
posts: {
373+
create: [
374+
{ title: 'post1', published: true },
375+
{ title: 'post2', published: false },
376+
],
377+
},
378+
},
379+
});
380+
381+
const moduleRef = await Test.createTestingModule({
382+
imports: [
383+
ZenStackModule.registerAsync({
384+
useFactory: (prismaService) => ({ getEnhancedPrisma: () => enhanceRaw(prismaService) }),
385+
inject: ['PrismaService'],
386+
extraProviders: [
387+
{
388+
provide: 'PrismaService',
389+
useValue: prisma,
390+
},
391+
],
392+
}),
393+
],
394+
providers: [
395+
{
396+
provide: REQUEST,
397+
useValue: {}
398+
},
399+
{
400+
provide: HttpAdapterHost,
401+
useValue: {
402+
httpAdapter: {
403+
getRequestHostname: jest.fn().mockReturnValue('localhost'),
404+
getRequestUrl: jest.fn().mockReturnValue('/api/rpc/post/findMany'),
405+
getRequestMethod: jest.fn().mockReturnValue('GET'),
406+
}
407+
}
408+
},
409+
ApiHandlerService,
410+
],
411+
}).compile();
412+
413+
const service = await moduleRef.resolve<ApiHandlerService>(ApiHandlerService);
414+
expect(await service.handleRequest({
415+
baseUrl: '/api/rpc'
416+
})).toEqual({
417+
data: [{
418+
id: 1,
419+
title: 'post1',
420+
published: true,
421+
authorId: 1,
422+
}]
423+
})
424+
})
425+
})

0 commit comments

Comments
 (0)