diff --git a/geonode_mapstore_client/client/js/reducers/__tests__/gndownload-test.js b/geonode_mapstore_client/client/js/reducers/__tests__/gndownload-test.js index 34b34b2612..edaf76e388 100644 --- a/geonode_mapstore_client/client/js/reducers/__tests__/gndownload-test.js +++ b/geonode_mapstore_client/client/js/reducers/__tests__/gndownload-test.js @@ -34,4 +34,11 @@ describe('gndownload reducer', () => { } }); }); + it('downloadMetaDataComplete does not mutate the previous state', () => { + const previousLinkState = { 1: true }; + const state = { downloads: { ISO: previousLinkState, DublinCore: {} } }; + gndownload(state, downloadMetaDataComplete('ISO', 1)); + // a reducer must be pure: the previous state object must remain untouched + expect(previousLinkState).toEqual({ 1: true }); + }); }); diff --git a/geonode_mapstore_client/client/js/reducers/gndownload.js b/geonode_mapstore_client/client/js/reducers/gndownload.js index 5883b0ec66..462106c8a1 100644 --- a/geonode_mapstore_client/client/js/reducers/gndownload.js +++ b/geonode_mapstore_client/client/js/reducers/gndownload.js @@ -31,17 +31,14 @@ function gnDownload(state = defaultState, action) { }; } case DOWNLOAD_METADATA_COMPLETE: { - const newState = { ...state }; const linkType = action?.link?.split(' ').join(''); - const downloads = newState.downloads[linkType]; - delete downloads[action.pk]; + // omit the completed pk immutably so the previous state is not mutated + const { [action.pk]: removed, ...remaining } = state.downloads[linkType] || {}; return { - ...newState, + ...state, downloads: { - ...newState.downloads, - [linkType]: { - ...downloads - } + ...state.downloads, + [linkType]: remaining } }; }