diff --git a/package.json b/package.json index c6a251c..e8c17c1 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,10 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start:watch": "nodemon ./src/index.js" + "test-express": "node ./tests/express-test | tap-spec", + "test-database": "node ./tests/database.test | tap-spec", + "start:watch": "nodemon ./src/index.js", + "coverage": "istanbul cover ./tests/database.test.js && istanbul cover ./tests/express-test.js" }, "repository": { "type": "git", @@ -29,6 +31,7 @@ "eslint": "^4.4.1", "istanbul": "^0.4.5", "nodemon": "^1.11.0", + "shot": "^3.4.2", "supertest": "^3.0.0", "tap-diff": "^0.1.1", "tap-spec": "^4.1.1", diff --git a/src/controllers/browse.js b/src/controllers/browse.js index 95a9059..0c5f936 100644 --- a/src/controllers/browse.js +++ b/src/controllers/browse.js @@ -18,15 +18,12 @@ exports.get = (req, res, next) => { } showData() .then ((stuff, text)=>{ - // console.log(stuff,text); res.render('browse',{ activePage:{ browse: true }, data: stuff.rows }); - console.log('this is out stuff: ', stuff); - // res.rows?? }) .catch((stuff, text)=>{ res.status(500); diff --git a/tests/database.test.js b/tests/database.test.js new file mode 100644 index 0000000..0bb5f77 --- /dev/null +++ b/tests/database.test.js @@ -0,0 +1,96 @@ +const tape = require('tape'); +const shot = require('shot'); +const fs = require('fs'); +const path = require('path'); +const buildDataBase = require('../src/model/db_build'); +const dbConnection = require('../src/model/db_connection'); +const app = require('../src/app'); + +// Select all firstnames from users +tape("Select all firstnames from users", t => { + buildDataBase(); + const expected = [ + { firstname: 'Abdullah'}, + { firstname: 'Max'}, + { firstname: 'Jen'}, + { firstname: 'Rebeca'} + ]; + + dbConnection.query('SELECT firstname FROM users;', (err, res) => { + if (err) { + t.error(err, 'This is an error'); + } else { + const actual = res.rows; + t.deepEquals(actual, expected, 'db query should get firstnames'); + t.end(); + } + }) +}) + +// test ideas database +tape("get idea titles from ideas database", t => { + buildDataBase(); + const expected = [ + { ideatitle: 'Movie recommendation app' }, + { ideatitle: 'travel giffy app' }, + { ideatitle: 'holiday inspiration app' }, + { ideatitle: 'mario cv app' } + ]; + + dbConnection.query('SELECT ideatitle FROM ideas;', (err, res) => { + if (err) { + t.error(err, 'This is an error'); + } else { + const actual = res.rows; + t.deepEquals(actual, expected, 'db query should get titles'); + t.end(); + } + }) +}) + +tape("check if post adds a new user to user table", t => { + buildDataBase(); + const insertUserSql = "INSERT INTO users (firstname, email) VALUES ($1, $2)"; + const expected = [ + { firstname: 'Amelie', email: 'amelie@amelie.com'} + ]; + + dbConnection.query(insertUserSql, [expected[0].firstname, expected[0].email], (err, res) => { + if (err) { + t.error(err, 'This is an error'); + } else { + dbConnection.query("SELECT firstname, email FROM users WHERE firstname='Amelie';", + (err, res) => { + if (err) { + t.error('This is an error'); + } else { + const actual = res.rows; + t.deepEquals(actual, expected, 'firstname should be Amelie'); + t.end(); + } + }) + } + }) +}) + + + + +// inserting a new idea +// tape("check if a post adds a new idea to ideatable", t => { +// buildDataBase(); +// const insertNewIdeaSql = "INSERT INTO ideas (userid, dataadded, ideatitle, ideadesc) VALUES ($1, $2, $3, $4)"; +// const expected = [ +// { userid: 5, dataadded: 2017-08-17, } +// ]; +// +// dbConnection.query('SELECT ideatitle FROM ideas;', (err, res) => { +// if (err) { +// t.error(err, 'This is an error'); +// } else { +// const actual = res.rows; +// t.deepEquals(actual, expected, 'db query should get titles'); +// t.end(); +// } +// }) +// }) diff --git a/tests/express-test.js b/tests/express-test.js new file mode 100644 index 0000000..6c68296 --- /dev/null +++ b/tests/express-test.js @@ -0,0 +1,46 @@ +const test = require('tape'); +const supertest = require('supertest'); +const app = require('./../src/app'); + +// check if tape works +test('1 equals 1', (t) => { + t.equals(1, 1, 'one should equal one'); + t.end(); +}); + +// check if supetest works +test('check if supertest works', (t) => { + supertest(app) + .get('/') + .expect(200) + .expect('Content-Type', /json/) + .end((err, res) => { + t.same(res.statusCode, 200, 'Status code is 200'); + t.end(); + }) +}) + +// check we get 404 on non-existing endpoint +test('404 when endpoint does not exist', (t) => { + supertest(app) + .get('/bsdjkhgablsg') + .expect(404) + .expect('Content-Type', /json/) + .end((err, res) => { + t.same(res.statusCode, 404, 'Status code is 404'); + t.end(); + }) +}) + +// test('check prepopulated browse database table', (t) => { +// supertest(app) +// .get('/browse') +// .expect(200) +// .expect('Content-Type', /json/) +// .end((err, res) => { +// let expected = "Movie recommendation app"; +// let actual = res; +// t.equals(actual, expected, 'Should be Movie recommendation app') +// t.end(); +// }) +// })