Skip to content

Commit 5803417

Browse files
committed
Setup first basic tests
1 parent d0359f8 commit 5803417

6 files changed

Lines changed: 2268 additions & 23 deletions

File tree

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["node6"],
3+
"plugins": ["transform-async-to-generator"]
4+
}

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"start": "micro src/index.js",
8-
"dev": "NODE_ENV=development nodemon --config package.json src/index.js"
8+
"dev": "NODE_ENV=development nodemon --config package.json src/index.js",
9+
"test": "jest"
910
},
1011
"author": "Max Stoiber <contact@mxstbr.com> (http://mxstbr.com/)",
1112
"license": "MIT",
@@ -14,7 +15,14 @@
1415
"micro": "6.1.0",
1516
"promise": "^7.1.1"
1617
},
17-
"devDependencies": {},
18+
"devDependencies": {
19+
"babel-jest": "^18.0.0",
20+
"babel-plugin-transform-async-to-generator": "^6.16.0",
21+
"babel-polyfill": "^6.20.0",
22+
"babel-preset-node6": "^11.0.0",
23+
"jest": "^18.1.0",
24+
"request-promise": "^4.1.1"
25+
},
1826
"execMap": {
1927
"js": "micro"
2028
}

tests/errors.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const request = require('request-promise')
2+
const { listen } = require('./utils')
3+
4+
const service = require('../src')
5+
let url
6+
7+
beforeEach(async () => {
8+
url = await listen(service)
9+
})
10+
11+
it('should throw an error if no pathname is provided', async () => {
12+
const fn = jest.fn()
13+
try {
14+
await request(url)
15+
fn()
16+
} catch (err) {
17+
expect(err.statusCode).toBe(400)
18+
expect(err.message.indexOf('include a path')).toBeGreaterThan(-1)
19+
}
20+
expect(fn).not.toHaveBeenCalled()
21+
})
22+
23+
it('should throw an error if a PUT request comes in', async () => {
24+
const fn = jest.fn()
25+
try {
26+
await request.put(`${url}/test`)
27+
expect(err.message.indexOf('make a GET or a POST request')).toBeGreaterThan(-1)
28+
fn()
29+
} catch (err) {
30+
expect(err.statusCode).toBe(400)
31+
}
32+
expect(fn).not.toHaveBeenCalled()
33+
})

tests/items.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const request = require('request-promise')
2+
const { listen, mockDb } = require('./utils')
3+
4+
jest.mock('../src/db', () => mockDb)
5+
const service = require('../src')
6+
let url
7+
8+
beforeEach(async () => {
9+
url = await listen(service)
10+
})
11+
12+
afterEach(async () => {
13+
mockDb._reset()
14+
})
15+
16+
describe('single', () => {
17+
it('should set the views of a non-existant path to one', async () => {
18+
const body = JSON.parse(await request(`${url}/nonexistant`))
19+
expect(body.views).toEqual(1)
20+
})
21+
22+
it('should increment the views of an existant path', async () => {
23+
await request(`${url}/existant`)
24+
const body = JSON.parse(await request(`${url}/existant`))
25+
expect(body.views).toEqual(2)
26+
})
27+
28+
it('should return 0 views on a non-existant path if inc is set to false', async () => {
29+
const body = JSON.parse(await request(`${url}/path?inc=false`))
30+
expect(body.views).toEqual(0)
31+
})
32+
33+
it('should not increment the views of an existant path if inc is set to false', async () => {
34+
await request(`${url}/existant`)
35+
const body = JSON.parse(await request(`${url}/existant?inc=false`))
36+
expect(body.views).toEqual(1)
37+
})
38+
39+
it('should not return anything for a POST request', async () => {
40+
const body = await request.post(`${url}/existant`)
41+
expect(body).toEqual('')
42+
})
43+
})
44+
45+
describe('all', () => {
46+
it('should return an empty array if no previous views exist', async () => {
47+
const body = JSON.parse(await request(`${url}/?all=true`))
48+
expect(body.data).toEqual({})
49+
expect(body.time).toBeDefined()
50+
})
51+
52+
it('should return previous views of one route', async () => {
53+
await request(`${url}/route`)
54+
await request(`${url}/route`)
55+
const body = JSON.parse(await request(`${url}/?all=true`))
56+
expect(Object.keys(body.data).length).toBe(1)
57+
expect(body.data['/route'].views).toBeDefined()
58+
expect(body.data['/route'].views.length).toBe(2)
59+
})
60+
61+
it('should return previous views of all routes', async () => {
62+
await request(`${url}/route`)
63+
await request(`${url}/route`)
64+
await request(`${url}/route2`)
65+
await request(`${url}/route2`)
66+
await request(`${url}/route2`)
67+
const body = JSON.parse(await request(`${url}/?all=true`))
68+
expect(Object.keys(body.data).length).toBe(2)
69+
expect(body.data['/route'].views).toBeDefined()
70+
expect(body.data['/route'].views.length).toBe(2)
71+
expect(body.data['/route2'].views).toBeDefined()
72+
expect(body.data['/route2'].views.length).toBe(3)
73+
})
74+
})

tests/utils.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const micro = require('micro')
2+
3+
// Mock the database
4+
const DB = () => {
5+
let data = {}
6+
let DELAY = 1
7+
8+
return {
9+
get: (key) => data[key],
10+
put: (key, val) => new Promise((res, rej) => {
11+
setTimeout(() => {
12+
data[key] = val
13+
res()
14+
}, DELAY)
15+
}),
16+
has: (key) => !!data[key],
17+
keys: () => Object.keys(data),
18+
// Custom methods used in tests
19+
_reset: () => { data = {} },
20+
_setDelay: (ms) => { DELAY = ms }
21+
}
22+
}
23+
24+
// Taken from the zeit/micro test suite
25+
// https://github.com/zeit/micro/blob/9bb0f0cb9b9406e08b3bccbcd827d96989b4e16a/test/index.js#L13-L26
26+
const listen = (fn, opts) => {
27+
const server = micro(fn, opts)
28+
29+
return new Promise((resolve, reject) => {
30+
server.listen(err => {
31+
if (err) {
32+
return reject(err)
33+
}
34+
35+
const { port } = server.address()
36+
resolve(`http://localhost:${port}`)
37+
})
38+
})
39+
}
40+
41+
module.exports = exports = {}
42+
exports.listen = listen
43+
exports.mockDb = DB()

0 commit comments

Comments
 (0)