-
Notifications
You must be signed in to change notification settings - Fork 26
Test Coverage (fetch, updateMany/createMany, addAction(s)) #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
86572ef
e9aefdb
2e1fff3
51ac4df
682fe8e
14f7936
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,7 @@ | |
"axios", | ||
"rest", | ||
"adapter", | ||
"http", | ||
"fetch" | ||
"http" | ||
], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
||
"dependencies": { | ||
"js-data-adapter": "~0.8.1" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,13 @@ before(function () { | |
} | ||
Test.sinon = require('sinon') | ||
Test.JSData = require('js-data') | ||
Test.addAction = require('./dist/js-data-http-node').addAction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to de-duplicate these 3 files. I would have preferred to do this via. es6 but I haven't gotten both build pipelines to work correctly with that =/ |
||
Test.addActions = require('./dist/js-data-http-node').addActions | ||
Test.HttpAdapter = require('./dist/js-data-http-node').HttpAdapter | ||
Test.store = new Test.JSData.DataStore() | ||
Test.adapter = new Test.HttpAdapter() | ||
Test.store.registerAdapter('http', Test.adapter, { default: true }) | ||
|
||
Test.User = new Test.JSData.Mapper({ | ||
name: 'user' | ||
}) | ||
|
@@ -28,15 +34,13 @@ before(function () { | |
basePath: 'api' | ||
}) | ||
|
||
Test.User.registerAdapter('http', Test.adapter, { default: true }) | ||
Test.Post.registerAdapter('http', Test.adapter, { default: true }) | ||
console.log('Testing against js-data ' + Test.JSData.version.full) | ||
}) | ||
|
||
beforeEach(function () { | ||
var Test = this | ||
Test.adapter = new Test.HttpAdapter() | ||
Test.User.registerAdapter('http', Test.adapter, { default: true }) | ||
Test.Post.registerAdapter('http', Test.adapter, { default: true }) | ||
|
||
Test.p1 = { author: 'John', age: 30, id: 5 } | ||
Test.p2 = { author: 'Sally', age: 31, id: 6 } | ||
Test.p3 = { author: 'Mike', age: 32, id: 7 } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/* global fetch:true Headers:true Request:true */ | ||
/* global fetch:true Headers:true */ | ||
|
||
import {utils} from 'js-data' | ||
import axios from '../node_modules/axios/dist/axios' | ||
|
@@ -53,7 +53,7 @@ function buildUrl (url, params) { | |
} | ||
|
||
val.forEach(function (v) { | ||
if (window.toString.call(v) === '[object Date]') { | ||
if (toString.call(v) === '[object Date]') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
v = v.toISOString() | ||
} else if (utils.isObject(v)) { | ||
v = utils.toJson(v) | ||
|
@@ -558,20 +558,19 @@ Adapter.extend({ | |
* @param {Object} config.headers Headers for the request. | ||
* @param {Object} config.params Querystring for the request. | ||
* @param {string} config.url Url for the request. | ||
* @param {Object} [opts] Configuration options. | ||
*/ | ||
fetch (config, opts) { | ||
fetch (config) { | ||
const requestConfig = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pruned |
||
method: config.method, | ||
// turn the plain headers object into the Fetch Headers object | ||
headers: new Headers(config.headers) | ||
headers: new Headers(config.headers || {}) | ||
} | ||
|
||
if (config.data) { | ||
requestConfig.body = utils.toJson(config.data) | ||
} | ||
|
||
return fetch(new Request(buildUrl(config.url, config.params), requestConfig)) | ||
return fetch(buildUrl(config.url, config.params), requestConfig) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the docs argument signature for fetch and Request is equivalent https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch, so it shouldn't need to explicitly create a |
||
.then((response) => { | ||
response.config = { | ||
method: config.method, | ||
|
@@ -969,7 +968,7 @@ Adapter.extend({ | |
sum (mapper, field, query, opts) { | ||
query || (query = {}) | ||
opts || (opts = {}) | ||
if (!utils.utils.isString(field)) { | ||
if (!utils.isString(field)) { | ||
throw new Error('field must be a string!') | ||
} | ||
opts.params = this.getParams(opts) | ||
|
@@ -1064,7 +1063,7 @@ Adapter.extend({ | |
* | ||
* // GET /reports/schools/:school_id/teachers | ||
* addAction('getTeacherReports', { | ||
* basePath: 'reports/schools', | ||
* endpoint: 'reports/schools', | ||
* pathname: 'teachers', | ||
* method: 'GET' | ||
* })(store.getMapper('school')) | ||
|
@@ -1098,42 +1097,39 @@ export function addAction (name, opts) { | |
opts.response = opts.response || function (response) { return response } | ||
opts.responseError = opts.responseError || function (err) { return utils.reject(err) } | ||
mapper[name] = function (id, _opts) { | ||
_opts = _opts || {} | ||
if (utils.isObject(id)) { | ||
_opts = id | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we still need these 3 lines. To use your test as an example, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah, I was thinking it would just force the |
||
_opts = _opts || {} | ||
let adapter = this.getAdapter(opts.adapter || this.defaultAdapter || 'http') | ||
let config = {} | ||
utils.fillIn(config, opts) | ||
if (!_opts.hasOwnProperty('endpoint') && config.endpoint) { | ||
_opts.endpoint = config.endpoint | ||
} | ||
utils.fillIn(_opts, opts) | ||
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Javascript is pretty dynamic-y but it may be worth considering having a limited There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // at the top of index.js
const ACTION_OPTS = ['foo', 'bar'] // in addAction()
utils.fillIn(_opts, utils.pick(opts, ACTION_OPTS)) |
||
let adapter = this.getAdapter(_opts.adapter || this.defaultAdapter || 'http') | ||
const config = {} | ||
config.mapper = this.name | ||
utils.deepMixIn(config, _opts) | ||
config.method = config.method || 'GET' | ||
if (typeof _opts.getEndpoint === 'function') { | ||
config.url = _opts.getEndpoint(this, _opts) | ||
} else { | ||
let args = [ | ||
_opts.basePath || this.basePath || adapter.basePath, | ||
adapter.getEndpoint(this, utils.isSorN(id) ? id : null, _opts) | ||
adapter.getEndpoint(this, id, _opts) | ||
] | ||
if (utils.isSorN(id)) { | ||
args.push(id) | ||
} | ||
args.push(opts.pathname || name) | ||
config.url = makePath.apply(null, args) | ||
} | ||
config.method = config.method || 'GET' | ||
config.mapper = this.name | ||
utils.deepMixIn(config, _opts) | ||
return utils.resolve(config) | ||
.then(_opts.request || opts.request) | ||
.then(_opts.request) | ||
.then((config) => adapter.HTTP(config)) | ||
.then((data) => { | ||
if (data && data.config) { | ||
data.config.mapper = this.name | ||
} | ||
return data | ||
}) | ||
.then(_opts.response || opts.response, _opts.responseError || opts.responseError) | ||
.then(_opts.response, _opts.responseError) | ||
} | ||
return mapper | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
describe('sum', function () { | ||
it('should include count=true in query_params', function (done) { | ||
var Test = this | ||
|
||
setTimeout(function () { | ||
Test.requests[0].respond(200, { 'Content-Type': 'application/json' }, '{"count": 5}') | ||
}, 5) | ||
|
||
Test.adapter.count(Test.Post).then(function (result) { | ||
Test.assert.equal(Test.requests[0].url, 'api/posts?count=true') | ||
done() | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
describe('createMany', function () { | ||
it('should createMany') | ||
it('should createMany', function (done) { | ||
var Test = this | ||
|
||
setTimeout(function () { | ||
Test.requests[0].respond(200, { 'Content-Type': 'application/json' }, '') | ||
}, 5) | ||
|
||
var many = [{ author_id: 2, text: 'bar' }, { author_id: 2, text: 'foo' }] | ||
Test.Post.createMany(many).then(function (result) { | ||
Test.assert.equal(Test.requests[0].url, 'api/posts') | ||
Test.assert.equal(Test.requests[0].method, 'POST') | ||
Test.assert.equal(Test.requests[0].requestBody, JSON.stringify(many)) | ||
done() | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,36 @@ | ||
describe('fetch', function () { | ||
it('should fetch') | ||
it('should fetch from a URL', function (done) { | ||
var Test = this | ||
// Don't test fetch for Node | ||
try { | ||
fetch // eslint-disable-line | ||
} catch (e) { | ||
return this.skip() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Skipping the mocha test because the |
||
} | ||
if (Test.TEST_FETCH) { | ||
Test.xhr = Test.sinon.useFakeXMLHttpRequest() | ||
Test.requests = [] | ||
Test.xhr.onCreate = function (xhr) { | ||
Test.requests.push(xhr) | ||
} | ||
} | ||
|
||
setTimeout(function () { | ||
Test.requests[0].respond(200, { 'Content-Type': 'application/json' }, '{}') | ||
}, 300) | ||
|
||
Test.adapter.fetch({ | ||
method: 'get', | ||
params: { active: true }, | ||
url: '/api/foos' | ||
}).then(function (response) { | ||
var request = Test.requests[0] | ||
Test.assert.equal(request.method, 'GET') | ||
Test.assert.equal(request.url, '/api/foos?active=true') | ||
if (Test.TEST_FETCH) { | ||
Test.xhr.restore() | ||
} | ||
done() | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,57 @@ | ||
describe('static addAction', function () { | ||
it('should addAction') | ||
it('should addAction', function (done) { | ||
var Test = this | ||
var SchoolMapper = Test.store.defineMapper('school', {}) | ||
|
||
// GET async/reports/schools/:school_id/teachers | ||
Test.addAction('getTeacherReportsAsync', { | ||
basePath: 'async/', | ||
endpoint: 'reports/schools', | ||
pathname: 'teachers', | ||
method: 'GET' | ||
})(SchoolMapper) | ||
|
||
setTimeout(function () { | ||
Test.requests[0].respond(200, { 'Content-Type': 'text/plain' }, '') | ||
}, 5) | ||
|
||
SchoolMapper.getTeacherReportsAsync(1234).then(function (response) { | ||
Test.assert.equal(1, Test.requests.length) | ||
Test.assert.equal(Test.requests[0].url, 'async/reports/schools/1234/teachers', 'Add action configures basePath, endpoint and pathname') | ||
Test.assert.equal(Test.requests[0].method, 'GET') | ||
done() | ||
}) | ||
}) | ||
|
||
it('addAction action is callable with params instead of id', function (done) { | ||
var Test = this | ||
var adapter = Test.adapter | ||
var store = new Test.JSData.DataStore() | ||
store.registerAdapter('http', adapter, { default: true }) | ||
var SchoolMapper = store.defineMapper('school', {}) | ||
|
||
// GET async/reports/schools/teachers | ||
Test.addAction('getAllTeacherReportsAsync', { | ||
basePath: 'async/', | ||
endpoint: 'reports/schools', | ||
pathname: 'teachers', | ||
method: 'GET' | ||
})(SchoolMapper) | ||
|
||
setTimeout(function () { | ||
Test.requests[0].respond(200, { 'Content-Type': 'text/plain' }, '') | ||
}, 5) | ||
|
||
// GET async/reports/schools/teachers?<key>=<value> | ||
SchoolMapper.getAllTeacherReportsAsync({ | ||
params: { | ||
subject: 'esperanto' | ||
} | ||
}).then(function (response) { | ||
Test.assert.equal(1, Test.requests.length) | ||
Test.assert.equal(Test.requests[0].url, 'async/reports/schools/teachers?subject=esperanto', 'Add action configures basePath, endpoint, pathname, and querystring') | ||
Test.assert.equal(Test.requests[0].method, 'GET') | ||
done() | ||
}) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's another test: // GET async/reports/schools/teachers
Test.JSDataHttp.addAction('getAllTeacherReportsAsync', {
basePath: 'async/',
endpoint: 'reports/schools',
pathname: 'teachers',
method: 'GET'
})(SchoolMapper)
setTimeout(function () {
Test.requests[0].respond(200, { 'Content-Type': 'text/plain' }, '')
}, 5)
// GET async/reports/schools/teachers?foo=bar
SchoolMapper.getAllTeacherReportsAsync({
params: {
foo: 'bar'
}
}).then(function (response) {
Test.assert.equal(1, Test.requests.length)
Test.assert.equal(Test.requests[0].url, 'async/reports/schools/teachers?foo=bar', 'Add action configures basePath, endpoint, pathname, and querystring')
Test.assert.equal(Test.requests[0].method, 'GET')
done()
}) |
||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
o, that shouldn't have been removed.