forked from AndrewKeig/express-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.test.js
67 lines (56 loc) · 1.9 KB
/
context.test.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const Joi = require('joi');
const request = require('supertest');
const { createServer } = require('../../_mocks_/express');
describe('Context', () => {
describe('when passed as true', () => {
const schema = {
body: Joi.object({
id: Joi.string()
.valid(Joi.ref('$params.id'))
.required(),
}),
};
const app = createServer('post', '/context/:id', schema, { context: true }, {});
describe('when the schema contains a reference to the request object', () => {
it('should return a 200 ok response', async () => {
const response = await request(app)
.post('/context/1')
.send({ id: '1' });
expect(response.statusCode).toBe(200);
});
});
describe('when the schema contains an invalid reference to the request object', () => {
it('should return a 400 response', async () => {
const response = await request(app)
.post('/context/1')
.send({ id: '2' });
expect(response.statusCode).toBe(400);
expect(response.body.details.body[0].path[0]).toBe('id');
});
});
});
describe('when passed as false', () => {
const schema = {
body: Joi.object({
id: Joi.string()
.valid(Joi.ref('$foo.bar'))
.required(),
}),
};
const joiContext = {
foo: {
bar: 'abc',
},
};
const app = createServer('post', '/context/:id', schema, { context: false }, { context: joiContext });
// Regression test for https://github.com/AndrewKeig/express-validation/issues/153
describe('and the joi options specify another context', () => {
it('should not overwrite the context passed in the joi options object', async () => {
const response = await request(app)
.post('/context/abc')
.send({ id: 'abc' });
expect(response.statusCode).toBe(200);
});
});
});
});