From 4e0804c7ff18c1cb6f4af252ad903a20b04a9191 Mon Sep 17 00:00:00 2001 From: Christopher Van Date: Tue, 13 Jan 2015 14:47:12 -0800 Subject: [PATCH] add test for `GET /games` hapi endpoint (fixes #286) --- test/api/controllers/game.js | 115 +++++++++++++++++++++++++++++------ 1 file changed, 96 insertions(+), 19 deletions(-) diff --git a/test/api/controllers/game.js b/test/api/controllers/game.js index f8ed1a0..459f424 100644 --- a/test/api/controllers/game.js +++ b/test/api/controllers/game.js @@ -2,15 +2,63 @@ var Lab = require('lab'); var Code = require('code'); +var Promise = require('es6-promise').Promise; + +var db = require('../../../lib/db'); var lab = exports.lab = Lab.script(); var req; var server = require('../../../'); +function submitGame(done) { + return new Promise(function (resolve) { + req = { + method: 'POST', + url: '/games', + payload: { + name: 'no flex zone', + slug: 'no-flex-zone', + game_url: 'https://no.flexzo.ne' + } + }; + + server.inject(req, function (res) { + + Code.expect(res.result).to.be.an.object().and + .not.contain(['error', 'validation']); + + Code.expect(res.result).to.deep.equal({ + name: req.payload.name, + slug: req.payload.slug, + game_url: req.payload.game_url, + description: null + }); + + Code.expect(res.statusCode).to.equal(201); + Code.expect(res.headers.location).to.equal( + '/games/' + req.payload.slug); + + if (done) { + resolve(); + } else { + resolve(req); + } + }); + }); +} + + lab.experiment('game creation', function () { + lab.after(function (done) { + + db.query('TRUNCATE games'); + done(); + }); + + lab.test('validation errors on empty payload', function (done) { req = { @@ -69,34 +117,63 @@ lab.experiment('game creation', function () { lab.test('success on valid payload', function (done) { + submitGame(done).then(done); + }); +}); + + +lab.experiment('games list', function () { + + + lab.afterEach(function (done) { + + db.query('TRUNCATE games'); + done(); + }); + + + lab.test('returns an empty array when no games exist', function (done) { + req = { - method: 'POST', - url: '/games', - payload: { - name: 'no flex zone', - slug: 'no-flex-zone', - game_url: 'https://no.flexzo.ne' - } + method: 'GET', + url: '/games' }; server.inject(req, function (res) { - Code.expect(res.result).to.be.an.object().and - .not.contain(['error', 'validation']); + Code.expect(res.result).to.be.an.empty().array(); - Code.expect(res.result).to.deep.equal({ - name: req.payload.name, - slug: req.payload.slug, - game_url: req.payload.game_url, - description: null - }); - - Code.expect(res.statusCode).to.equal(201); - Code.expect(res.headers.location).to.equal( - '/games/' + req.payload.slug); + Code.expect(res.statusCode).to.equal(200); done(); }); }); + + lab.test('returns an array when game(s) exist(s)', function (done) { + + submitGame().then(function (prevReq) { + + req = { + method: 'GET', + url: '/games' + }; + + server.inject(req, function (res) { + + Code.expect(res.result).to.be.a.length(1).array(); + + Code.expect(res.result[0]).to.deep.equal({ + name: prevReq.payload.name, + slug: prevReq.payload.slug, + game_url: prevReq.payload.game_url, + description: null + }); + + Code.expect(res.statusCode).to.equal(200); + + done(); + }); + }); + }); });