From fe5a4d9902dc79892d14200fabad09a6103ce7cd Mon Sep 17 00:00:00 2001 From: fares98 Date: Wed, 21 Aug 2019 15:58:57 +0300 Subject: [PATCH] handle error and test for routes Relates #16 --- package.json | 5 +++-- src/controllers/err.js | 16 ++++++++++++++++ src/controllers/homeData.js | 4 ++++ src/controllers/index.js | 6 +++++- src/views/clientError.hbs | 1 + src/views/serverError.hbs | 2 ++ tests/index.test.js | 1 + tests/routes.test.js | 38 +++++++++++++++++++++++++++++++++++++ 8 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/controllers/err.js create mode 100644 src/views/clientError.hbs create mode 100644 src/views/serverError.hbs diff --git a/package.json b/package.json index 81cfbd0..37dba52 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "scripts": { "test": "node tests/index.test.js | tap-spec", "dev": "nodemon src/index.js", - "start": "node src/index.js" + "start": "node src/index.js", + "runBuild": "node src/database/db_build.js" }, "repository": { "type": "git", @@ -37,4 +38,4 @@ "url": "https://github.com/GSG-G7/Tour-Around/issues" }, "homepage": "https://github.com/GSG-G7/Tour-Around#readme" -} +} \ No newline at end of file diff --git a/src/controllers/err.js b/src/controllers/err.js new file mode 100644 index 0000000..7947f5e --- /dev/null +++ b/src/controllers/err.js @@ -0,0 +1,16 @@ +const clientError = (req, res) => { + res.status(404) + .render('clientError'); +}; + + +const serverError = (err, req, res, next) => { + res.status(500) + .render('serverError'); +}; + + +module.exports = { + clientError, + serverError, +}; diff --git a/src/controllers/homeData.js b/src/controllers/homeData.js index 2c55f9c..388e8c3 100644 --- a/src/controllers/homeData.js +++ b/src/controllers/homeData.js @@ -11,7 +11,11 @@ const postData = (req, res) => { postUser(req.body) .then(({ rows }) => postPlace(req.body, rows[0].user_id)) .catch((error) => console.log(error)); + getData() + .then(({ rows }) => res.status(200).render('home', { rows })); }; + + module.exports = { homeData, postData }; diff --git a/src/controllers/index.js b/src/controllers/index.js index 5fa609c..aec9a09 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -1,8 +1,12 @@ const router = require('express').Router(); -const {homeData, postData} = require('./homeData'); +const { homeData, postData } = require('./homeData'); +const { clientError, serverError } = require('./err'); router.get('/', homeData); router.post('/postPlace', postData); +router.use(clientError); +router.use(serverError); + module.exports = router; diff --git a/src/views/clientError.hbs b/src/views/clientError.hbs new file mode 100644 index 0000000..80aecfc --- /dev/null +++ b/src/views/clientError.hbs @@ -0,0 +1 @@ +

Client Error

\ No newline at end of file diff --git a/src/views/serverError.hbs b/src/views/serverError.hbs new file mode 100644 index 0000000..d2a302c --- /dev/null +++ b/src/views/serverError.hbs @@ -0,0 +1,2 @@ +

Server Error

+ \ No newline at end of file diff --git a/tests/index.test.js b/tests/index.test.js index 1cfc37d..7879fd2 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -1 +1,2 @@ const { test1 } = require('./functions.test'); +require('./routes.test'); diff --git a/tests/routes.test.js b/tests/routes.test.js index e69de29..6ec2f2a 100644 --- a/tests/routes.test.js +++ b/tests/routes.test.js @@ -0,0 +1,38 @@ +const test = require('tape'); +const supertest = require('supertest'); +const app = require('./../src/app'); + +test('Status Code equal 200', (t) => { + supertest(app) + .get('/') + .expect(200) + .expect('content-type', /html/) + .end((err, res) => { + t.error(err); + t.equal(res.text.includes('

Hi Team

'), true, 'The page should have a heading tag'); + t.end(); + }); +}); + + +test('post', (t) => { + supertest(app) + .post('/postPlace') + .send({ + userName: 'yosef', + email: 'yosef@gmail.com', + phone: '45646', + placeName: 'take', + location: 'gaza', + service: 'food', + delivery: false, + img: 'https://www.google.com' + }) + .expect(201) + .expect('content-type', /html/) + .end((err, res) => { + // t.error(err); + console.log(res.body); + t.end(); + }); +});