Skip to content

Commit

Permalink
chore(more-tests): refactor test structure and write more assertions
Browse files Browse the repository at this point in the history
- remove unneccessary npm module
- modify test structure and refactor affected tests
- Handle TODOs

[Finsihes #160977188]
  • Loading branch information
akhilome committed Oct 4, 2018
1 parent 8d750d7 commit 5f60f38
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 271 deletions.
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"chai": "^4.1.2",
"chai-http": "^4.2.0",
"coveralls": "^3.0.2",
"dirty-chai": "^2.0.1",
"eslint": "^5.5.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0",
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/orderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class OrderController {
return res.status(200).json({
status: 'success',
message: 'order fetched successfully',
targetOrder,
order: targetOrder,
});
} catch (error) {
return res.status(500).json(error);
Expand Down Expand Up @@ -76,7 +76,7 @@ class OrderController {
});
}
} catch (error) {
res.status(500).json();
res.status(500).json(error);
}

const dbInsertQuery = 'INSERT INTO orders(item, author) VALUES($1, $2)';
Expand Down
4 changes: 1 addition & 3 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import 'chai/register-should';
import dirtyChai from 'dirty-chai';
import app from '../server';

chai.use(chaiHttp);
chai.use(dirtyChai);

describe('GET /', () => {
it('should have a status code of 200', (done) => {
Expand All @@ -21,7 +19,7 @@ describe('GET /', () => {
chai.request(app)
.get('/')
.end((err, res) => {
res.body.should.be.an('object').that.is.not.empty();
res.body.should.be.an('object');
done();
});
});
Expand Down
50 changes: 31 additions & 19 deletions tests/routes/auth.spec.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
import chai from 'chai';
import 'chai/register-should';
import chaiHttp from 'chai-http';
import dirtyChai from 'dirty-chai';
import app from '../../server/index';
import { seedData, emptyTables, populateUsersTable } from '../seed/seed';
import { users, emptyTables } from '../seed/seed';

chai.use(chaiHttp);
chai.use(dirtyChai);

before(emptyTables);

describe('POST /auth/signup', () => {
before(emptyTables);
it('should signup an admin user successfully', (done) => {
chai.request(app)
.post('/api/v1/auth/signup')
.send(users.admin)
.end((err, res) => {
if (err) done(err);

res.status.should.eql(201);
res.body.should.be.an('object').that.has.keys(['status', 'message', 'user']);
res.body.status.should.eql('success');
res.body.user.should.have.keys(['id', 'name', 'email']);
res.body.user.name.should.eql(users.admin.name);
res.body.user.email.should.eql(users.admin.email);
done();
});
});

it('should signup a valid user successfully', (done) => {
chai.request(app)
.post('/api/v1/auth/signup')
.send(seedData.users.admin)
.send(users.validUser)
.end((err, res) => {
if (err) done(err);

res.status.should.eql(201);
res.body.should.be.an('object').that.has.keys(['status', 'message', 'user']);
res.body.status.should.eql('success');
res.body.user.should.have.keys(['id', 'name', 'email']);
res.body.user.name.should.eql(seedData.users.admin.name);
res.body.user.email.should.eql(seedData.users.admin.email);
res.body.user.name.should.eql(users.validUser.name);
res.body.user.email.should.eql(users.validUser.email);
done();
});
});

it('should not signup a user with no name', (done) => {
chai.request(app)
.post('/api/v1/auth/signup')
.send(seedData.users.invalidUserNoName)
.send(users.invalidUserNoName)
.end((err, res) => {
if (err) done(err);

Expand All @@ -45,7 +60,7 @@ describe('POST /auth/signup', () => {
it('should not signup a user with no email', (done) => {
chai.request(app)
.post('/api/v1/auth/signup')
.send(seedData.users.invalidUserNoEmail)
.send(users.invalidUserNoEmail)
.end((err, res) => {
if (err) done(err);

Expand All @@ -59,7 +74,7 @@ describe('POST /auth/signup', () => {
it('should not signup a user with no password', (done) => {
chai.request(app)
.post('/api/v1/auth/signup')
.send(seedData.users.invalidUserNoPass)
.send(users.invalidUserNoPass)
.end((err, res) => {
if (err) done(err);

Expand All @@ -73,7 +88,7 @@ describe('POST /auth/signup', () => {
it('should not signup a user with missmatching passwords', (done) => {
chai.request(app)
.post('/api/v1/auth/signup')
.send(seedData.users.invalidUserPassMissMatch)
.send(users.invalidUserPassMissMatch)
.end((err, res) => {
if (err) done(err);
res.status.should.eql(400);
Expand All @@ -88,7 +103,7 @@ describe('POST /auth/signup', () => {
it('should not signup a user if email already exists', (done) => {
chai.request(app)
.post('/api/v1/auth/signup')
.send(seedData.users.admin)
.send(users.admin)
.end((err, res) => {
if (err) done(err);
res.status.should.eql(400);
Expand All @@ -100,13 +115,10 @@ describe('POST /auth/signup', () => {
});

describe('POST /auth/login', () => {
beforeEach(emptyTables);
beforeEach(populateUsersTable);

it('should sign an existing user in', (done) => {
chai.request(app)
.post('/api/v1/auth/login')
.send(seedData.users.validUser)
.send(users.validUser)
.end((err, res) => {
if (err) done(err);

Expand All @@ -119,7 +131,7 @@ describe('POST /auth/login', () => {
it('should respond with a 400 if required fields are missing', (done) => {
chai.request(app)
.post('/api/v1/auth/login')
.send(seedData.users.invalidUserNoData)
.send(users.invalidUserNoData)
.end((err, res) => {
if (err) done(err);

Expand All @@ -131,7 +143,7 @@ describe('POST /auth/login', () => {
it('should not sign user in if non-existent email is provided', (done) => {
chai.request(app)
.post('/api/v1/auth/login')
.send(seedData.users.invalidUser)
.send(users.invalidUser)
.end((err, res) => {
if (err) done(err);

Expand All @@ -144,7 +156,7 @@ describe('POST /auth/login', () => {
it('should not sign user in if invalid password is provided', (done) => {
chai.request(app)
.post('/api/v1/auth/login')
.send(seedData.users.validUserInvalidPass)
.send(users.validUserInvalidPass)
.end((err, res) => {
if (err) done(err);

Expand Down
26 changes: 6 additions & 20 deletions tests/routes/menu.spec.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import chai from 'chai';
import 'chai/register-should';
import chaiHttp from 'chai-http';
import dirtyChai from 'dirty-chai';

import app from '../../server/index';
import {
seedData,
emptyTablesPromise,
populateUsersTablePromise,
populateMenuTablePromise,
generateValidToken,
} from '../seed/seed';
import { users, generateValidToken } from '../seed/seed';

chai.use(chaiHttp);
chai.use(dirtyChai);

before(async () => {
await emptyTablesPromise;
await populateMenuTablePromise;
await populateUsersTablePromise;
});

describe('POST /menu', () => {
it('should not allow non admin users add new food to menu', (done) => {
chai.request(app)
.post('/api/v1/menu')
.set('x-auth', generateValidToken(seedData.users.validUser))
.set('x-auth', generateValidToken(users.validUser))
.send({
foodName: 'Steak',
foodImage: 'https://i.imgur.com/7itOeyG.jpg',
Expand All @@ -42,7 +28,7 @@ describe('POST /menu', () => {
it('should not accept an incomplete request body', (done) => {
chai.request(app)
.post('/api/v1/menu')
.set('x-auth', generateValidToken(seedData.users.admin))
.set('x-auth', generateValidToken(users.admin))
.send({ foodName: 'Pizza' })
.end((err, res) => {
if (err) done(err);
Expand All @@ -56,7 +42,7 @@ describe('POST /menu', () => {
it('should not accept an improperly formatted price', (done) => {
chai.request(app)
.post('/api/v1/menu')
.set('x-auth', generateValidToken(seedData.users.admin))
.set('x-auth', generateValidToken(users.admin))
.send({ foodName: 'Smoked bread', price: 'cheap' })
.end((err, res) => {
if (err) done(err);
Expand All @@ -70,7 +56,7 @@ describe('POST /menu', () => {
it('should add new food items to the menu successfully', (done) => {
chai.request(app)
.post('/api/v1/menu')
.set('x-auth', generateValidToken(seedData.users.admin))
.set('x-auth', generateValidToken(users.admin))
.send({
foodName: 'Tasty Chicken',
foodImage: 'https://i.imgur.com/z490cis.jpg',
Expand All @@ -90,7 +76,7 @@ describe('POST /menu', () => {
});

describe('GET /menu', () => {
const { validUser } = seedData.users;
const { validUser } = users;

it('should get menu successfully if user is logged in', (done) => {
chai.request(app)
Expand Down
Loading

0 comments on commit 5f60f38

Please sign in to comment.