TwoToc code
[YouAndWeb_TwoToc] / server / api / category / category.integration.js
1 'use strict';
2
3 var app = require('../..');
4 var request = require('supertest');
5
6 var newCategory;
7
8 describe('Category API:', function() {
9
10   describe('GET /api/categories', function() {
11     var categorys;
12
13     beforeEach(function(done) {
14       request(app)
15         .get('/api/categories')
16         .expect(200)
17         .expect('Content-Type', /json/)
18         .end(function(err, res) {
19           if (err) {
20             return done(err);
21           }
22           categorys = res.body;
23           done();
24         });
25     });
26
27     it('should respond with JSON array', function() {
28       categorys.should.be.instanceOf(Array);
29     });
30
31   });
32
33   describe('POST /api/categories', function() {
34     beforeEach(function(done) {
35       request(app)
36         .post('/api/categories')
37         .send({
38           name: 'New Category',
39           info: 'This is the brand new category!!!'
40         })
41         .expect(201)
42         .expect('Content-Type', /json/)
43         .end(function(err, res) {
44           if (err) {
45             return done(err);
46           }
47           newCategory = res.body;
48           done();
49         });
50     });
51
52     it('should respond with the newly created category', function() {
53       newCategory.name.should.equal('New Category');
54       newCategory.info.should.equal('This is the brand new category!!!');
55     });
56
57   });
58
59   describe('GET /api/categories/:id', function() {
60     var category;
61
62     beforeEach(function(done) {
63       request(app)
64         .get('/api/categories/' + newCategory._id)
65         .expect(200)
66         .expect('Content-Type', /json/)
67         .end(function(err, res) {
68           if (err) {
69             return done(err);
70           }
71           category = res.body;
72           done();
73         });
74     });
75
76     afterEach(function() {
77       category = {};
78     });
79
80     it('should respond with the requested category', function() {
81       category.name.should.equal('New Category');
82       category.info.should.equal('This is the brand new category!!!');
83     });
84
85   });
86
87   describe('PUT /api/categories/:id', function() {
88     var updatedCategory
89
90     beforeEach(function(done) {
91       request(app)
92         .put('/api/categories/' + newCategory._id)
93         .send({
94           name: 'Updated Category',
95           info: 'This is the updated category!!!'
96         })
97         .expect(200)
98         .expect('Content-Type', /json/)
99         .end(function(err, res) {
100           if (err) {
101             return done(err);
102           }
103           updatedCategory = res.body;
104           done();
105         });
106     });
107
108     afterEach(function() {
109       updatedCategory = {};
110     });
111
112     it('should respond with the updated category', function() {
113       updatedCategory.name.should.equal('Updated Category');
114       updatedCategory.info.should.equal('This is the updated category!!!');
115     });
116
117   });
118
119   describe('DELETE /api/categories/:id', function() {
120
121     it('should respond with 204 on successful removal', function(done) {
122       request(app)
123         .delete('/api/categories/' + newCategory._id)
124         .expect(204)
125         .end(function(err, res) {
126           if (err) {
127             return done(err);
128           }
129           done();
130         });
131     });
132
133     it('should respond with 404 when category does not exist', function(done) {
134       request(app)
135         .delete('/api/categories/' + newCategory._id)
136         .expect(404)
137         .end(function(err, res) {
138           if (err) {
139             return done(err);
140           }
141           done();
142         });
143     });
144
145   });
146
147 });