File size: 975 Bytes
1fe7075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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();
  });
});