-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathdefaults.test.js
58 lines (50 loc) · 1.95 KB
/
defaults.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
const Joi = require('joi');
const request = require('supertest');
const { createServer } = require('../../_mocks_/express');
const generateUsername = context => {
if (context && context.firstname && context.lastname) {
return `${context.firstname.toLowerCase()}-${context.lastname.toLowerCase()}`;
}
};
generateUsername.description = 'generated username';
const schema = {
body: Joi.object({
username: Joi.string().default(generateUsername),
firstname: Joi.string().default('Andrew'),
lastname: Joi.string().default('Keig'),
created: Joi.date().default(Date.now),
registered: Joi.boolean().default(true),
values: Joi.array().default(['1']),
}),
};
const app = createServer('post', '/defaults', schema, { context: true }, {}, 'body');
describe('Defaults', () => {
describe('when the values are missing', () => {
it('should return the request with default values', async () => {
const response = await request(app)
.post('/defaults')
.send({ firstname: 'Jane', lastname: 'Doe' });
expect(response.statusCode).toBe(200);
expect(response.body.firstname).toBe('Jane');
expect(response.body.lastname).toBe('Doe');
expect(response.body.username).toBe('jane-doe');
expect(response.body).toHaveProperty('created');
expect(response.body.registered).toBe(true);
expect(response.body.values[0]).toBe('1');
});
});
describe('when no values', () => {
it('should return the request with default values', async () => {
const response = await request(app)
.post('/defaults')
.send({});
expect(response.statusCode).toBe(200);
expect(response.body.firstname).toBe('Andrew');
expect(response.body.lastname).toBe('Keig');
expect(response.body).not.toHaveProperty('username');
expect(response.body).toHaveProperty('created');
expect(response.body.registered).toBe(true);
expect(response.body.values[0]).toBe('1');
});
});
});