forked from web/foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
exercises/07_unit_testing/supertest/unit tests/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |