diff --git a/__tests__/src/reducers/annotations.test.js b/__tests__/src/reducers/annotations.test.js index 762a43d79833aae4a01992c96f6bfb58e3bdf29f..7a76ee279efa8875474a68070ec47fe067b38571 100644 --- a/__tests__/src/reducers/annotations.test.js +++ b/__tests__/src/reducers/annotations.test.js @@ -46,6 +46,78 @@ describe('annotation reducer', () => { }, }); }); + it('should be able to RECEIVE_ANNOTATION from multiple sources and merge state', () => { + const firstReduction = annotationsReducer( + { + foo: { + abc123: { + id: 'abc123', + isFetching: true, + }, + }, + }, + { + annotationId: 'efg456', + annotationJson: { + '@type': 'sc:AnnotationList', + content: 'anno stuff', + id: 'efg456', + }, + targetId: 'foo', + type: ActionTypes.RECEIVE_ANNOTATION, + }, + ); + expect(firstReduction).toMatchObject({ + foo: { + abc123: { + id: 'abc123', + isFetching: true, + }, + efg456: { + isFetching: false, + json: { + '@type': 'sc:AnnotationList', + content: 'anno stuff', + id: 'efg456', + }, + }, + }, + }); + const secondReduction = annotationsReducer( + firstReduction, + { + annotationId: 'abc123', + annotationJson: { + '@type': 'sc:AnnotationList', + content: 'anno stuff', + id: 'abc123', + }, + targetId: 'foo', + type: ActionTypes.RECEIVE_ANNOTATION, + }, + ); + expect(secondReduction).toMatchObject({ + foo: { + abc123: { + id: 'abc123', + isFetching: false, + json: { + '@type': 'sc:AnnotationList', + content: 'anno stuff', + id: 'abc123', + }, + }, + efg456: { + isFetching: false, + json: { + '@type': 'sc:AnnotationList', + content: 'anno stuff', + id: 'efg456', + }, + }, + }, + }); + }); it('should handle RECEIVE_ANNOTATION_FAILURE', () => { expect(annotationsReducer( { diff --git a/src/state/reducers/annotations.js b/src/state/reducers/annotations.js index aabbfc88c5d7a0064b44dc9e8c332d8f9bedecb5..57fa265bdc92808715bdf0012f9202b1b45203f6 100644 --- a/src/state/reducers/annotations.js +++ b/src/state/reducers/annotations.js @@ -9,6 +9,7 @@ export const annotationsReducer = (state = {}, action) => { return { ...state, [action.targetId]: { + ...state[action.targetId], [action.annotationId]: { id: action.annotationId, isFetching: true, @@ -19,6 +20,7 @@ export const annotationsReducer = (state = {}, action) => { return { ...state, [action.targetId]: { + ...state[action.targetId], [action.annotationId]: { id: action.annotationId, isFetching: false, @@ -30,6 +32,7 @@ export const annotationsReducer = (state = {}, action) => { return { ...state, [action.targetId]: { + ...state[action.targetId], [action.annotationId]: { error: action.error, id: action.annotationId,