diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/services/meal/index.test.js | 108 | ||||
| -rw-r--r-- | test/services/user/index.test.js | 1 |
2 files changed, 105 insertions, 4 deletions
diff --git a/test/services/meal/index.test.js b/test/services/meal/index.test.js index 92dd70f..c01ba93 100644 --- a/test/services/meal/index.test.js +++ b/test/services/meal/index.test.js @@ -1,10 +1,110 @@ 'use strict'; +const chai = require('chai') +const chaiHttp = require('chai-http') +const should = chai.should(); const assert = require('assert'); const app = require('../../../src/app'); -describe('meal service', function() { +const User = app.service('users') +const Meal = app.service('meals') +const authentication = require('feathers-authentication/client'); +const bodyParser = require('body-parser'); + +var token, userid, mealid + +app + .use(bodyParser.json()) + .use(bodyParser.urlencoded({ extended: true })) + .configure(authentication()); +chai.use(chaiHttp); + + +describe('meal service', () => { + before((done) => { + this.server = app.listen(3030) + this.server.once('listening', () => { + User.create({ + email: 'test@test.com', + password: 'password', + goal: 2000, + }, () => { + chai.request(app) + .post('/auth/local') + .set('Accept', 'application/json') + .send({ + email: 'test@test.com', + password: 'password', + }) + .end((err, res) => { + token = res.body.token + userid = res.body.data.id + done() + }) + }) + }) + }) + + after((done) => { + chai.request(app) + .delete('/users/'.concat(userid)) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer '.concat(token)) + .send() + .end((err, res) => { + this.server.close(function(){}) + done() + }) + }) + it('registered the meals service', () => { - assert.ok(app.service('meals')); - }); -}); + assert.ok(app.service('meals')) + }) + + it('should create a new meal', (done) => { + chai.request(app) + .post('/meals') + .set('Accept', 'application/json') + .set('Authorization', 'Bearer '.concat(token)) + .send({ + name: 'breakfast', + date: new Date('Thu Mar 16 2017 22:00:00 GMT+0100 (CET)'), + calories: 500, + userid: userid, + }) + .end((err, res) => { + res.body.should.have.property('name') + res.body.name.should.equal('breakfast') + res.body.calories.should.equal(500) + mealid = res.body.id + done() + }) + }) + + it('should update an existing meal', (done) => { + chai.request(app) + .patch('/meals/'.concat(mealid)) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer '.concat(token)) + .send({ + calories: 650, + }) + .end((err, res) => { + res.body.calories.should.equal(650) + done() + }) + }) + + it('should be able to destroy a meal', (done) => { + chai.request(app) + .delete('/meals/'.concat(mealid)) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer '.concat(token)) + .send() + .end((err, res) => { + assert.equal(res.statusCode, 200); + done() + }) + }) + +}) diff --git a/test/services/user/index.test.js b/test/services/user/index.test.js index 4302422..a9072c0 100644 --- a/test/services/user/index.test.js +++ b/test/services/user/index.test.js @@ -9,6 +9,7 @@ const app = require('../../../src/app'); const User = app.service('users') const authentication = require('feathers-authentication/client'); const bodyParser = require('body-parser'); + var token, userid app |
