Skip to content
Permalink
Browse files
added some simple tests
  • Loading branch information
aa7401 committed Aug 18, 2018
1 parent 1e1f205 commit 9c719d9158ab059507aa7e4fbbd907325088c79f
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 22 deletions.

This file was deleted.

@@ -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
}
}
@@ -4,7 +4,7 @@
"description": "Simple dynamic website that uses the Google Books API",
"main": "index.js",
"scripts": {
"tests": "jest",
"test": "jest",
"watch": "node_modules/.bin/jest --coverage --watchAll",
"coverage": "node_modules/.bin/jest --coverage",
"jsdoc": "node_modules/.bin/jsdoc -d docs/jsdoc/ modules/"
@@ -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)
})

})
@@ -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}`))
@@ -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
@@ -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"
}
}
@@ -0,0 +1,7 @@

<h2>My List</h2>
<table>
{{#each items}}
<tr><td>{{this.item}}</td><td>{{this.qty}}</td></tr>
{{/each}}
</table>
@@ -0,0 +1,10 @@

<!DOCTYPE html>
<html>
<head>
<title>Onlne Bookshop</title>
</head>
<body>
{{{body}}}
</body>
</html>

0 comments on commit 9c719d9

Please sign in to comment.