UNPKG

2.97 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({ q: {
53 _id: {
54 $in: [tag.id, tag.id]
55 }
56 }})
57 .expect(200)
58 .end(function(err, res) {
59 if (err) {
60 throw err;
61 }
62 res.body.tags.length.should.equal(1);
63 done();
64 });
65 });
66 });
67 describe('GET /:id', function() {
68 it('should return a tag', function(done) {
69 request(app)
70 .get('/tags/' + tag.id)
71 .expect(200)
72 .end(function(err, res) {
73 if (err) throw err
74 res.body.tag._id.should.equal(tag.id);
75 res.body.tag.name.should.equal('a');
76 res.body.tag.post_id.should.equal(post.id);
77 done();
78 });
79 });
80 });
81 describe('PUT /:id', function() {
82 it('should update and return a tag', function(done) {
83 request(app)
84 .put('/tags/' + tag.id).send({ tag: {
85 name: 'b'
86 }})
87 .expect(200)
88 .end(function(err, res) {
89 res.body.tag.name.should.equal('b');
90 if (err) throw err
91 Tag.findById(tag.id, function(err, tag) {
92 if (err) throw err
93 tag.name.should.equal('b');
94 done();
95 });
96 });
97 });
98 });
99 describe('DELETE /:id', function() {
100 it('should remove a tag', function(done) {
101 request(app)
102 .del('/tags/' + tag.id)
103 .expect(200)
104 .expect({})
105 .end(function(err, res) {
106 if (err) throw err
107 Tag.findById(tag.id, function(err, _tag) {
108 if (err) throw err
109 should.not.exist(_tag);
110 done();
111 });
112 });
113 });
114 });
115});