Issues (8)

test/book.spec.js (1 issue)

1
// During the rest the en variable is set to test
2
/* global describe it beforeEach */
3
process.env.NODE_ENV = 'test';
4
5
const Book = require('../app/models/book');
6
7
// Require the dev-dependencies
8
const chai = require('chai');
9
const chaiHttp = require('chai-http');
10
const server = require('../server');
11
12
const should = chai.should();
13
14
chai.use(chaiHttp);
15
16
// Our parent block
17
18
describe('Books', () => {
19
    beforeEach((done) => {
20
        Book.remove({}, (err) => {
0 ignored issues
show
The parameter err is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
21
            done();
22
        });
23
    });
24
25
    /**
26
     * Test the /GET route
27
     */
28
29
    describe('/GET book', () => {
30
        it('it should GET all the books', (done) => {
31
            chai.request(server)
32
                .get('/api/books')
33
                .end((err, res) => {
34
                    res.should.have.status(200);
35
                    res.body.should.be.a('array');
36
                    res.body.length.should.be.eql(0);
37
                    done();
38
                });
39
        });
40
    });
41
    /**
42
     * Test the /POST route
43
     */
44
    describe('/POST book', () => {
45
        it('it should not POST a book without pages field', (done) => {
46
            const book = {
47
                title: 'The Lord of the Rings',
48
                author: 'J.R.R Tolkien',
49
                year: 1954
50
            };
51
            chai.request(server)
52
                .post('/api/books')
53
                .send(book)
54
                .end((err, res) => {
55
                    res.should.have.status(200);
56
                    res.body.should.be.a('object');
57
                    res.body.should.have.property('errors');
58
                    res.body.errors.should.have.property('pages');
59
                    res.body.errors.pages.should.have.property('kind').eql('required');
60
                    done();
61
                });
62
        });
63
        it('it should POST a book', (done) => {
64
            const book = {
65
                title: 'The Lord of the Rings',
66
                author: 'J.R.R Tolkien',
67
                year: 1954,
68
                pages: 1170
69
            };
70
            chai.request(server)
71
                .post('/api/books')
72
                .send(book)
73
                .end((err, res) => {
74
                    res.should.have.status(200);
75
                    res.body.should.be.a('object');
76
                    res.body.should.have.property('message').eql('Book successfully added!');
77
                    res.body.book.should.have.property('title');
78
                    res.body.book.should.have.property('author');
79
                    res.body.book.should.have.property('pages');
80
                    res.body.book.should.have.property('year');
81
                    done();
82
                });
83
        });
84
    });
85
    /**
86
     * Test the GET/:id route
87
     */
88
    describe('/GET/:id book', () => {
89
        it('it should GET a book by the given id', (done) => {
90
            const book = new Book({
91
                title: 'The Lord of the Rings',
92
                author: 'J.R.R Tolkien',
93
                year: 1954,
94
                pages: 1170
95
            });
96
            book.save((err, book) => {
97
                chai.request(server)
98
                    .get(`/api/books/${book.id}`)
99
                    .end((err, res) => {
100
                        res.should.have.status(200);
101
                        res.body.should.be.a('object');
102
                        res.body.should.have.property('title');
103
                        res.body.should.have.property('author');
104
                        res.body.should.have.property('pages');
105
                        res.body.should.have.property('year');
106
                        res.body.should.have.property('_id').eql(book.id);
107
                        done();
108
                    });
109
110
            });
111
        });
112
113
        it('it should GET an error if the book ID is not valid', (done) => {
114
            chai.request(server)
115
                .get('/api/books/90')
116
                .end((err, res) => {
117
                    res.should.have.status(400);
118
                    res.body.should.be.a('object');
119
                    res.body.should.have.property('message');
120
                    res.body.should.have.property('name');
121
                    res.body.should.have.property('kind').eql('ObjectId');
122
                    res.body.should.have.property('value').eql('90');
123
                    res.body.should.have.property('path').eql('_id');
124
                    done();
125
                });
126
        });
127
128
        it('it should GET not found if the book ID don\'t exist', (done) => {
129
            chai.request(server)
130
                .get('/api/books/589e02e559f531603fe40322')
131
                .end((err, res) => {
132
                    res.should.have.status(404);
133
                    res.body.should.be.a('object');
134
                    res.body.should.have.property('message').eql('Book not found!');
135
                    res.body.should.have.property('id').eql('589e02e559f531603fe40322');
136
                    done();
137
                });
138
        });
139
    });
140
    /**
141
     * Test the /PUT/:id route
142
     */
143
    describe('/PUT/:id book', () => {
144
        it('it should UPDATE a book given the id', (done) => {
145
            const book = new Book({
146
                title: 'The Chronicles of Narnia',
147
                author: 'C.S. Lewis',
148
                year: 1950,
149
                pages: 778
150
            });
151
            book.save((err, book) => {
152
                chai.request(server)
153
                    .put(`/api/books/${book.id}`)
154
                    .send({
155
                        year: 1952
156
                    })
157
                    .end((err, res) => {
158
                        res.should.have.status(200);
159
                        res.body.should.be.a('object');
160
                        res.body.should.have.property('message').eql('Book updated!');
161
                        res.body.book.should.have.property('year').eql(1952);
162
                        done();
163
                    });
164
            });
165
        });
166
167
        it('it should return an error if the book year is invalid', (done) => {
168
            const book = new Book({
169
                title: 'The Chronicles of Narnia',
170
                author: 'C.S. Lewis',
171
                year: 1950,
172
                pages: 778
173
            });
174
            book.save((err, book) => {
175
                chai.request(server)
176
                    .put(`/api/books/${book.id}`)
177
                    .send({
178
                        year: '1952a'
179
                    })
180
                    .end((err, res) => {
181
                        res.should.have.status(500);
182
                        res.body.should.be.a('object');
183
                        res.body.should.have.property('errors');
184
                        res.body.errors.should.have.property('year');
185
                        res.body.errors.year.should.have.property('kind').eql('Number');
186
                        done();
187
                    });
188
            });
189
        });
190
191
        it('it should return an error if the book ID is not valid', (done) => {
192
            chai.request(server)
193
                .put('/api/books/90')
194
                .send({
195
                    year: 2009
196
                })
197
                .end((err, res) => {
198
                    res.should.have.status(400);
199
                    res.body.should.be.a('object');
200
                    res.body.should.have.property('message');
201
                    res.body.should.have.property('name');
202
                    res.body.should.have.property('kind').eql('ObjectId');
203
                    res.body.value.should.have.property('_id').eql('90');
204
                    res.body.should.have.property('path').eql('_id');
205
                    done();
206
                });
207
        });
208
209
        it('it should return not found if the book ID don\'t exist', (done) => {
210
            chai.request(server)
211
                .put('/api/books/589e02e559f531603fe40322')
212
                .send({
213
                    year: 2010
214
                })
215
                .end((err, res) => {
216
                    res.should.have.status(404);
217
                    res.body.should.be.a('object');
218
                    res.body.should.have.property('message').eql('Book not found!');
219
                    res.body.should.have.property('id').eql('589e02e559f531603fe40322');
220
                    done();
221
                });
222
        });
223
    });
224
225
    /**
226
     * Test the /DELETE/:id route
227
     */
228
    describe('/DELETE/:id Book', () => {
229
        it('it should DELETE a book given the id', (done) => {
230
            const book = new Book({
231
                title: 'The Chronicles of Narnia',
232
                author: 'C.S. Lewis',
233
                year: 1950,
234
                pages: 778
235
            });
236
            book.save((err, book) => {
237
                chai.request(server)
238
                    .delete(`/api/books/${book.id}`)
239
                    .end((err, res) => {
240
                        res.should.have.status(200);
241
                        res.body.should.be.a('object');
242
                        res.body.should.have.property('message').eql('Book successfully deleted!');
243
                        res.body.result.should.have.property('ok').eql(1);
244
                        res.body.result.should.have.property('n').eql(1);
245
                        done();
246
                    });
247
            });
248
        });
249
        it('it should return an error if the book ID is not valid', (done) => {
250
            chai.request(server)
251
                .delete('/api/books/90')
252
                .end((err, res) => {
253
                    res.should.have.status(400);
254
                    res.body.should.be.a('object');
255
                    res.body.should.have.property('message');
256
                    res.body.should.have.property('name');
257
                    res.body.should.have.property('kind').eql('ObjectId');
258
                    res.body.should.have.property('value').eql('90');
259
                    res.body.should.have.property('path').eql('_id');
260
                    done();
261
                });
262
        });
263
264
        it('it should return not found if the book ID don\'t exist', (done) => {
265
            chai.request(server)
266
                .delete('/api/books/589e02e559f531603fe40322')
267
                .end((err, res) => {
268
                    res.should.have.status(404);
269
                    res.body.should.be.a('object');
270
                    res.body.should.have.property('message').eql('Book not found!');
271
                    res.body.should.have.property('id').eql('589e02e559f531603fe40322');
272
                    done();
273
                });
274
        });
275
    });
276
});