From 67e18b106ee33dbc070808a6b07d79e8c017fe5a Mon Sep 17 00:00:00 2001 From: Mark Tyers Date: Sat, 14 Sep 2019 16:45:47 +0100 Subject: [PATCH] added supertest files --- 06 Code Quality.md | 2 +- .../supertest/.vscode/launch.json | 28 +++++++++ exercises/07_unit_testing/supertest/index.js | 57 +++++++++++++++++++ .../07_unit_testing/supertest/package.json | 31 ++++++++++ .../supertest/unit tests/index.spec.js | 13 +++++ .../07_unit_testing/supertest/views/home.hbs | 33 +++++++++++ 6 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 exercises/07_unit_testing/supertest/.vscode/launch.json create mode 100644 exercises/07_unit_testing/supertest/index.js create mode 100644 exercises/07_unit_testing/supertest/package.json create mode 100644 exercises/07_unit_testing/supertest/unit tests/index.spec.js create mode 100644 exercises/07_unit_testing/supertest/views/home.hbs diff --git a/06 Code Quality.md b/06 Code Quality.md index 9d63607d..ca40be68 100644 --- a/06 Code Quality.md +++ b/06 Code Quality.md @@ -382,4 +382,4 @@ Run the `asyncFunctions.js` script. Note that it works in the same way as the pr 2. instead of printing the exchange rate, ask for the amount to be converted and them return the equivalent in the chosen currency 3. use the [OpenExchangeRates](https://openexchangerates.org/api/currencies.json) API to display the full name of the chosen currency 4. rewrite the `printObject` promise as an _async function_. -5. rewrite another promise as an _async function_. \ No newline at end of file +5. rewrite another promise as an _async function_. diff --git a/exercises/07_unit_testing/supertest/.vscode/launch.json b/exercises/07_unit_testing/supertest/.vscode/launch.json new file mode 100644 index 00000000..909a9fb5 --- /dev/null +++ b/exercises/07_unit_testing/supertest/.vscode/launch.json @@ -0,0 +1,28 @@ + +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Launch via npm", + "type": "node", + "request": "launch", + "cwd": "${workspaceFolder}", + "runtimeExecutable": "npm", + "runtimeArgs": ["run-script", "debug"], + "port": 9229 + }, + { + "type": "node", + "request": "launch", + "name": "Jest", + "program": "${workspaceFolder}/node_modules/.bin/jest", + "args": ["--runInBand"], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "disableOptimisticBPs": true, + "windows": { + "program": "${workspaceFolder}/node_modules/jest/bin/jest", + } + } + ] +} diff --git a/exercises/07_unit_testing/supertest/index.js b/exercises/07_unit_testing/supertest/index.js new file mode 100644 index 00000000..36a9e519 --- /dev/null +++ b/exercises/07_unit_testing/supertest/index.js @@ -0,0 +1,57 @@ +#!/usr/bin/env node + +'use strict' + +const Koa = require('koa') +const Router = require('koa-router') +const stat = require('koa-static') +const bodyParser = require('koa-bodyparser') +const handlebars = require('koa-hbs-renderer') + +const app = new Koa() +const router = new Router() +app.use(stat('public')) +app.use(bodyParser()) +app.use(handlebars({ paths: { views: `${__dirname}/views` } })) +app.use(router.routes()) + +const port = 8080 + +const data = [] + +router.get('/', async ctx => { + try { + const data = {} + if(ctx.query.msg) data.msg = ctx.query.msg + for(const key in data) data.items[key].key = key + ctx.render('home', data) + } catch(err) { + console.log(err.message) + ctx.render('home', {msg: err.message}) + } +}) + +router.post('/', ctx => { + try { + const body = ctx.request.body + body.qty = Number(body.qty) + if(isNaN(body.qty)) throw new Error('the quantity must be a number') + data.push({item: body.item, qty: body.qty}) + ctx.redirect('/') + } catch(err) { + console.log(err.message) + ctx.redirect(`/?msg=${err.message}`) + } +}) + +router.get('/delete/:key', ctx => { + try { + console.log(`key: ${ctx.params.key}`) + ctx.redirect('/msg=item deleted') + } catch(err) { + console.log(err.message) + ctx.redirect(`/${err.message}`) + } +}) + +module.exports = app.listen(port, () => console.log(`listening on port ${port}`)) diff --git a/exercises/07_unit_testing/supertest/package.json b/exercises/07_unit_testing/supertest/package.json new file mode 100644 index 00000000..a9895d12 --- /dev/null +++ b/exercises/07_unit_testing/supertest/package.json @@ -0,0 +1,31 @@ +{ + "name": "testingroutes", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "linter": "node_modules/.bin/eslint .", + "test": "node_modules/.bin/jest --coverage --runInBand", + "start": "nodemon --inspect ./start.js --ignore public/", + "debug": "node --nolazy --inspect-brk=9229 index.js" + }, + "author": "", + "license": "ISC", + "dependencies": { + "http-status-codes": "^1.3.2", + "koa": "^2.8.1", + "koa-bodyparser": "^4.2.1", + "koa-hbs-renderer": "^1.2.0", + "koa-router": "^7.4.0", + "koa-static": "^5.0.0" + }, + "devDependencies": { + "jest": "^24.9.0", + "supertest": "^4.0.2" + }, + "jest": { + "projects": [ + "/jest-test.config.js" + ] + } +} diff --git a/exercises/07_unit_testing/supertest/unit tests/index.spec.js b/exercises/07_unit_testing/supertest/unit tests/index.spec.js new file mode 100644 index 00000000..af42e454 --- /dev/null +++ b/exercises/07_unit_testing/supertest/unit tests/index.spec.js @@ -0,0 +1,13 @@ + +'use strict' + +const request = require('supertest') +const status = require('http-status-codes') + +const server = require('../index.js') + +beforeAll( async() => console.log('Jest starting!')) + +describe('GET /', done => { + // tests go here +}) diff --git a/exercises/07_unit_testing/supertest/views/home.hbs b/exercises/07_unit_testing/supertest/views/home.hbs new file mode 100644 index 00000000..1a9495f0 --- /dev/null +++ b/exercises/07_unit_testing/supertest/views/home.hbs @@ -0,0 +1,33 @@ + + + + + ToDo List + + + +

ToDo List

+

My List

+ {{#if msg}} +

{{msg}}

+ {{/if}} + {{#if items.length}} + + + + {{#each items}} + + {{/each}} +
Shopping List
ItemQtyAction
{{this.item}}{{this.qty}}delete
+ {{else}} +

Your list is empty, lets add some items...

+ {{/if}} +
+ Item: + + Qty: + + +
+ +