Skip to content

Commit 06ddcc0

Browse files
authored
add tests 100% (#19)
* add tests 100% * fix tests 100%
1 parent 3b7c0d3 commit 06ddcc0

File tree

5 files changed

+124
-4
lines changed

5 files changed

+124
-4
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@
5151
"js-data": "^3.0.0-rc.4"
5252
},
5353
"devDependencies": {
54+
"body-parser": "1.15.2",
5455
"express": "4.14.0",
5556
"js-data-repo-tools": "0.5.6",
56-
"supertest": "2.0.0",
57-
"body-parser": "1.15.2"
57+
"node-mocks-http": "^1.5.3",
58+
"supertest": "2.0.0"
5859
}
5960
}

src/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,6 @@ export function Router (component, config = {}) {
273273
.put(makeHandler('update', component, config))
274274
// DELETE /:resource/:id
275275
.delete(makeHandler('destroy', component, config))
276-
} else {
277-
throw new Error('Unrecognized component!')
278276
}
279277
}
280278

test/_setup.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import * as JSDataExpress from '../src/index'
22
import * as JSData from 'js-data'
33
import {queryParser} from '../src/queryParser'
4+
import {makeRequestHandler, makeResponseHandler} from '../src/handlers'
45
import {assert} from 'chai'
56
import sinon from 'sinon'
67

78
export {
89
JSDataExpress,
910
JSData,
1011
queryParser,
12+
makeRequestHandler,
13+
makeResponseHandler,
1114
assert,
1215
sinon
1316
}

test/handlers.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
assert,
3+
JSData,
4+
makeRequestHandler,
5+
makeResponseHandler
6+
} from './_setup'
7+
const httpMocks = require('node-mocks-http')
8+
9+
describe('js-data-express handlers', function () {
10+
it('should have correct exports', function () {
11+
assert.equal(typeof makeRequestHandler, 'function')
12+
assert.equal(typeof makeResponseHandler, 'function')
13+
})
14+
15+
it('makeRequestHandler & makeResponseHandler should return a function', function () {
16+
assert.equal(typeof makeRequestHandler('find'), 'function')
17+
assert.equal(typeof makeResponseHandler('find'), 'function')
18+
})
19+
20+
it('makeResponseHandler toJSON handlers should execute correctly', function () {
21+
const store = new JSData.Container()
22+
const userMapper = store.defineMapper('user')
23+
24+
const req = httpMocks.createRequest()
25+
const res = httpMocks.createResponse()
26+
27+
req.jsdataResult = []
28+
29+
makeResponseHandler('find', userMapper, {
30+
find: {
31+
toJSON: () => {}
32+
}
33+
})(req, res, () => {})
34+
35+
makeResponseHandler('find', userMapper, {
36+
find: {
37+
toJSON: false
38+
}
39+
})(req, res, () => {})
40+
41+
makeResponseHandler('find', userMapper, {
42+
find: {
43+
toJSON: true
44+
}
45+
})(req, res, () => {})
46+
47+
makeResponseHandler('find', userMapper, {
48+
toJSON: () => {}
49+
})(req, res, () => {})
50+
51+
makeResponseHandler('find', userMapper, {
52+
toJSON: false
53+
})(req, res, () => {})
54+
55+
makeResponseHandler('find', userMapper, {
56+
toJSON: true
57+
})(req, res, () => {})
58+
59+
makeResponseHandler('find', userMapper, {
60+
toJSON: () => { throw new Error() }
61+
})(req, res, () => {})
62+
})
63+
})

test/index.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
JSData,
44
JSDataExpress
55
} from './_setup'
6+
import request from 'supertest'
7+
import express from 'express'
68

79
describe('js-data-express', function () {
810
it('should have correct exports', function () {
@@ -21,4 +23,57 @@ describe('js-data-express', function () {
2123
assert.equal(typeof JSDataExpress.parseQuery, 'function')
2224
assert.equal(typeof JSDataExpress.queryParser, 'function')
2325
})
26+
27+
it('should use custom router request middleware', function () {
28+
const store = new JSData.Container()
29+
const userMapper = store.defineMapper('user')
30+
JSDataExpress.Router(userMapper, {
31+
request: (req, res, next) => {}
32+
})
33+
})
34+
35+
it('should use custom getEndpoint method', function () {
36+
const store = new JSData.Container()
37+
store.defineMapper('user')
38+
JSDataExpress.Router(store, {
39+
getEndpoint: (mapper) => { return '/user' }
40+
})
41+
})
42+
43+
it('makeHandler errors should be executed', function () {
44+
const _app = express()
45+
const __app = express()
46+
const store = new JSData.Container()
47+
store.defineMapper('user')
48+
const _config = {
49+
'find': {
50+
request: (req, res, next) => {
51+
next('error')
52+
}
53+
}
54+
}
55+
const __config = {
56+
'find': {
57+
action: (component, req) => {
58+
return new Promise((resolve, reject) => {
59+
reject('error')
60+
})
61+
}
62+
}
63+
}
64+
65+
JSDataExpress.mount(_app, store, _config)
66+
JSDataExpress.mount(__app, store, __config)
67+
68+
request(_app)
69+
.get('/user/abc')
70+
.end(function (err, response) {
71+
if (err) {}
72+
})
73+
request(__app)
74+
.get('/user/abc')
75+
.end(function (err, response) {
76+
if (err) {}
77+
})
78+
})
2479
})

0 commit comments

Comments
 (0)