Skip to content

Commit

Permalink
Write tests for models
Browse files Browse the repository at this point in the history
  • Loading branch information
Billmike committed Mar 16, 2018
1 parent 3e6a08e commit 9a4f817
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 13 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"heroku-postbuild": "npm run migrate",
"migrate": "node_modules/.bin/sequelize db:migrate",
"nyc-local": "cross-env NODE_ENV=test npm run test-migrate && cross-env NODE_ENV=test npm run test",
"test-local": "cross-env NODE_ENV=test npm run test-migrate && npm run seed-testdb && cross-env NODE_ENV=test mocha --compilers js:babel-core/register server/__test__/*.spec.js --timeout 50000 --exit",
"test": "cross-env NODE_ENV=test npm run test-migrate && npm run seed-testdb && NODE_ENV=test nyc mocha --compilers js:babel-core/register server/__test__/*.spec.js --timeout 50000 --exit && npm run coverage",
"test-local": "cross-env NODE_ENV=test npm run test-migrate && npm run seed-testdb && cross-env NODE_ENV=test mocha --compilers js:babel-core/register server/__test__/index.spec.js --timeout 50000 --exit",
"test": "cross-env NODE_ENV=test npm run test-migrate && npm run seed-testdb && NODE_ENV=test nyc mocha --compilers js:babel-core/register server/__test__/index.spec.js --timeout 50000 --exit && npm run coverage",
"test-migrate": "sequelize db:migrate:undo:all && sequelize db:migrate",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"client-test": "jest ./client/src/__tests__/**/** --config ./jest.config.js --coverage",
Expand All @@ -32,7 +32,8 @@
"run-prod": "npm run build-client && node ./dist/bin/www.js",
"run-dev": "npm run build-test && node ./dist/bin/www.js",
"seed-testdb": "sequelize db:seed:all --env test",
"e2e": "./node_modules/.bin/nightwatch"
"e2e": "./node_modules/.bin/nightwatch",
"trial": "mocha --compilers js:babel-core/register server/__test__/validators/validatesignin.spec.js"
},
"nyc": {
"require": [
Expand Down Expand Up @@ -163,4 +164,4 @@
"validator": "^9.2.0",
"webpack": "^3.4.1"
}
}
}
8 changes: 8 additions & 0 deletions server/__test__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import './favorite.spec';
import './recipes.spec';
import './users.spec';
import './vote.spec';
import './models/user.spec';
import './models/recipe.spec';
import './models/vote.spec';
import './models/favorite.spec';
16 changes: 16 additions & 0 deletions server/__test__/models/favorite.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from 'chai';
import models from '../../models';

const { Favorite } = models;

describe('Favorite model', () => {
describe('Create favorites', () => {
it('Should create user favorite', (done) => {
Favorite.create({
}).then((recipe) => {
expect(recipe).to.be.an('object');
done();
});
});
});
});
90 changes: 90 additions & 0 deletions server/__test__/models/recipe.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { expect } from 'chai';
import models from '../../models';

const { Recipe } = models;

describe('Recipe models', () => {
describe('Create recipe', () => {
it('should fail to create a recipe if no recipe name if provided', (done) => {
Recipe.create({
description: 'Best recipe in town',
imageUrl: 'http://someimagehere',
category: 'Lunch',
ingredients: 'New baby\nNew recipe',
instructions: 'Make it shine\nLet it shine'
}).catch((error) => {
expect(error.errors[0].message).to.equal('name cannot be null');
expect(error.errors[0].type).to.equal('notNull Violation');
done();
});
});
it('should fail to create a recipe if no category is picked', (done) => {
Recipe.create({
name: 'My recipe',
description: 'Best recipe in town',
imageUrl: 'http://someimagehere',
ingredients: 'New baby\nNew recipe',
instructions: 'Make it shine\nLet it shine'
}).catch((error) => {
expect(error.errors[0].message).to.equal('category cannot be null');
expect(error.errors[0].type).to.equal('notNull Violation');
done();
});
});
it('Should create a recipe succesfully', (done) => {
Recipe.create({
name: 'Amazing recipe',
description: 'Amazing recipe description',
imageUrl: 'http://someimagehere',
category: 'Dessert',
ingredients: 'New baby\nNew recipe',
instructions: 'Make it shine\nLet it shine'
}).then((recipe) => {
expect(recipe.dataValues.name).to.be.a('string');
expect(recipe.dataValues.description).to.be.a('string');
expect(recipe.dataValues.imageUrl).to.be.a('string');
expect(recipe.dataValues.category).to.be.a('string');
expect(recipe.dataValues.ingredients).to.be.a('string');
expect(recipe.dataValues.instructions).to.be.a('string');
expect(recipe.dataValues).to.be.an('object');
done();
});
});
});
describe('Get all recipes', () => {
it('Should get all the recipes in the application', (done) => {
Recipe.all()
.then((recipes) => {
expect(recipes).to.be.an('array');
done();
});
});
});
describe('Edit a recipe', () => {
it('Should modify a recipe', (done) => {
Recipe.findById(2)
.then((recipe) => {
recipe.update({
name: 'Amazing recipe here'
}).then((updatedRecipe) => {
expect(updatedRecipe.dataValues.name)
.to.equal('Amazing recipe here');
expect(updatedRecipe.dataValues).to.be.an('object');
done();
});
});
});
});
describe('Delete a recipe', () => {
it('Should delete a recipe', (done) => {
Recipe.findById(2)
.then((recipe) => {
recipe.destroy()
.then((deletedRecipe) => {
expect(deletedRecipe.length).to.equal(0);
done();
});
});
});
});
});
17 changes: 17 additions & 0 deletions server/__test__/models/review.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect } from 'chai';
import models from '../../models';

const { Review } = models;

describe('Review model', () => {
describe('Create reviews', () => {
it('should create a new review', (done) => {
Review.create({
content: 'Amazing recipe man!'
}).then((review) => {
expect(review).to.be.an('object');
done();
});
});
});
});
96 changes: 96 additions & 0 deletions server/__test__/models/user.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { expect } from 'chai';
import models from '../../models';

const { User } = models;

describe('User model', () => {
describe('Create User', () => {
it(
'should throw an error if no username is provided when during creation',
(done) => {
User.create({
email: 'somerandom@gmail.com',
password: 'qwertyuiop'
}).catch((error) => {
expect(error.errors[0].message).to.equal('username cannot be null');
expect(error.errors[0].type === 'notNull Violation');
done();
});
}
);
it('should throw an error if no password is provided', (done) => {
User.create({
username: 'anyrandomname',
email: 'anyrandomemail@gmail.com'
}).catch((error) => {
expect(error.errors[0].message).to.equal('password cannot be null');
expect(error.errors[0].type).to.equal('notNull Violation');
done();
});
});
it('it should throw an error if password length is less than 8', (done) => {
User.create({
username: 'brandnew',
email: 'brandnew@gmail.com',
password: 'qwer'
}).catch((error) => {
expect(error.errors[0].message)
.to.equal('Enter a password greater than 8 characters');
expect(error.errors[0].type).to.equal('Validation error');
done();
});
});
it('Should throw an error if the email is of an invalid format', (done) => {
User.create({
username: 'william',
email: 'williamgates',
password: 'qwertyuiop'
}).catch((error) => {
expect(error.errors[0].message).to
.equal('Validation isEmail on email failed');
expect(error.errors[0].type).to.equal('Validation error');
expect(error.errors[0].value).to.equal('williamgates');
done();
});
});
it('should create a user if all information is valid', (done) => {
User.create({
username: 'williamsshakespear',
email: 'williamsshakespear@gmail.com',
password: 'qwertyuiop'
}).then((user) => {
expect(user.dataValues.username).to.be.a('string');
expect(user.dataValues.email).to.be.a('string');
expect(user.dataValues.password).to.be.a('string');
expect(user.dataValues.username).to.equal('williamsshakespear');
expect(user.dataValues.email).to.equal('williamsshakespear@gmail.com');
expect(user.dataValues.password).to.equal('qwertyuiop');
done();
});
});
it('should throw an error if the username already exists', (done) => {
User.create({
username: 'williamsshakespear',
email: 'quantity@gmail.com',
password: 'qwertyuiop'
}).catch((error) => {
expect(error.errors[0].message).to.equal('username must be unique');
expect(error.errors[0].type).to.equal('unique violation');
expect(error.errors[0].value).to.equal('williamsshakespear');
done();
});
});
it('Should throw an error if the email already exists', (done) => {
User.create({
username: 'zxcvbnmasd',
email: 'williamsshakespear@gmail.com',
password: 'qwertyuiop'
}).catch((error) => {
expect(error.errors[0].message).to.equal('email must be unique');
expect(error.errors[0].type).to.equal('unique violation');
expect(error.errors[0].value).to.equal('williamsshakespear@gmail.com');
done();
});
});
});
});
18 changes: 18 additions & 0 deletions server/__test__/models/vote.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from 'chai';
import models from '../../models';

const { Vote } = models;

describe('Votes model', () => {
describe('Create votes', () => {
it('Should create a vote for a recipe', (done) => {
Vote.create({
userId: 3,
voteType: 'upvote'
}).then((vote) => {
expect(vote.dataValues).to.be.an('object');
done();
});
});
});
});
42 changes: 42 additions & 0 deletions server/__test__/validators/validateRecipe.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from 'chai';
import validateRecipe from '../../validators/validateRecipe';

const inputData = {
name: 'Fried rice',
description: 'Amazing recipe',
category: 'Lunch',
ingredients: 'Baking powder\nSoda',
instructions: 'Soda powder\nNomencleture'
};

describe('Validate Add recipe', () => {
it('Should throw an error if no name is provided on recipe creation', () => {
const noName = { ...inputData, name: '' };
const invalidData = validateRecipe(noName);
expect(invalidData.errors.name).to.equal('Recipe name is required');
});
it('Should throw an error if no description is passed', () => {
const noDescription = { ...inputData, description: '' };
const invalidData = validateRecipe(noDescription);
expect(invalidData.errors.description).to
.equal('Enter a description for your recipe');
});
it('should throw an error if no category is provided', () => {
const noCategory = { ...inputData, category: '' };
const invalidData = validateRecipe(noCategory);
expect(invalidData.errors.category).to
.equal('Select a category from the dropdown');
});
it('Should throw an error if no ingredient is passed through', () => {
const noIngredient = { ...inputData, ingredients: '' };
const invalidData = validateRecipe(noIngredient);
expect(invalidData.errors.ingredients).to
.equal('Input some ingredients for your recipe');
});
it('Should throw an error if no instruction is provided', () => {
const noInstruction = { ...inputData, instructions: '' };
const invalidData = validateRecipe(noInstruction);
expect(invalidData.errors.instructions)
.to.equal('Input directions on how to cook your recipe');
});
});
23 changes: 23 additions & 0 deletions server/__test__/validators/validatesignin.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect } from 'chai';
import validatesignin from '../../validators/validatesignin';

const helper = validatesignin;

describe('Validate signin function', () => {
it('should return an error if no email is passed in the data', () => {
const data = {
password: 'qwertyuiop'
};
const invalidInput = validatesignin(data);
expect(invalidInput.errors.email).to
.equal('Input an email address to sign-in.');
});
it('should return an error if no password is passed in', () => {
const data = {
email: 'qwertyuiop@gmail.com'
};
const invalidInput = validatesignin(data);
expect(invalidInput.errors.password).to
.equal('Input a password to sign-in.');
});
});
Loading

0 comments on commit 9a4f817

Please sign in to comment.