Skip to content

Commit 14f7936

Browse files
committed
Add fetch test
1 parent 682fe8e commit 14f7936

File tree

8 files changed

+48
-10
lines changed

8 files changed

+48
-10
lines changed

fetch/karma.conf.js

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)

fetch/package.json

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)

package.json

Lines changed: 2 additions & 1 deletion
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

Lines changed: 5 additions & 6 deletions
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,

test/fetch.test.js

Lines changed: 34 additions & 1 deletion
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
})

0 commit comments

Comments
 (0)