Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Write tests for POST /recipes endpoint
Browse files Browse the repository at this point in the history
- modify route handler based on failing tests to make them pass
  • Loading branch information
akhilome committed Sep 28, 2018
1 parent a5d5f2b commit e3f68be
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
7 changes: 5 additions & 2 deletions controllers/Recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ class RecipeController {

try {
const result = await pool.query(text, [name, ingredients, directions]);
res.json({ recipe: result.rows });
res.status(201).json({
message: 'recipe added successfully',
recipe: result.rows,
});
} catch (err) {
res.status(400).json({ err });
res.status(400).json({ error: err });
}
}

Expand Down
41 changes: 41 additions & 0 deletions tests/routes/recipes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,44 @@ describe('DELETE /recipes/:id', () => {
});
});
});

describe('POST /recipes', () => {
const data = {
complete: {
name: 'new recipe',
ingredients: 'recipe ingredients',
directions: 'how to prepare meal with this recipe',
},
incomplete: {
name: 'recipe 2',
directions: 'no ingredients',
},
};

it('should successfully add new recipe to db on provision of complete data', (done) => {
chai.request(app)
.post('/api/v1/recipes')
.send(data.complete)
.end((err, res) => {
if (err) done(err);

res.status.should.eql(201);
res.body.should.be.an('object').which.has.keys(['message', 'recipe']);
res.body.recipe[0].should.eql({ id: 3, ...data.complete });
done();
});
});

it('should respond with a 400 if data is incomplete', (done) => {
chai.request(app)
.post('/api/v1/recipes')
.send(data.incomplete)
.end((err, res) => {
if (err) done(err);

res.status.should.eql(400);
res.body.should.have.keys(['error']);
done();
});
});
});

0 comments on commit e3f68be

Please sign in to comment.