Skip to content

Commit

Permalink
Process included records (fixes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrichar1 committed Nov 12, 2018
1 parent 4bae78f commit ee79354
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/jsonapi-vuex.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ const actions = (api, conf = {}) => {
config['data'] = config['data'] || {}
return api.get(path, config)
.then((results) => {
// Process included records
if ('included' in results.data) {
const included_data = jsonapiToNorm(results.data.included)
context.commit('add_records', included_data)
}
const res_data = jsonapiToNorm(results.data.data)
context.commit('add_records', res_data)
return res_data
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/jsonapi-vuex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,19 @@ describe("jsonapi-vuex tests", () => {
done()
})
})
it("should add included record(s) to the store", (done) => {
const data = {
data: json_item1,
included: [ json_item2 ]
}
mock_api.onAny().reply(200, data)
jm.actions.get(stub_context, norm_item1, { params: { included: "widgets" }})
.then(() => {
// included is always an array, so add_records data will have id index
expect(stub_context.commit).to.have.been.calledWith("add_records", { 2: norm_item2 })
done()
})
})
it("should handle API errors", (done) => {
mock_api.onAny().reply(500)
jm.actions.get(stub_context, norm_item1)
Expand Down

0 comments on commit ee79354

Please sign in to comment.