UNPKG

3.04 kBJavaScriptView Raw
1var should = require('should')
2 , request = require('supertest')
3 , mongoose = require('mongoose')
4 , app = require('./support/')
5 , db = require('./support/db')
6 , models = require('./support/models')
7 , User = models.User
8 , Tag = models.Tag
9 , Post = models.Post
10 , Comment = models.Comment;
11
12describe('tags', function() {
13 beforeEach(function(done) {
14 db.setUp(done);
15 });
16 afterEach(function(done) {
17 db.tearDown(done);
18 });
19 describe('GET /', function() {
20 it('should return a list of tags', function(done) {
21 request(app)
22 .get('/tags')
23 .expect(200)
24 .end(function(err, res) {
25 if (err) throw err
26 res.body.tags.length.should.equal(1);
27 done();
28 });
29 });
30 });
31 describe('POST /', function() {
32 it('should create and return a tag', function(done) {
33 request(app)
34 .post('/tags')
35 .send({ tag: {
36 name: 'b',
37 post_id: post.id
38 }})
39 .expect(200)
40 .end(function(err, res) {
41 if (err) throw err
42 res.body.tag.name.should.equal('b');
43 res.body.tag.post_id.should.equal(post.id);
44 done();
45 });
46 });
47 });
48 describe('QUERY /', function() {
49 it('should return matched tags', function(done) {
50 request(app)
51 .post('/tags')
52 .send(
53 {
54 query: {
55 q:{
56 _id: { $in: [tag.id, tag.id] }
57 }
58 }
59 }
60 )
61 .expect(200)
62 .end(function(err, res) {
63 if (err) {
64 throw err;
65 }
66 res.body.tags.length.should.equal(1);
67 done();
68 });
69 });
70 });
71 describe('GET /:id', function() {
72 it('should return a tag', function(done) {
73 request(app)
74 .get('/tags/' + tag.id)
75 .expect(200)
76 .end(function(err, res) {
77 if (err) throw err
78 res.body.tag._id.should.equal(tag.id);
79 res.body.tag.name.should.equal('a');
80 res.body.tag.post_id.should.equal(post.id);
81 done();
82 });
83 });
84 });
85 describe('PUT /:id', function() {
86 it('should update and return a tag', function(done) {
87 request(app)
88 .put('/tags/' + tag.id).send({ tag: {
89 name: 'b'
90 }})
91 .expect(200)
92 .end(function(err, res) {
93 res.body.tag.name.should.equal('b');
94 if (err) throw err
95 Tag.findById(tag.id, function(err, tag) {
96 if (err) throw err
97 tag.name.should.equal('b');
98 done();
99 });
100 });
101 });
102 });
103 describe('DELETE /:id', function() {
104 it('should remove a tag', function(done) {
105 request(app)
106 .del('/tags/' + tag.id)
107 .expect(200)
108 .expect({})
109 .end(function(err, res) {
110 if (err) throw err
111 Tag.findById(tag.id, function(err, _tag) {
112 if (err) throw err
113 should.not.exist(_tag);
114 done();
115 });
116 });
117 });
118 });
119});