Skip to content

Commit 86572ef

Browse files
committed
Fix addAction to pass through opts into _opts on functional call
* Adds tests for #action(<id>) and #action(<params>) queries
1 parent 9d8ca3a commit 86572ef

File tree

5 files changed

+72
-15
lines changed

5 files changed

+72
-15
lines changed

fetch/karma.start.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ before(function () {
1414
}
1515
Test.sinon = sinon
1616
Test.JSData = JSData
17+
Test.addAction = JSDataHttp.addAction
1718
Test.HttpAdapter = JSDataHttp.HttpAdapter
1819
Test.User = new JSData.Mapper({
1920
name: 'user'

karma.start.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ before(function () {
1414
}
1515
Test.sinon = sinon
1616
Test.JSData = JSData
17+
Test.addAction = JSDataHttp.addAction
1718
Test.HttpAdapter = JSDataHttp.HttpAdapter
1819
Test.User = new JSData.Mapper({
1920
name: 'user'

node/mocha.start.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ before(function () {
1818
}
1919
Test.sinon = require('sinon')
2020
Test.JSData = require('js-data')
21+
Test.addAction = require('./dist/js-data-http-node').addAction
2122
Test.HttpAdapter = require('./dist/js-data-http-node').HttpAdapter
2223
Test.User = new Test.JSData.Mapper({
2324
name: 'user'

src/index.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ Adapter.extend({
10641064
*
10651065
* // GET /reports/schools/:school_id/teachers
10661066
* addAction('getTeacherReports', {
1067-
* basePath: 'reports/schools',
1067+
* endpoint: 'reports/schools',
10681068
* pathname: 'teachers',
10691069
* method: 'GET'
10701070
* })(store.getMapper('school'))
@@ -1098,42 +1098,39 @@ export function addAction (name, opts) {
10981098
opts.response = opts.response || function (response) { return response }
10991099
opts.responseError = opts.responseError || function (err) { return utils.reject(err) }
11001100
mapper[name] = function (id, _opts) {
1101+
_opts = _opts || {}
11011102
if (utils.isObject(id)) {
11021103
_opts = id
11031104
}
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-
}
1105+
utils.fillIn(_opts, opts)
1106+
let adapter = this.getAdapter(_opts.adapter || this.defaultAdapter || 'http')
1107+
const config = {}
1108+
config.mapper = this.name
1109+
utils.deepMixIn(config, _opts)
1110+
config.method = config.method || 'GET'
11111111
if (typeof _opts.getEndpoint === 'function') {
11121112
config.url = _opts.getEndpoint(this, _opts)
11131113
} else {
11141114
let args = [
11151115
_opts.basePath || this.basePath || adapter.basePath,
1116-
adapter.getEndpoint(this, utils.isSorN(id) ? id : null, _opts)
1116+
adapter.getEndpoint(this, id, _opts)
11171117
]
11181118
if (utils.isSorN(id)) {
11191119
args.push(id)
11201120
}
11211121
args.push(opts.pathname || name)
11221122
config.url = makePath.apply(null, args)
11231123
}
1124-
config.method = config.method || 'GET'
1125-
config.mapper = this.name
1126-
utils.deepMixIn(config, _opts)
11271124
return utils.resolve(config)
1128-
.then(_opts.request || opts.request)
1125+
.then(_opts.request)
11291126
.then((config) => adapter.HTTP(config))
11301127
.then((data) => {
11311128
if (data && data.config) {
11321129
data.config.mapper = this.name
11331130
}
11341131
return data
11351132
})
1136-
.then(_opts.response || opts.response, _opts.responseError || opts.responseError)
1133+
.then(_opts.response, _opts.responseError)
11371134
}
11381135
return mapper
11391136
}

test/static.addAction.test.js

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

0 commit comments

Comments
 (0)