So I am trying to learn TDD/BDD with Javascript. I am creating a simple RESTful web app that uses GET, POST, PUT, and DELETE API calls to update data in a MongoDB database, and wanted to see if there was anything I can do to improve on the unit test I wrote. Both to make them more organized in the testing output, and to include more specific details that might be important from a testing standpoint.
The web app is built using the MEAN stack.
var superagent = require("superagent");
var chai = require("chai");
var mocha = require("mocha");
var should = require("should");
var expect = chai.expect;
var chaiHttp = require('chai-http');
var request = require("request");
var app = require('../app');
var mongoose = require("mongoose");
var campaign = require("../app_api/models/campaigns");
chai.use(chaiHttp);
describe("Campaign API Calls", function() {
var test_id = 0;
var test_campaign_name = "New_Tester_Campaign";
describe("/POST/api/campaigns", function() {
it("should create new campaign with valid input", (done) => {
var campaign = {
campaignName: test_campaign_name,
campaignStatus: "active"
}
chai.request(app)
.post('/api/campaigns')
.send(campaign)
.end((err, res) => {
res.status.should.be.equal(201);
res.body.should.be.type('object');
res.body.should.have.property('campaignName').eql(test_campaign_name);
res.body.should.have.property('campaignStatus');
res.body.should.have.property('_id');
test_id = res.body._id; // Store ID of campaign for use by remaining tests
done();
});
});
it("should not create a new campaign with invalid input", (done) => {
var campaign = {
campaignName: "",
campaignStatus: "active"
}
chai.request(app)
.post('/api/campaigns')
.send(campaign)
.end((err, res) => {
res.status.should.be.equal(400);
res.body.should.have.property('message').eql('Campaign Name is Required');
done();
});
});
});
describe("/GET/api/campaigns", function() {
it("it should returns a list of campaigns", (done) => {
chai.request(app)
.get('/api/campaigns')
.end((err, res) => {
res.status.should.be.equal(200);
res.body.should.be.type('object');
done();
});
});
});
describe("/GET/api/campaigns/:campaignid", function () {
it("it should GET the details of one campaign", (done) => {
chai.request(app)
.get('/api/campaigns/' + test_id)
.end((err, res) => {
res.status.should.be.equal(200);
res.body.should.have.property('campaignName').eql(test_campaign_name);
res.body.should.have.property('_id').eql(test_id);
done();
});
});
});
describe("/PUT/api/campaigns/:campaignid", function () {
it("it should update a single campaign", (done) => {
chai.request(app)
.put('/api/campaign/' + test_id)
.field('campaignName', 'Tester_Campaign')
.end((err, res) => {
res.status.should.be.equal(200);
res.body.should.have.property('campaignName')
.eql('Tester_Campaign');
don();
});
});
});
describe("/DELETE/api/campaigns/:campaignid", function() {
it("it should delete a single campaign", function() {
chai.request(app)
.delete('/api/campaign/' + test_id)
.end((err, res) => {
res.status.should.be.equal(200);
res.body.should.have.property('message')
.eql('Campaign successfully deleted');
done();
});
});
});
});
it("it ...")-- The method name didn't tip you off that it it shouldn't start with "it"? :) \$\endgroup\$