-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
209 additions
and
22 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
{ | ||
"env": { | ||
"es6": true, | ||
"jasmine": true, | ||
"node": true, | ||
"browser": true, | ||
"jest": true | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 8 | ||
}, | ||
"rules": { | ||
"arrow-body-style": 2, | ||
"arrow-spacing": [1, {"before": true, "after": true}], | ||
"brace-style": 2, | ||
"camelcase": [2, {"properties": "never"}], | ||
"eol-last": 1, | ||
"eqeqeq": 2, | ||
"global-require": 2, | ||
"handle-callback-err": 2, | ||
"indent": [1, "tab", {"SwitchCase": 1}], | ||
"key-spacing": ["error", {"beforeColon": false, "afterColon": true}], | ||
"linebreak-style": [2, "unix"], | ||
"no-cond-assign": 2, | ||
"no-dupe-args": 2, | ||
"no-dupe-keys": 2, | ||
"no-duplicate-case": 2, | ||
"no-empty": 1, | ||
"no-empty-function": 2, | ||
"no-multiple-empty-lines": 1, | ||
"no-extra-parens": 2, | ||
"no-func-assign": 2, | ||
"no-irregular-whitespace": 2, | ||
"no-magic-numbers": [1, {"ignore": [-1, 0, 1]}], | ||
"no-multi-spaces": 1, | ||
"no-multi-str": 1, | ||
"no-unexpected-multiline": 2, | ||
"no-unreachable": 2, | ||
"no-self-assign": 2, | ||
"no-trailing-spaces": 1, | ||
"no-undef": 2, | ||
"no-unused-vars": 1, | ||
"no-var": 2, | ||
"prefer-arrow-callback": 1, | ||
"prefer-const": 2, | ||
"quotes": [1, "single"], | ||
"semi": [1, "never"], | ||
"space-before-function-paren": [2, "never"], | ||
"strict": [2, "global"], | ||
"yoda": 2 | ||
} | ||
} |
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,68 @@ | ||
|
||
'use strict' | ||
|
||
/* eslint-disable no-magic-numbers */ | ||
|
||
const todo = require('../modules/todo') | ||
|
||
describe('add', () => { | ||
|
||
beforeEach( () => todo.add('bread', 1)) | ||
afterEach( () => todo.clear()) | ||
|
||
test('check there is a single item', done => { | ||
expect.assertions(1) | ||
const items = todo.getAll() | ||
expect(items.length).toBe(1) | ||
done() | ||
}) | ||
|
||
}) | ||
|
||
describe('clear', () => { | ||
|
||
test('clear list with items', done => { | ||
expect.assertions(1) | ||
todo.add('bread', 1) | ||
todo.add('butter', 2) | ||
todo.clear() | ||
const items = todo.getAll() | ||
expect(items.length).toBe(0) | ||
done() | ||
}) | ||
|
||
test('clearing empty list should throw error', () => { | ||
expect.assertions(1) | ||
try { | ||
todo.clear() | ||
} catch(err) { | ||
expect(err.message).toBe('trying to clear empty list') | ||
} | ||
}) | ||
|
||
}) | ||
|
||
describe('add', () => { | ||
|
||
afterEach( () => todo.clear()) | ||
|
||
test('adding a single item', () => { | ||
expect.assertions(1) | ||
todo.add('bread', 1) | ||
const items = todo.getAll() | ||
expect(items.length).toBe(1) | ||
}) | ||
|
||
}) | ||
|
||
describe('getAll', () => { | ||
|
||
test('retrieving a single item', () => { | ||
expect.assertions(2) | ||
todo.add('bread', 1) | ||
const items = todo.getAll() | ||
expect(Array.isArray(items)).toBeTruthy() | ||
expect(items.length).toBe(1) | ||
}) | ||
|
||
}) |
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,30 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict' | ||
|
||
const express = require('express') | ||
|
||
const handlebars = require('express3-handlebars').create({defaultLayout: 'main'}) | ||
const bodyParser = require('body-parser') | ||
const app = express() | ||
app.use(express.static('public')) | ||
app.use(bodyParser.urlencoded({ extended: true })) | ||
|
||
app.engine('handlebars', handlebars.engine) | ||
app.set('view engine', 'handlebars') | ||
|
||
const port = 8080 | ||
|
||
const todo = require('./modules/todo') | ||
|
||
app.get('/', async(req, res) => { | ||
try { | ||
todo.add('bread', 1) | ||
const data = todo.getAll() | ||
res.render('home', {items: data}) | ||
} catch(err) { | ||
console.log(err.message) | ||
} | ||
}) | ||
|
||
app.listen(port, () => console.log(`app 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,15 @@ | ||
|
||
'use strict' | ||
|
||
let data = [] | ||
|
||
module.exports.clear = () => { | ||
if(data.length === 0) throw new Error('trying to clear empty list') | ||
data = [] | ||
} | ||
|
||
module.exports.add = (item, qty = 1) => { | ||
data.push({item: item, qty: qty}) | ||
} | ||
|
||
module.exports.getAll = () => data |
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,25 @@ | ||
{ | ||
"name": "todo", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest", | ||
"watch": "node_modules/.bin/jest --coverage --watchAll", | ||
"coverage": "node_modules/.bin/jest --coverage" | ||
}, | ||
"jest": { | ||
"testEnvironment": "node", | ||
"verbose": true | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"eslint": "^5.3.0", | ||
"jest": "^23.5.0" | ||
}, | ||
"dependencies": { | ||
"express": "^4.16.3", | ||
"express3-handlebars": "^0.5.2" | ||
} | ||
} |
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,7 @@ | ||
|
||
<h2>My List</h2> | ||
<table> | ||
{{#each items}} | ||
<tr><td>{{this.item}}</td><td>{{this.qty}}</td></tr> | ||
{{/each}} | ||
</table> |
10 changes: 10 additions & 0 deletions
10
exercises/01_tdd_nodejs/todo/views/layouts/main.handlebars
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,10 @@ | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Onlne Bookshop</title> | ||
</head> | ||
<body> | ||
{{{body}}} | ||
</body> | ||
</html> |