prevision / apps /prevision-api /src /common /interceptors /response.interceptor.spec.ts
github-actions[bot]
Automated Deploy: 23110475c742a5d4f03930b55bca09042215c1dd
1fe7075
Raw History Blame Contribute Delete
975 Bytes
import { lastValueFrom, of } from 'rxjs';
import type { CallHandler, ExecutionContext } from '@nestjs/common';
import { Paginated } from '../dto/paginated.dto.js';
import { ResponseInterceptor } from './response.interceptor.js';
const run = (body: unknown) =>
lastValueFrom(new ResponseInterceptor().intercept({} as ExecutionContext, { handle: () => of(body) } as CallHandler));
describe('ResponseInterceptor', () => {
it('wraps a body in { success, data }', async () => {
expect(await run({ id: 1 })).toEqual({ success: true, data: { id: 1 } });
});
it('turns a page into data plus pagination meta', async () => {
const page = new Paginated(['a', 'b'], 5, { page: 2, pageSize: 2 });
expect(await run(page)).toEqual({
success: true,
data: ['a', 'b'],
meta: { page: 2, pageSize: 2, total: 5, totalPages: 3 },
});
});
it('leaves empty responses empty', async () => {
expect(await run(undefined)).toBeUndefined();
});
});