Skip to content

Commit 18b95e3

Browse files
committed
Fix add-action opts bug + test
1 parent 9d8ca3a commit 18b95e3

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/index.js

Lines changed: 10 additions & 16 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+
* endppoint: 'reports/schools',
10681068
* pathname: 'teachers',
10691069
* method: 'GET'
10701070
* })(store.getMapper('school'))
@@ -1098,42 +1098,36 @@ 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-
if (utils.isObject(id)) {
1102-
_opts = id
1103-
}
11041101
_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-
}
1102+
utils.fillIn(_opts, opts)
1103+
let adapter = this.getAdapter(_opts.adapter || this.defaultAdapter || 'http')
1104+
const config = {}
1105+
config.mapper = this.name
1106+
utils.deepMixIn(config, _opts)
1107+
config.method = config.method || 'GET'
11111108
if (typeof _opts.getEndpoint === 'function') {
11121109
config.url = _opts.getEndpoint(this, _opts)
11131110
} else {
11141111
let args = [
11151112
_opts.basePath || this.basePath || adapter.basePath,
1116-
adapter.getEndpoint(this, utils.isSorN(id) ? id : null, _opts)
1113+
adapter.getEndpoint(this, id, _opts)
11171114
]
11181115
if (utils.isSorN(id)) {
11191116
args.push(id)
11201117
}
11211118
args.push(opts.pathname || name)
11221119
config.url = makePath.apply(null, args)
11231120
}
1124-
config.method = config.method || 'GET'
1125-
config.mapper = this.name
1126-
utils.deepMixIn(config, _opts)
11271121
return utils.resolve(config)
1128-
.then(_opts.request || opts.request)
1122+
.then(_opts.request)
11291123
.then((config) => adapter.HTTP(config))
11301124
.then((data) => {
11311125
if (data && data.config) {
11321126
data.config.mapper = this.name
11331127
}
11341128
return data
11351129
})
1136-
.then(_opts.response || opts.response, _opts.responseError || opts.responseError)
1130+
.then(_opts.response, _opts.responseError)
11371131
}
11381132
return mapper
11391133
}

test/static.addAction.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
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.JSDataHttp.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+
})
328
})

0 commit comments

Comments
 (0)