-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists-endpoint.spec.js
114 lines (101 loc) · 3.6 KB
/
lists-endpoint.spec.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const knex = require('knex');
const app = require('../src/app');
const helpers = require('./test-helpers');
describe('Lists Endpoint', function () {
let db;
const { testLists, testProjects, testUsers } = helpers.makeFixtures();
before('make knex instance', () => {
db = knex({
client: 'pg',
connection: process.env.TEST_DATABASE_URL,
});
app.set('db', db);
});
after('disconnect from db', () => db.destroy());
before('cleanup', () => helpers.cleanTables(db));
afterEach('cleanup', () => helpers.cleanTables(db));
describe(`POST /api/lists`, () => {
context(`List Validation`, () => {
beforeEach('Insert users', () => {
return db.into('gtpro_users').insert(testUsers);
});
beforeEach('Insert projects', () => {
return db.into('gtpro_projects').insert(testProjects);
});
beforeEach('Insert lists', () => {
return db.into('gtpro_lists').insert(testLists);
});
const requiredFields = ['title', 'project_id'];
requiredFields.forEach((field) => {
const registerAttemptBody = {
title: 'test title',
project_id: 'test project_id',
};
it(`responds with 400 required error when '${field}' is missing`, () => {
delete registerAttemptBody[field];
return supertest(app)
.post('/api/lists')
.send(registerAttemptBody)
.expect(400, {
error: { message: `${field} is required` },
});
});
context(`Happy path`, () => {
it(`responds 201, serialized list`, () => {
const testList = testLists[0];
const testUser = testUsers[0];
const newList = {
title: 'test title',
project_id: testList.project_id,
};
return supertest(app)
.post('/api/lists')
.send(newList)
.expect(201)
.expect((res) => {
expect(res.body).to.have.property('id');
expect(res.body.title).to.eql(newList.title);
expect(res.body.project_id).to.eql(newList.project_id);
expect(res.headers.location).to.eql(
`/api/lists/${res.body.id}`
);
const expectedDate = new Date().toLocaleString();
const actualDate = new Date(
res.body.date_created
).toLocaleString();
expect(actualDate).to.eql(expectedDate);
})
.expect((res) =>
db
.from('gtpro_lists')
.select('*')
.where({ id: res.body.id })
.first()
.then((row) => {
expect(row.title).to.eql(newList.title);
expect(row.project_id).to.eql(newList.project_id);
const expectedDate = new Date().toLocaleString();
const actualDate = new Date(
row.date_created
).toLocaleString();
expect(actualDate).to.eql(expectedDate);
})
);
});
});
});
});
describe(`GET /api/lists`, () => {
context(`Given no lists`, () => {
it(`responds with 200 and an empty list`, () => {
return supertest(app).get('/api/lists').expect(200, []);
});
});
context('Given there are lists in the database', () => {
beforeEach('Insert lists', () =>
db.into('gtpro_lists').insert(testLists)
);
});
});
});
});