Skip to content

Commit a939636

Browse files
committed
Add fetch test
1 parent c709bd6 commit a939636

File tree

8 files changed

+29
-12
lines changed

8 files changed

+29
-12
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/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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* global JSData:true, JSDataHttp:true, sinon:true, chai:true */
2+
23
before(function () {
34
var Test = this
45
Test.fail = function (msg) {

node/mocha.start.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
var { Headers, Request, fetch, Response } = require('whatwg-fetch') // eslint-disable-line
34
var querystring = require('querystring')
45
require('source-map-support').install()
56

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
"standard": {
3131
"parser": "babel-eslint",
3232
"globals": [
33-
"Headers",
34-
"fetch",
35-
"Request",
3633
"action",
3734
"describe",
3835
"it",
@@ -85,6 +82,7 @@
8582
"babel-plugin-transform-regenerator": "6.11.4",
8683
"babel-preset-stage-0": "6.5.0",
8784
"istanbul": "0.4.4",
85+
"js-data-fetch": "file:fetch",
8886
"js-data-repo-tools": "0.5.6",
8987
"karma": "1.2.0",
9088
"karma-browserstack-launcher": "1.0.1",
@@ -95,6 +93,7 @@
9593
"phantomjs-prebuilt": "2.1.12",
9694
"rollup-plugin-commonjs": "3.3.1",
9795
"rollup-plugin-replace": "1.1.1",
98-
"uglify-js": "2.7.0"
96+
"uglify-js": "2.7.0",
97+
"whatwg-fetch": "^1.0.0"
9998
}
10099
}

src/index.js

Lines changed: 4 additions & 5 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'
@@ -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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
describe('fetch', function () {
2-
it('should fetch')
2+
it('should fetch from a URL', function (done) {
3+
var Test = this
4+
5+
setTimeout(function () {
6+
Test.requests[0].respond(200, { 'Content-Type': 'application/json' }, '{}')
7+
}, 5)
8+
9+
Test.adapter.fetch({
10+
method: 'get',
11+
params: { active: true },
12+
url: '/api/foos'
13+
}).then(function (response) {
14+
var request = Test.requests[0]
15+
Test.assert.equal(request.method, 'GET')
16+
Test.assert.equal(request.url, '/api/foos?active=true')
17+
done()
18+
})
319
})

0 commit comments

Comments
 (0)