Last active
April 27, 2018 16:52
-
-
Save cklanac/ead56e13159c7729f4cf9d0bcba70af8 to your computer and use it in GitHub Desktop.
Starter test suite for Noteful
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* DISCLAIMER: | |
* The examples shown below are superficial tests which only check the API responses. | |
* They do not verify the responses against the data in the database. We will learn | |
* how to crosscheck the API responses against the database in a later exercise. | |
*/ | |
const app = require('../server'); | |
const chai = require('chai'); | |
const chaiHttp = require('chai-http'); | |
const knex = require('../knex'); | |
const seedData = require('../db/seedData'); | |
const expect = chai.expect; | |
chai.use(chaiHttp); | |
describe('Reality check', function () { | |
it('true should be true', function () { | |
expect(true).to.be.true; | |
}); | |
it('2 + 2 should equal 4', function () { | |
expect(2 + 2).to.equal(4); | |
}); | |
}); | |
describe('Environment', () => { | |
it('NODE_ENV should be "test"', () => { | |
expect(process.env.NODE_ENV).to.equal('test'); | |
}); | |
it('connection should be test database', () => { | |
expect(knex.client.connectionSettings.database).to.equal('noteful-test'); | |
}); | |
}); | |
describe('Noteful App', function () { | |
beforeEach(function () { | |
return seedData('./db/noteful.sql', 'dev'); | |
}); | |
after(function () { | |
return knex.destroy(); // destroy the connection | |
}); | |
describe('Static app', function () { | |
it('GET request "/" should return the index page', function () { | |
return chai.request(app) | |
.get('/') | |
.then(function (res) { | |
expect(res).to.exist; | |
expect(res).to.have.status(200); | |
expect(res).to.be.html; | |
}); | |
}); | |
}); | |
describe('404 handler', function () { | |
it('should respond with 404 when given a bad path', function () { | |
return chai.request(app) | |
.get('/bad/path') | |
.then(res => { | |
expect(res).to.have.status(404); | |
}); | |
}); | |
}); | |
describe('GET /api/notes', function () { | |
it('should return the default of 10 Notes ', function () { | |
return chai.request(app) | |
.get('/api/notes') | |
.then(function (res) { | |
expect(res).to.have.status(200); | |
expect(res).to.be.json; | |
expect(res.body).to.be.a('array'); | |
expect(res.body).to.have.length(10); | |
}); | |
}); | |
it('should return a list with the correct right fields', function () { | |
return chai.request(app) | |
.get('/api/notes') | |
.then(function (res) { | |
expect(res).to.have.status(200); | |
expect(res).to.be.json; | |
expect(res.body).to.be.a('array'); | |
expect(res.body).to.have.length(10); | |
res.body.forEach(function (item) { | |
expect(item).to.be.a('object'); | |
expect(item).to.include.keys('id', 'title', 'content'); | |
}); | |
}); | |
}); | |
it('should return correct search results for a valid query', function () { | |
return chai.request(app) | |
.get('/api/notes?searchTerm=about%20cats') | |
.then(function (res) { | |
expect(res).to.have.status(200); | |
expect(res).to.be.json; | |
expect(res.body).to.be.a('array'); | |
expect(res.body).to.have.length(4); | |
expect(res.body[0]).to.be.an('object'); | |
}); | |
}); | |
it('should return an empty array for an incorrect query', function () { | |
return chai.request(app) | |
.get('/api/notes?searchTerm=Not%20a%20Valid%20Search') | |
.then(function (res) { | |
expect(res).to.have.status(200); | |
expect(res).to.be.json; | |
expect(res.body).to.be.a('array'); | |
expect(res.body).to.have.length(0); | |
}); | |
}); | |
}); | |
describe('GET /api/notes/:id', function () { | |
it('should return correct notes', function () { | |
return chai.request(app) | |
.get('/api/notes/1000') | |
.then(function (res) { | |
expect(res).to.have.status(200); | |
expect(res).to.be.json; | |
expect(res.body).to.be.an('object'); | |
expect(res.body).to.include.keys('id', 'title', 'content'); | |
expect(res.body.id).to.equal(1000); | |
expect(res.body.title).to.equal('5 life lessons learned from cats'); | |
}); | |
}); | |
it('should respond with a 404 for an invalid id', function () { | |
return chai.request(app) | |
.get('/DOES/NOT/EXIST') | |
.then(res => { | |
expect(res).to.have.status(404); | |
}); | |
}); | |
}); | |
describe('POST /api/notes', function () { | |
it('should create and return a new item when provided valid data', function () { | |
const newItem = { | |
'title': 'The best article about cats ever!', | |
'content': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor...' | |
}; | |
return chai.request(app) | |
.post('/api/notes') | |
.send(newItem) | |
.then(function (res) { | |
expect(res).to.have.status(201); | |
expect(res).to.be.json; | |
expect(res.body).to.be.a('object'); | |
expect(res.body).to.include.keys('id', 'title', 'content'); | |
expect(res.body.title).to.equal(newItem.title); | |
expect(res.body.content).to.equal(newItem.content); | |
expect(res).to.have.header('location'); | |
}); | |
}); | |
it('should return an error when missing "title" field', function () { | |
const newItem = { | |
'foo': 'bar' | |
}; | |
return chai.request(app) | |
.post('/api/notes') | |
.send(newItem) | |
.then(res => { | |
expect(res).to.have.status(400); | |
expect(res).to.be.json; | |
expect(res.body).to.be.a('object'); | |
expect(res.body.message).to.equal('Missing `title` in request body'); | |
}); | |
}); | |
}); | |
describe('PUT /api/notes/:id', function () { | |
it('should update the note', function () { | |
const updateItem = { | |
'title': 'What about dogs?!', | |
'content': 'woof woof' | |
}; | |
return chai.request(app) | |
.put('/api/notes/1005') | |
.send(updateItem) | |
.then(function (res) { | |
expect(res).to.have.status(200); | |
expect(res).to.be.json; | |
expect(res.body).to.be.a('object'); | |
expect(res.body).to.include.keys('id', 'title', 'content'); | |
expect(res.body.id).to.equal(1005); | |
expect(res.body.title).to.equal(updateItem.title); | |
expect(res.body.content).to.equal(updateItem.content); | |
}); | |
}); | |
it('should respond with a 404 for an invalid id', function () { | |
const updateItem = { | |
'title': 'What about dogs?!', | |
'content': 'woof woof' | |
}; | |
return chai.request(app) | |
.put('/DOES/NOT/EXIST') | |
.send(updateItem) | |
.then(res => { | |
expect(res).to.have.status(404); | |
}); | |
}); | |
it('should return an error when missing "title" field', function () { | |
const updateItem = { | |
'foo': 'bar' | |
}; | |
return chai.request(app) | |
.put('/api/notes/1005') | |
.send(updateItem) | |
.then(res => { | |
expect(res).to.have.status(400); | |
expect(res).to.be.json; | |
expect(res.body).to.be.a('object'); | |
expect(res.body.message).to.equal('Missing `title` in request body'); | |
}); | |
}); | |
}); | |
describe('DELETE /api/notes/:id', function () { | |
it('should delete an item by id', function () { | |
return chai.request(app) | |
.delete('/api/notes/1005') | |
.then(function (res) { | |
expect(res).to.have.status(204); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment