Skip to content

Commit

Permalink
added supertest files
Browse files Browse the repository at this point in the history
  • Loading branch information
aa7401 committed Sep 14, 2019
1 parent e886bde commit 67e18b1
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 06 Code Quality.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_.
5. rewrite another promise as an _async function_.
28 changes: 28 additions & 0 deletions exercises/07_unit_testing/supertest/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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",
}
}
]
}
57 changes: 57 additions & 0 deletions exercises/07_unit_testing/supertest/index.js
Original file line number Diff line number Diff line change
@@ -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}`))
31 changes: 31 additions & 0 deletions exercises/07_unit_testing/supertest/package.json
Original file line number Diff line number Diff line change
@@ -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": [
"<rootDir>/jest-test.config.js"
]
}
}
13 changes: 13 additions & 0 deletions exercises/07_unit_testing/supertest/unit tests/index.spec.js
Original file line number Diff line number Diff line change
@@ -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
})
33 changes: 33 additions & 0 deletions exercises/07_unit_testing/supertest/views/home.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

<!DOCTYPE html>
<html>
<head>
<title>ToDo List</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>ToDo List</h1>
<h2>My List</h2>
{{#if msg}}
<p class="msg">{{msg}}</p>
{{/if}}
{{#if items.length}}
<table>
<caption>Shopping List</caption>
<tr><th>Item</th><th>Qty</th><th>Action</th></tr>
{{#each items}}
<tr><td>{{this.item}}</td><td>{{this.qty}}</td><td><a href="/delete/{{this.key}}">delete</a></td></tr>
{{/each}}
</table>
{{else}}
<h2>Your list is empty, lets add some items...</h2>
{{/if}}
<form method="post" action="/">
Item:
<input type="text" name="item" />
Qty:
<input type="text" name="qty" />
<input type="submit" value="Add Item" />
</form>
</body>
</html>

0 comments on commit 67e18b1

Please sign in to comment.