tests(middleware-joi): add integration test
DELETE
packages/middleware-joi/__tests__/integration.spec.ts
-import { validator$, Joi } from '../src';
-
-describe('Joi middleware - Integration', () => {
- it('not implemented');
-});

ADD
packages/middleware-joi/test/helpers/api.spec-util.ts
+import { validator$, Joi } from '../../src';
+import {
+ Effect,
+ matchPath,
+ matchType,
+ httpListener,
+ use,
+ combineRoutes,
+ bodyParser$
+} from '@marblejs/core';
+import { map } from 'rxjs/operators';
+
+const getUser$: Effect = request$ =>
+ request$.pipe(
+ matchPath('/user/:id'),
+ matchType('GET'),
+ use(
+ validator$({
+ params: {
+ id: Joi.number()
+ .integer()
+ .min(1)
+ .max(10)
+ .required()
+ }
+ })
+ ),
+ map(req => req.params),
+ map(params => ({ status: 200, body: { id: params!.id } }))
+ );
+
+const postUser$: Effect = request$ =>
+ request$.pipe(
+ matchPath('/user'),
+ matchType('POST'),
+ use(
+ validator$({
+ body: {
+ name: Joi.string().required(),
+ passport: Joi.string().default('marble.js')
+ }
+ })
+ ),
+ map(req => req.body),
+ map(response => ({ body: response }))
+ );
+
+const api$ = combineRoutes('/api', [getUser$, postUser$]);
+
+const middlewares = [
+ bodyParser$,
+ validator$(
+ {
+ headers: {
+ token: Joi.string()
+ .token()
+ .required()
+ }
+ },
+ { stripUnknown: true }
+ )
+];
+const effects = [api$];
+export const server = () => httpListener({ middlewares, effects });

ADD
packages/middleware-joi/test/integration/integration.spec.ts
+import { server } from '../helpers/api.spec-util';
+import * as request from 'supertest';
+
+describe('Joi middleware - Integration', () => {
+ const app = server();
+ const token = '181782881DB38D84';
+
+ it('should fail without a token', async () => {
+ const expected = {
+ error: {
+ status: 400,
+ message: '"token" is required'
+ }
+ };
+
+ return request(app)
+ .get('/api/user/1')
+ .then(res => expect(res.body).toEqual(expected));
+ });
+
+ it('should send a get request with parameters', async () => {
+ return request(app)
+ .get('/api/user/1')
+ .set('token', token)
+ .expect(200, { id: 1 });
+ });
+
+ it('should send a get request with an invalid param', async () => {
+ const expected = {
+ error: {
+ status: 400,
+ message: '"id" must be less than or equal to 10'
+ }
+ };
+
+ return request(app)
+ .get('/api/user/11')
+ .set('token', token)
+ .then(res => expect(res.body).toEqual(expected));
+ });
+
+ it('should send a post request with body', async () => {
+ return request(app)
+ .post('/api/user')
+ .set('token', token)
+ .send({ name: 'lucio' })
+ .expect(200, { name: 'lucio', passport: 'marble.js' });
+ });
+});

RENAME
packages/middleware-joi/__tests__/unit/body.spec.ts
packages/middleware-joi/test/unit/body.spec.ts
@@ -31,9 +31,7 @@ describe('Joi middleware - Body', () => {
},
error => {
expect(error).toBeDefined();
- expect(error.message).toBe(
- 'child "token" fails because ["token" is required]'
- );
+ expect(error.message).toBe('"token" is required');
done();
}
);

RENAME
packages/middleware-joi/__tests__/unit/header.spec.ts
packages/middleware-joi/test/unit/header.spec.ts
@@ -30,9 +30,7 @@ describe('Joi middleware - Header', () => {
},
error => {
expect(error).toBeDefined();
- expect(error.message).toBe(
- 'child "token" fails because ["token" is required]'
- );
+ expect(error.message).toBe('"token" is required');
done();
}
);

RENAME
packages/middleware-joi/__tests__/unit/params.spec.ts
packages/middleware-joi/test/unit/params.spec.ts
@@ -36,9 +36,7 @@ describe('Joi middleware - Params', () => {
},
error => {
expect(error).toBeDefined();
- expect(error.message).toBe(
- 'child "id" fails because ["id" is required]'
- );
+ expect(error.message).toBe('"id" is required');
done();
}
);
@@ -64,7 +62,7 @@ describe('Joi middleware - Params', () => {
error => {
expect(error).toBeDefined();
expect(error.message).toBe(
- 'child "id" fails because ["id" must only contain alpha-numeric and underscore characters]'
+ '"id" must only contain alpha-numeric and underscore characters'
);
done();
}

RENAME
packages/middleware-joi/__tests__/unit/query.spec.ts
packages/middleware-joi/test/unit/query.spec.ts
@@ -36,9 +36,7 @@ describe('Joi middleware - Query', () => {
},
error => {
expect(error).toBeDefined();
- expect(error.message).toBe(
- 'child "id" fails because ["id" is required]'
- );
+ expect(error.message).toBe('"id" is required');
done();
}
);
@@ -65,7 +63,7 @@ describe('Joi middleware - Query', () => {
error => {
expect(error).toBeDefined();
expect(error.message).toBe(
- 'child "id" fails because ["id" must only contain alpha-numeric and underscore characters]'
+ '"id" must only contain alpha-numeric and underscore characters'
);
done();
}

RENAME
packages/middleware-joi/__tests__/unit/schema.spec.ts
packages/middleware-joi/test/unit/schema.spec.ts
MODIFY
packages/middleware-joi/tsconfig.json
packages/middleware-joi/tsconfig.json
],
"exclude": [
- "__tests__"
+ "test/"
]
}

tests(middleware-joi): add integration test