nestjs-paginate/src/test/decorator.spec.ts

142 lines
4.5 KiB
TypeScript
Raw Normal View History

2020-06-26 21:25:03 +00:00
import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants'
import { HttpArgumentsHost, CustomParamFactory, ExecutionContext } from '@nestjs/common/interfaces'
import { Request as ExpressRequest } from 'express'
import { FastifyRequest } from 'fastify'
import { Paginate, PaginateQuery } from '../index'
2020-06-26 21:25:03 +00:00
2020-06-28 17:34:00 +00:00
// eslint-disable-next-line @typescript-eslint/ban-types
2020-06-26 21:25:03 +00:00
function getParamDecoratorFactory<T>(decorator: Function): CustomParamFactory {
class Test {
public test(@decorator() _value: T): void {
//
}
}
const args = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test')
return args[Object.keys(args)[0]].factory
}
const decoratorfactory = getParamDecoratorFactory<PaginateQuery>(Paginate)
function expressContextFactory(query: ExpressRequest['query']): Partial<ExecutionContext> {
2020-06-26 21:25:03 +00:00
const mockContext: Partial<ExecutionContext> = {
switchToHttp: (): HttpArgumentsHost =>
Object({
getRequest: (): Partial<ExpressRequest> =>
2020-06-26 21:25:03 +00:00
Object({
protocol: 'http',
get: () => 'localhost',
originalUrl: '/items?search=2423',
query: query,
}),
}),
}
return mockContext
}
function fastifyContextFactory(query: FastifyRequest['query']): Partial<ExecutionContext> {
const mockContext: Partial<ExecutionContext> = {
switchToHttp: (): HttpArgumentsHost =>
Object({
getRequest: (): Partial<FastifyRequest> =>
Object({
protocol: 'http',
hostname: 'localhost',
url: '/items?search=2423',
2020-06-26 21:25:03 +00:00
query: query,
}),
}),
}
return mockContext
}
describe('Decorator', () => {
it('should handle express undefined query fields', () => {
const context = expressContextFactory({})
2020-06-26 21:25:03 +00:00
const result: PaginateQuery = decoratorfactory(null, context)
expect(result).toStrictEqual({
page: undefined,
limit: undefined,
sortBy: undefined,
2020-06-28 17:34:00 +00:00
search: undefined,
2021-10-12 11:01:53 +00:00
searchBy: undefined,
2021-08-19 14:42:18 +00:00
filter: undefined,
2020-06-27 18:54:14 +00:00
path: 'http://localhost/items',
2020-06-26 21:25:03 +00:00
})
})
it('should handle fastify undefined query fields', () => {
const context = fastifyContextFactory({})
const result: PaginateQuery = decoratorfactory(null, context)
expect(result).toStrictEqual({
page: undefined,
limit: undefined,
sortBy: undefined,
search: undefined,
searchBy: undefined,
filter: undefined,
path: 'http://localhost/items',
})
})
it('should handle express defined query fields', () => {
const context = expressContextFactory({
page: '1',
limit: '20',
sortBy: ['id:ASC', 'createdAt:DESC'],
search: 'white',
'filter.name': '$not:$eq:Kitty',
'filter.createdAt': ['$gte:2020-01-01', '$lte:2020-12-31'],
})
const result: PaginateQuery = decoratorfactory(null, context)
expect(result).toStrictEqual({
page: 1,
limit: 20,
sortBy: [
['id', 'ASC'],
['createdAt', 'DESC'],
],
search: 'white',
searchBy: undefined,
path: 'http://localhost/items',
filter: {
name: '$not:$eq:Kitty',
createdAt: ['$gte:2020-01-01', '$lte:2020-12-31'],
},
})
})
it('should handle fastify defined query fields', () => {
const context = fastifyContextFactory({
2020-06-26 21:25:03 +00:00
page: '1',
limit: '20',
sortBy: ['id:ASC', 'createdAt:DESC'],
2020-06-28 17:34:00 +00:00
search: 'white',
2021-08-19 14:42:18 +00:00
'filter.name': '$not:$eq:Kitty',
'filter.createdAt': ['$gte:2020-01-01', '$lte:2020-12-31'],
2020-06-26 21:25:03 +00:00
})
const result: PaginateQuery = decoratorfactory(null, context)
expect(result).toStrictEqual({
page: 1,
limit: 20,
sortBy: [
['id', 'ASC'],
['createdAt', 'DESC'],
],
2020-06-28 17:34:00 +00:00
search: 'white',
2021-10-12 11:01:53 +00:00
searchBy: undefined,
2020-06-27 18:54:14 +00:00
path: 'http://localhost/items',
2021-08-19 14:42:18 +00:00
filter: {
name: '$not:$eq:Kitty',
createdAt: ['$gte:2020-01-01', '$lte:2020-12-31'],
},
2020-06-26 21:25:03 +00:00
})
})
})