Skip to content

Commit

Permalink
* Don't try to merge objects if there is no objects in response.
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Jan 4, 2018
1 parent 2141b8d commit 520ed41
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.6.2](https://github.com/Kozea/redux-api-unrest/compare/v0.6.1...v0.6.2)

* Don't try to merge objects if there is no objects in response.

## [0.6.1](https://github.com/Kozea/redux-api-unrest/compare/v0.6.0...v0.6.1)

* Dependencies update
Expand Down
18 changes: 10 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,15 @@ export default class ApiUnrest {
case this.events[endpoint].success:
return {
...state,
objects: this.mergeObjects(
action.method,
state.objects,
action.objects,
action.metadata.primary_keys,
action.batch
),
objects: action.objects
? this.mergeObjects(
action.method,
state.objects,
action.objects,
action.metadata.primary_keys,
action.batch
)
: state.objects,
metadata: action.metadata,
loading: false,
error: null,
Expand Down Expand Up @@ -353,7 +355,7 @@ export default class ApiUnrest {
}
throw httpError(response.status, json.message || json.description, json)
}
return response.json()
return { ...(await response.json()), code: response.status }
}

onBeforeFetchHook({ url, opts }) {
Expand Down
33 changes: 33 additions & 0 deletions test/reducers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,37 @@ describe('Api unrest reducers', () => {
expect(store.getState().fruit.metadata.occurences).toEqual(1)
expect(store.getState().fruit.metadata.primary_keys).toEqual(['id'])
})

it('does nothing on missing objects', () => {
// When there's is a 202 success without objects for example
const api = new ApiUnrest({
fruit: 'fruit',
color: 'base/color/:id?',
tree: 'forest/tree/:type?/:age?',
})
const reducer = combineReducers(api.reducers)
const store = createStore(reducer, applyMiddleware(thunk))

store.dispatch({
type: api.events.color.success,
metadata: { something: true },
method: 'POST',
parameters: {},
batch: true,
})
expect(Object.keys(store.getState().color)).toEqual([
'objects',
'metadata',
'loading',
'error',
'lastFetch',
'lastFetchParameters',
])
expect(store.getState().color.objects).toEqual([])
expect(store.getState().color.error).toBeNull()
expect(store.getState().color.lastFetch).toBeNull()
expect(store.getState().color.lastFetchParameters).toBeNull()
expect(store.getState().color.loading).toEqual(false)
expect(store.getState().color.metadata.something).toBeTruthy()
})
})
1 change: 1 addition & 0 deletions test/state.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('Api unrest update the state when fetching', () => {
expect(store.getState().color.loading).toBeFalsy()
expect(store.getState().color.objects).toEqual(['response'])
expect(store.getState().color.error).toBeNull()
expect(store.getState().color.metadata.code).toEqual(200)
})
it('sets and remove loading flag during error', async () => {
const api = new ApiUnrest(
Expand Down

0 comments on commit 520ed41

Please sign in to comment.