tests(middleware-joi): add query tests
MODIFY
packages/middleware-joi/test/helpers/api.spec-util.ts
packages/middleware-joi/test/helpers/api.spec-util.ts
@@ -5,11 +5,28 @@ import {
matchType,
httpListener,
use,
- combineRoutes,
- bodyParser$
+ combineRoutes
} from '@marblejs/core';
+import { bodyParser$ } from '@marblejs/middleware-body';
import { map } from 'rxjs/operators';
+const getPost$: Effect = request$ =>
+ request$.pipe(
+ matchPath('/post'),
+ matchType('GET'),
+ use(
+ validator$({
+ query: {
+ page: Joi.number()
+ .integer()
+ .greater(1)
+ }
+ })
+ ),
+ map(req => req.query),
+ map(query => ({ status: 200, body: { ...query } }))
+ );
+
const getUser$: Effect = request$ =>
request$.pipe(
matchPath('/user/:id'),
@@ -29,6 +46,23 @@ const getUser$: Effect = request$ =>
map(params => ({ status: 200, body: { id: params!.id } }))
);
+const storePost$: Effect = request$ =>
+ request$.pipe(
+ matchPath('/post'),
+ matchType('POST'),
+ use(
+ validator$({
+ query: {
+ timestamp: Joi.date().required()
+ },
+ body: {
+ title: Joi.string().required()
+ }
+ })
+ ),
+ map(resp => ({ body: { ...resp.body, ...resp.query } }))
+ );
+
const postUser$: Effect = request$ =>
request$.pipe(
matchPath('/user'),
@@ -45,7 +79,7 @@ const postUser$: Effect = request$ =>
map(response => ({ body: response }))
);
-const api$ = combineRoutes('/api', [getUser$, postUser$]);
+const api$ = combineRoutes('/api', [getPost$, getUser$, postUser$, storePost$]);
const middlewares = [
bodyParser$,

MODIFY
packages/middleware-joi/test/integration/integration.spec.ts
packages/middleware-joi/test/integration/integration.spec.ts
@@ -39,6 +39,13 @@ describe('Joi middleware - Integration', () => {
.then(res => expect(res.body).toEqual(expected));
});
+ it('should send a get request with query', async () => {
+ return request(app)
+ .get('/api/post?page=2')
+ .set('token', token)
+ .expect(200, { page: 2 });
+ });
+
it('should send a post request with body', async () => {
return request(app)
.post('/api/user')
@@ -46,4 +53,13 @@ describe('Joi middleware - Integration', () => {
.send({ name: 'lucio' })
.expect(200, { name: 'lucio', passport: 'marble.js' });
});
+
+ it('should send a post request with query and body', async () => {
+ const time = Date.now();
+ return request(app)
+ .post(`/api/post?timestamp=${time}`)
+ .set('token', token)
+ .send({ title: 'Middleware Joi' })
+ .expect(200, { title: 'Middleware Joi', timestamp: new Date(time).toISOString() });
+ });
});

MODIFY
packages/middleware-joi/test/unit/body.spec.ts
packages/middleware-joi/test/unit/body.spec.ts
import { validator$, Joi } from '../../src';
import { of } from 'rxjs';
-import { HttpRequest, HttpResponse, bodyParser$ } from '@marblejs/core';
+import { HttpRequest, HttpResponse } from '@marblejs/core';
+import { bodyParser$ } from '@marblejs/middleware-body';
const MockReq = require('mock-req');
describe('Joi middleware - Body', () => {

MODIFY
packages/middleware-joi/test/unit/query.spec.ts
packages/middleware-joi/test/unit/query.spec.ts
import { validator$, Joi } from '../../src';
import { of } from 'rxjs';
-import { HttpRequest, HttpResponse, RouteParameters } from '@marblejs/core';
+import { HttpRequest, HttpResponse, RouteParameters, QueryParameters } from '@marblejs/core';
const reqMatched = (
url: string,
- query = '',
matchers: string[] = [],
- params: RouteParameters = {}
-) =>
- (({
- url: !!query ? `${url}?${query}` : url,
- matchers,
- params
- } as any) as HttpRequest);
+ params: RouteParameters = {},
+ query: QueryParameters = {},
+) => ({ url, matchers, params, query } as any as HttpRequest);
describe('Joi middleware - Query', () => {
it('should throws an error if dont pass a required field', done => {
expect.assertions(2);
- const req$ = of(reqMatched('/test', null, [], {}));
+ const req$ = of(reqMatched('/test', null, {}, {}));
const res = {} as HttpResponse;
const schema = {
query: Joi.object({
@@ -42,11 +37,10 @@ describe('Joi middleware - Query', () => {
);
});
- // FIXME: fix after merge PR
- it.skip('should throws an error if pass a invalid field', done => {
+ it('should throws an error if pass a invalid field', done => {
expect.assertions(2);
- const req$ = of(reqMatched('/test', 'id=@@@', [], {}));
+ const req$ = of(reqMatched('/test', null, {}, { id: '@@@' }));
const res = {} as HttpResponse;
const schema = {
query: Joi.object({
@@ -70,10 +64,10 @@ describe('Joi middleware - Query', () => {
);
});
- it.skip('should validates query with a valid value', done => {
+ it('should validates query with a valid value', done => {
expect.assertions(2);
- const req$ = of(reqMatched('/test', 'id=181782881DB38D84', [], {}));
+ const req$ = of(reqMatched('/test', null, {}, { id: '181782881DB38D84' }));
const res = {} as HttpResponse;
const schema = {
query: Joi.object({

tests(middleware-joi): add query tests