Unit Tests with Mocha and Chai in express.js - Tests not passing

Unit Tests with Mocha and Chai in express.js - Tests not passing
0

#1

I know this is pretty long. Pardon me if there could have been a better way to present my codes. I need to solve this urgently.
The tests are not passing. What am I doing wrong?

db/db.js

const orders =  [
    {
      id: 1,
      orderStatus: "In Transit",
      name: "sandwich",
      quantity: "2",
      price: "10",
      date: new Date().toDateString()
    },
    {
      id: 2,
      orderStatus: "In Transit",
      name: "cake",
      quantity: "2",
      price: "10",
      date: new Date().toDateString()
    },
    {
      id: 3,
      orderStatus: "In Transit",
      name: "Cheese",
      quantity: "2",
      price: "10",
      date: new Date().toDateString()
    },
    {
      id: 4,
      orderStatus: "In Transit",
      name: "Sausage",
      quantity: "2",
      price: "10",
      date: new Date().toDateString()
    }
];

export default orders;

controllers/orders.js

import db from '../db/db';

//ALL ORDERS
export const allOrders = (req, res) => {
 res.status(200).send({
    success: 'true',
    message: 'orders retrieved successfully',
    orders: db
  });
}

//FETCH AN ORDER
export const getOrderById = (req, res) => {
	  const id = parseInt(req.params.id, 10);
  db.map((order) => {
     if (order.id === id) {
       return res.status(200).send({
          success: 'true',
          message: 'order retrieved successfully',
          order
       });
      }
  });
  return res.status(404).send({
    success: 'false',
    message: 'order does not exist'
  });
}

//PLACE AN ORDER
export const placeAnOrder = (req, res) =>{
	if(!req.body.name) {
    return res.status(400).send({
      success: 'false',
      message: 'name is required'
    });
  } else if(!req.body.quantity) {
    return res.status(400).send({
      success: 'false',
      message: 'quantity is required'
    });
  }
 const order = {
   id: db.length + 1,
   orderStatus: req.body.status,
   name: req.body.name,
   quantity: req.body.quantity,
   price: "10",
   date: new Date().toDateString()
 }
 db.push(order);
 return res.status(200).send({
   success: 'true',
   message: 'order added successfully',
   order: order
 })
}

//UPDATE ORDER STATUS
export const updateOrderStatus = (req, res) => {
  const id = parseInt(req.params.id, 10);
  let orderFound;
  let itemIndex;
  db.map((order, index) => {
    if (order.id === id) {
      orderFound = order;
      itemIndex = index;
    }
  });

  if (!orderFound) {
    return res.status(404).send({
      success: 'false',
      message: 'order not found'
    });
  }

  if (!req.body.name) {
    return res.status(400).send({
      success: 'false',
      message: 'name is required'
    });
  } else if (!req.body.quantity) {
    return res.status(400).send({
      success: 'false',
      message: 'quantity is required'
    });
  }

  const updatedOrder = {
    id: orderFound.id,
    name: req.body.name || orderFound.name,
    quantity: req.body.quantity || orderFound.quantity
  };

  db.splice(itemIndex, 1, updatedOrder);

  return res.status(200).send({
    success: 'true',
    message: 'order added successfully',
    updatedOrder: updatedOrder
  });
}

routes/orders.js


import {Router} from 'express';
import { allOrders, getOrderById, placeAnOrder, updateOrderStatus } from '../controllers/orders';

const router = Router();

router.get('/', allOrders);
router.get('/:id', getOrderById);
router.post('/', placeAnOrder);
router.put('/:id', updateOrderStatus);

export default router;

test/test.js

import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../app.js';

chai.use(chaiHttp);
let should = chai.should();

describe('API Orders Endpoint Testing', () => {
	//Test to get all orders
	it('should list ALL orders on / GET', (done) => {
		chai.request(app)
			.get('/api/v1/orders')
			.end((err, res) => {
				res.should.have.status(200);
				res.body.should.be.a('object');
				res.body.should.have.property('name');
      			res.body.should.have.property('quantity');
				done();
			});
	});
	//Test to Fetch a single order
	it('should get a SINGLE order on /:id GET', (done) => {
		const id = 1;
		chai.request(app)
			.get(`/api/v1/orders/${id}`)
			.end((err, res) => {
				res.should.have.status(200);
				res.body.should.be.a('object');
				res.body.should.have.property('name');
      			res.body.should.have.property('quantity');
				done();
			});
	});
	//Test to place a new order
	it('should get place an order on / POST', (done) => {
		chai.request(app)
			.post('/api/v1/orders')
			.send({
		      	orderStatus: "Delivered",
		      	name: "Fish-Roll",
		      	quantity: "10",
		      	price: "100",
		      	date: new Date().toDateString()
				})
			.end((err, res) => {
				res.should.have.status(200);
				res.body.should.be.a('object');
				res.body.should.have.property('name');
      			res.body.should.have.property('quantity');
				done();
			});
	});
	//Test to update status of an order
	it('should update the status of an order on /:id GET', (done) => {
		const id = 2;
		chai.request(app)
			.put(`/api/v1/orders/${id}`)
			.end((err, res) => {
				res.should.have.status(200);
				res.body.should.be.a('object');
				res.body.should.have.property('name');
      			res.body.should.have.property('quantity');
				done();
			});
	});

}); 

app.js

import express from 'express';
import bodyParser from 'body-parser';
import babelRegister from 'babel-register';
import orders from './routes/orders';

const app = express();

// Parse incoming requests data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use(express.json());

app.get('/', (req, res) => {
	return res.status(200).send({'Message':'Welcome to Fast food fast home page'});
});

// Orders
app.use('/api/v1/orders', orders);

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log(`server running on port ${PORT}`)
});

export default app;

package.json

{
  "name": "fast-food-fast",
  "version": "1.0.0",
  "description": "An app for ordering fast food online.\r To view the admin panel log in with \"admin\" as password.\r The UI is responsive feel free to resize your browser to see what happens.\r https://shittu1.github.io/fast-food-fast/.",
  "main": "app.js",
  "scripts": {
    "test": "mocha --recursive --require babel-register",
    "build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
    "start": "npm run build && node dist/app.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Shittu1/fast-food-fast.git"
  },
  "author": "Adams Shittu",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/Shittu1/fast-food-fast/issues"
  },
  "homepage": "https://github.com/Shittu1/fast-food-fast#readme",
  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "morgan": "^1.9.1"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-register": "^6.26.0",
    "chai": "^4.2.0",
    "chai-http": "^4.2.0",
    "eslint": "^5.6.1",
    "eslint-config-rallycoding": "^3.2.0",
    "mocha": "^5.2.0",
    "nodemon": "^1.18.4",
    "rimraf": "^2.6.2",
    "should": "^13.2.3",
    "supertest": "^3.3.0"
  }
}

Folder Structure
folder-structure