Skip to content

Commit 1b7b107

Browse files
pikjmdobry
authored andcommitted
Test Coverage (fetch, updateMany/createMany, addAction(s)) (#56)
* Fix addAction to pass through opts into _opts on functional call * Adds tests for #action(<id>) and #action(<params>) queries * Add addActions test * Setup store and adapter in start.js * Sum and count tests * Tests for createMany and updateMany * Add fetch test
1 parent 9d8ca3a commit 1b7b107

15 files changed

+247
-40
lines changed

fetch/karma.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = function (config) {
5252
browsers: browsers,
5353
files: [
5454
'node_modules/babel-polyfill/dist/polyfill.js',
55+
'node_modules/whatwg-fetch/fetch.js',
5556
'node_modules/js-data/dist/js-data.js',
5657
'fetch/dist/js-data-fetch.js',
5758
'fetch/karma.start.js',

fetch/karma.start.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* global JSData:true, JSDataHttp:true, sinon:true, chai:true */
2+
23
before(function () {
34
var Test = this
5+
Test.TEST_FETCH = true
46
Test.fail = function (msg) {
57
if (msg instanceof Error) {
68
console.log(msg.stack)
@@ -14,7 +16,14 @@ before(function () {
1416
}
1517
Test.sinon = sinon
1618
Test.JSData = JSData
19+
Test.addAction = JSDataHttp.addAction
20+
Test.addActions = JSDataHttp.addActions
1721
Test.HttpAdapter = JSDataHttp.HttpAdapter
22+
23+
Test.store = new JSData.DataStore()
24+
Test.adapter = new Test.HttpAdapter()
25+
Test.store.registerAdapter('http', Test.adapter, { default: true })
26+
1827
Test.User = new JSData.Mapper({
1928
name: 'user'
2029
})
@@ -24,14 +33,13 @@ before(function () {
2433
basePath: 'api'
2534
})
2635

36+
Test.User.registerAdapter('http', Test.adapter, { default: true })
37+
Test.Post.registerAdapter('http', Test.adapter, { default: true })
2738
console.log('Testing against js-data ' + JSData.version.full)
2839
})
2940

3041
beforeEach(function () {
3142
var Test = this
32-
Test.adapter = new Test.HttpAdapter()
33-
Test.User.registerAdapter('http', Test.adapter, { default: true })
34-
Test.Post.registerAdapter('http', Test.adapter, { default: true })
3543

3644
Test.p1 = { author: 'John', age: 30, id: 5 }
3745
Test.p2 = { author: 'Sally', age: 31, id: 6 }

fetch/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
"axios",
1919
"rest",
2020
"adapter",
21-
"http",
22-
"fetch"
21+
"http"
2322
],
2423
"dependencies": {
2524
"js-data-adapter": "~0.8.1"

karma.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = function (config) {
5252
browsers: browsers,
5353
files: [
5454
'node_modules/babel-polyfill/dist/polyfill.js',
55+
'node_modules/whatwg-fetch/fetch.js',
5556
'node_modules/js-data/dist/js-data.js',
5657
'dist/js-data-http.js',
5758
'karma.start.js',

karma.start.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* global JSData:true, JSDataHttp:true, sinon:true, chai:true */
2+
23
before(function () {
34
var Test = this
5+
Test.TEST_FETCH = false
46
Test.fail = function (msg) {
57
if (msg instanceof Error) {
68
console.log(msg.stack)
@@ -14,7 +16,15 @@ before(function () {
1416
}
1517
Test.sinon = sinon
1618
Test.JSData = JSData
19+
Test.addAction = JSDataHttp.addAction
20+
Test.addActions = JSDataHttp.addActions
1721
Test.HttpAdapter = JSDataHttp.HttpAdapter
22+
23+
Test.store = new JSData.DataStore()
24+
Test.adapter = new Test.HttpAdapter()
25+
26+
Test.store.registerAdapter('http', Test.adapter, { default: true })
27+
1828
Test.User = new JSData.Mapper({
1929
name: 'user'
2030
})
@@ -24,14 +34,13 @@ before(function () {
2434
basePath: 'api'
2535
})
2636

37+
Test.User.registerAdapter('http', Test.adapter, { default: true })
38+
Test.Post.registerAdapter('http', Test.adapter, { default: true })
2739
console.log('Testing against js-data ' + JSData.version.full)
2840
})
2941

3042
beforeEach(function () {
3143
var Test = this
32-
Test.adapter = new Test.HttpAdapter()
33-
Test.User.registerAdapter('http', Test.adapter, { default: true })
34-
Test.Post.registerAdapter('http', Test.adapter, { default: true })
3544

3645
Test.p1 = { author: 'John', age: 30, id: 5 }
3746
Test.p2 = { author: 'Sally', age: 31, id: 6 }

node/mocha.start.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ before(function () {
1818
}
1919
Test.sinon = require('sinon')
2020
Test.JSData = require('js-data')
21+
Test.addAction = require('./dist/js-data-http-node').addAction
22+
Test.addActions = require('./dist/js-data-http-node').addActions
2123
Test.HttpAdapter = require('./dist/js-data-http-node').HttpAdapter
24+
Test.store = new Test.JSData.DataStore()
25+
Test.adapter = new Test.HttpAdapter()
26+
Test.store.registerAdapter('http', Test.adapter, { default: true })
27+
2228
Test.User = new Test.JSData.Mapper({
2329
name: 'user'
2430
})
@@ -28,15 +34,13 @@ before(function () {
2834
basePath: 'api'
2935
})
3036

37+
Test.User.registerAdapter('http', Test.adapter, { default: true })
38+
Test.Post.registerAdapter('http', Test.adapter, { default: true })
3139
console.log('Testing against js-data ' + Test.JSData.version.full)
3240
})
3341

3442
beforeEach(function () {
3543
var Test = this
36-
Test.adapter = new Test.HttpAdapter()
37-
Test.User.registerAdapter('http', Test.adapter, { default: true })
38-
Test.Post.registerAdapter('http', Test.adapter, { default: true })
39-
4044
Test.p1 = { author: 'John', age: 30, id: 5 }
4145
Test.p2 = { author: 'Sally', age: 31, id: 6 }
4246
Test.p3 = { author: 'Mike', age: 32, id: 7 }

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
"phantomjs-prebuilt": "2.1.12",
9696
"rollup-plugin-commonjs": "3.3.1",
9797
"rollup-plugin-replace": "1.1.1",
98-
"uglify-js": "2.7.0"
98+
"uglify-js": "2.7.0",
99+
"whatwg-fetch": "^1.0.0"
99100
}
100101
}

src/index.js

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global fetch:true Headers:true Request:true */
1+
/* global fetch:true Headers:true */
22

33
import {utils} from 'js-data'
44
import axios from '../node_modules/axios/dist/axios'
@@ -53,7 +53,7 @@ function buildUrl (url, params) {
5353
}
5454

5555
val.forEach(function (v) {
56-
if (window.toString.call(v) === '[object Date]') {
56+
if (toString.call(v) === '[object Date]') {
5757
v = v.toISOString()
5858
} else if (utils.isObject(v)) {
5959
v = utils.toJson(v)
@@ -558,20 +558,19 @@ Adapter.extend({
558558
* @param {Object} config.headers Headers for the request.
559559
* @param {Object} config.params Querystring for the request.
560560
* @param {string} config.url Url for the request.
561-
* @param {Object} [opts] Configuration options.
562561
*/
563-
fetch (config, opts) {
562+
fetch (config) {
564563
const requestConfig = {
565564
method: config.method,
566565
// turn the plain headers object into the Fetch Headers object
567-
headers: new Headers(config.headers)
566+
headers: new Headers(config.headers || {})
568567
}
569568

570569
if (config.data) {
571570
requestConfig.body = utils.toJson(config.data)
572571
}
573572

574-
return fetch(new Request(buildUrl(config.url, config.params), requestConfig))
573+
return fetch(buildUrl(config.url, config.params), requestConfig)
575574
.then((response) => {
576575
response.config = {
577576
method: config.method,
@@ -969,7 +968,7 @@ Adapter.extend({
969968
sum (mapper, field, query, opts) {
970969
query || (query = {})
971970
opts || (opts = {})
972-
if (!utils.utils.isString(field)) {
971+
if (!utils.isString(field)) {
973972
throw new Error('field must be a string!')
974973
}
975974
opts.params = this.getParams(opts)
@@ -1064,7 +1063,7 @@ Adapter.extend({
10641063
*
10651064
* // GET /reports/schools/:school_id/teachers
10661065
* addAction('getTeacherReports', {
1067-
* basePath: 'reports/schools',
1066+
* endpoint: 'reports/schools',
10681067
* pathname: 'teachers',
10691068
* method: 'GET'
10701069
* })(store.getMapper('school'))
@@ -1098,42 +1097,39 @@ export function addAction (name, opts) {
10981097
opts.response = opts.response || function (response) { return response }
10991098
opts.responseError = opts.responseError || function (err) { return utils.reject(err) }
11001099
mapper[name] = function (id, _opts) {
1100+
_opts = _opts || {}
11011101
if (utils.isObject(id)) {
11021102
_opts = id
11031103
}
1104-
_opts = _opts || {}
1105-
let adapter = this.getAdapter(opts.adapter || this.defaultAdapter || 'http')
1106-
let config = {}
1107-
utils.fillIn(config, opts)
1108-
if (!_opts.hasOwnProperty('endpoint') && config.endpoint) {
1109-
_opts.endpoint = config.endpoint
1110-
}
1104+
utils.fillIn(_opts, opts)
1105+
let adapter = this.getAdapter(_opts.adapter || this.defaultAdapter || 'http')
1106+
const config = {}
1107+
config.mapper = this.name
1108+
utils.deepMixIn(config, _opts)
1109+
config.method = config.method || 'GET'
11111110
if (typeof _opts.getEndpoint === 'function') {
11121111
config.url = _opts.getEndpoint(this, _opts)
11131112
} else {
11141113
let args = [
11151114
_opts.basePath || this.basePath || adapter.basePath,
1116-
adapter.getEndpoint(this, utils.isSorN(id) ? id : null, _opts)
1115+
adapter.getEndpoint(this, id, _opts)
11171116
]
11181117
if (utils.isSorN(id)) {
11191118
args.push(id)
11201119
}
11211120
args.push(opts.pathname || name)
11221121
config.url = makePath.apply(null, args)
11231122
}
1124-
config.method = config.method || 'GET'
1125-
config.mapper = this.name
1126-
utils.deepMixIn(config, _opts)
11271123
return utils.resolve(config)
1128-
.then(_opts.request || opts.request)
1124+
.then(_opts.request)
11291125
.then((config) => adapter.HTTP(config))
11301126
.then((data) => {
11311127
if (data && data.config) {
11321128
data.config.mapper = this.name
11331129
}
11341130
return data
11351131
})
1136-
.then(_opts.response || opts.response, _opts.responseError || opts.responseError)
1132+
.then(_opts.response, _opts.responseError)
11371133
}
11381134
return mapper
11391135
}

test/count.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
describe('sum', function () {
2+
it('should include count=true in query_params', function (done) {
3+
var Test = this
4+
5+
setTimeout(function () {
6+
Test.requests[0].respond(200, { 'Content-Type': 'application/json' }, '{"count": 5}')
7+
}, 5)
8+
9+
Test.adapter.count(Test.Post).then(function (result) {
10+
Test.assert.equal(Test.requests[0].url, 'api/posts?count=true')
11+
done()
12+
})
13+
})
14+
})

test/createMany.test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
describe('createMany', function () {
2-
it('should createMany')
2+
it('should createMany', function (done) {
3+
var Test = this
4+
5+
setTimeout(function () {
6+
Test.requests[0].respond(200, { 'Content-Type': 'application/json' }, '')
7+
}, 5)
8+
9+
var many = [{ author_id: 2, text: 'bar' }, { author_id: 2, text: 'foo' }]
10+
Test.Post.createMany(many).then(function (result) {
11+
Test.assert.equal(Test.requests[0].url, 'api/posts')
12+
Test.assert.equal(Test.requests[0].method, 'POST')
13+
Test.assert.equal(Test.requests[0].requestBody, JSON.stringify(many))
14+
done()
15+
})
16+
})
317
})

test/fetch.test.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
11
describe('fetch', function () {
2-
it('should fetch')
2+
it('should fetch from a URL', function (done) {
3+
var Test = this
4+
// Don't test fetch for Node
5+
try {
6+
fetch // eslint-disable-line
7+
} catch (e) {
8+
return this.skip()
9+
}
10+
if (Test.TEST_FETCH) {
11+
Test.xhr = Test.sinon.useFakeXMLHttpRequest()
12+
Test.requests = []
13+
Test.xhr.onCreate = function (xhr) {
14+
Test.requests.push(xhr)
15+
}
16+
}
17+
18+
setTimeout(function () {
19+
Test.requests[0].respond(200, { 'Content-Type': 'application/json' }, '{}')
20+
}, 300)
21+
22+
Test.adapter.fetch({
23+
method: 'get',
24+
params: { active: true },
25+
url: '/api/foos'
26+
}).then(function (response) {
27+
var request = Test.requests[0]
28+
Test.assert.equal(request.method, 'GET')
29+
Test.assert.equal(request.url, '/api/foos?active=true')
30+
if (Test.TEST_FETCH) {
31+
Test.xhr.restore()
32+
}
33+
done()
34+
})
35+
})
336
})

test/static.addAction.test.js

+55-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
11
describe('static addAction', function () {
2-
it('should addAction')
2+
it('should addAction', function (done) {
3+
var Test = this
4+
var SchoolMapper = Test.store.defineMapper('school', {})
5+
6+
// GET async/reports/schools/:school_id/teachers
7+
Test.addAction('getTeacherReportsAsync', {
8+
basePath: 'async/',
9+
endpoint: 'reports/schools',
10+
pathname: 'teachers',
11+
method: 'GET'
12+
})(SchoolMapper)
13+
14+
setTimeout(function () {
15+
Test.requests[0].respond(200, { 'Content-Type': 'text/plain' }, '')
16+
}, 5)
17+
18+
SchoolMapper.getTeacherReportsAsync(1234).then(function (response) {
19+
Test.assert.equal(1, Test.requests.length)
20+
Test.assert.equal(Test.requests[0].url, 'async/reports/schools/1234/teachers', 'Add action configures basePath, endpoint and pathname')
21+
Test.assert.equal(Test.requests[0].method, 'GET')
22+
done()
23+
})
24+
})
25+
26+
it('addAction action is callable with params instead of id', function (done) {
27+
var Test = this
28+
var adapter = Test.adapter
29+
var store = new Test.JSData.DataStore()
30+
store.registerAdapter('http', adapter, { default: true })
31+
var SchoolMapper = store.defineMapper('school', {})
32+
33+
// GET async/reports/schools/teachers
34+
Test.addAction('getAllTeacherReportsAsync', {
35+
basePath: 'async/',
36+
endpoint: 'reports/schools',
37+
pathname: 'teachers',
38+
method: 'GET'
39+
})(SchoolMapper)
40+
41+
setTimeout(function () {
42+
Test.requests[0].respond(200, { 'Content-Type': 'text/plain' }, '')
43+
}, 5)
44+
45+
// GET async/reports/schools/teachers?<key>=<value>
46+
SchoolMapper.getAllTeacherReportsAsync({
47+
params: {
48+
subject: 'esperanto'
49+
}
50+
}).then(function (response) {
51+
Test.assert.equal(1, Test.requests.length)
52+
Test.assert.equal(Test.requests[0].url, 'async/reports/schools/teachers?subject=esperanto', 'Add action configures basePath, endpoint, pathname, and querystring')
53+
Test.assert.equal(Test.requests[0].method, 'GET')
54+
done()
55+
})
56+
})
357
})

0 commit comments

Comments
 (0)