Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const app = require('../app');
- let chai = require('chai');
- let chaiHttp = require('chai-http');
- chai.use(chaiHttp);
- // tests
- const Thread = require('../models/thread');
- const User = require('../models/user');
- const expect = chai.expect;
- const assert = chai.assert;
- const dummyUser = {
- Username: "test_thread",
- Password: "hunter2"
- };
- const dummyUser2 = {
- Username: "test_thread2",
- Password: "hunter2"
- };
- const dummyThread = {
- id: "",
- username: dummyUser.Username,
- title: "Test Thread",
- content: "This is a test thread."
- };
- const dummyThread2 = {
- id: "",
- Username: dummyUser2.Username,
- Title: "Test Thread 2",
- Content: "This is a second test thread."
- };
- const dummyComment = {
- username: dummyUser.Username,
- content: "This is an test comment"
- };
- before(function(){
- User.create(dummyUser);
- })
- describe('/api/threads end-point', function() {
- it('should create a new thread', function(done) {
- chai.request(app)
- .post('/api/threads')
- .send(dummyThread)
- .end(function(err, res) {
- assert(res.status, 201);
- dummyThread.id = res.body._id;
- done();
- });
- });
- it('responds with list of threads', function (done) {
- chai.request(app)
- .get('/api/threads')
- .end(function(err, res) {
- expect(err).to.be.null;
- expect(res).to.have.status(200);
- assert.property(res.body[0], 'Votes');
- assert.property(res.body[0], '_id');
- assert.property(res.body[0], 'User');
- assert.property(res.body[0], 'Title');
- assert.property(res.body[0], 'Content');
- done();
- });
- });
- it('responds with specific thread', function (done) {
- Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
- chai.request(app)
- .get('/api/threads/' + thread._id)
- .end(function(err, res) {
- expect(err).to.be.null;
- expect(res).to.have.status(200);
- //console.log(res.body);
- assert.property(res.body, 'Comments');
- assert.property(res.body, 'Votes');
- assert.property(res.body, '_id');
- assert.property(res.body, 'User');
- assert.property(res.body, 'Title');
- assert.property(res.body, 'Content');
- done();
- });
- })
- });
- it('updates an existing thread content', function (done) {
- dummyThread.newcontent = "This dummy thread has been updated!";
- Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
- chai.request(app)
- .put('/api/threads/' + thread._id)
- .send(dummyThread)
- .end(function(err, res) {
- expect(err).to.be.null;
- expect(res).to.have.status(202);
- dummyThread.content = dummyThread.newcontent;
- done();
- });
- })
- });
- it('adds an comment to a thread', function (done) {
- Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
- chai.request(app)
- .post('/api/threads/' + thread._id + "/comments")
- .send(dummyComment)
- .end(function(err, res) {
- expect(err).to.be.null;
- expect(res).to.have.status(202);
- done();
- });
- })
- });
- it('adds a vote to an thread', function (done) {
- Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
- chai.request(app)
- .post('/api/threads/' + thread._id + "/votes")
- .send({
- username: dummyUser.Username,
- votestatus: false
- })
- .end(function(err, res) {
- expect(err).to.be.null;
- expect(res).to.have.status(202);
- done();
- });
- })
- });
- it('doesnt add duplicate votes' , function (done) {
- Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
- chai.request(app)
- .post('/api/threads/' + thread._id + "/votes")
- .send({
- username: dummyUser.Username,
- votestatus: false
- })
- .end(function(err, res) {
- expect(err).to.be.null;
- expect(res.body.Votes).to.have.length(1);
- done();
- });
- })
- });
- it('updates vote status of existing vote', function(done){
- Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
- chai.request(app)
- .post('/api/threads/' + thread._id + "/votes")
- .send({
- username: dummyUser.Username,
- votestatus: true
- })
- .end(function(err, res) {
- expect(err).to.be.null;
- expect(res).to.have.status(202);
- expect(res.body.Votes[0].VoteStatus).to.be.equal(true);
- done();
- });
- })
- })
- it('gets a list of all threads sorted by most upvotes', function(done){
- Thread.create(dummyThread2);
- chai.request(app)
- .get('/api/threads')
- .set('sortBy', 'upvotes')
- .end(function(err, res){
- expect(err).to.be.null;
- expect(res.body[0]._id).to.be.equal(dummyThread.id);
- done();
- })
- })
- it('gets a list of all threads sorted by highest score', function(done){
- chai.request(app)
- .get('/api/threads')
- .set('sortBy', 'score')
- .end(function(err, res){
- expect(err).to.be.null;
- expect(res.body[0]._id).to.be.equal(dummyThread.id);
- done();
- })
- })
- it('deactivates an existing thread', function (done) {
- Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
- chai.request(app)
- .delete('/api/threads/' + thread._id)
- .end(function(err, res) {
- expect(err).to.be.null;
- expect(res).to.have.status(200);
- done();
- });
- })
- });
- });
- // removing dummy data from database.
- after(function() {
- Thread.findOneAndDelete({ _id: dummyThread.id }, function (err) {});
- Thread.findOneAndDelete({ _id: dummyThread2.id }, function (err) {});
- User.findOneAndDelete({ Username: dummyUser.Username }, function (err) {});
- User.findOneAndDelete({ Username: dummyUser2.Username }, function (err) {});
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement