diff --git a/app/browser/reducers/downloadsReducer.js b/app/browser/reducers/downloadsReducer.js index 246663f1b41..5a2c01230bc 100644 --- a/app/browser/reducers/downloadsReducer.js +++ b/app/browser/reducers/downloadsReducer.js @@ -14,6 +14,7 @@ const {cancelDownload, pauseDownload, resumeDownload} = require('../electronDown const {CANCEL, PAUSE, RESUME} = require('../../common/constants/electronDownloadItemActions') const appActions = require('../../../js/actions/appActions') const userPrefs = require('../../../js/state/userPrefs') +const getSetting = require('../../../js/settings').getSetting const downloadsReducer = (state, action) => { const download = action.downloadId ? state.getIn(['downloads', action.downloadId]) : undefined @@ -21,10 +22,17 @@ const downloadsReducer = (state, action) => { ![appConstants.APP_MERGE_DOWNLOAD_DETAIL, appConstants.APP_CLEAR_COMPLETED_DOWNLOADS, appConstants.APP_SELECT_DEFAULT_DOWNLOAD_PATH, - appConstants.APP_CHANGE_SETTING].includes(action.actionType)) { + appConstants.APP_CHANGE_SETTING, + appConstants.APP_SET_STATE].includes(action.actionType)) { return state } switch (action.actionType) { + case appConstants.APP_SET_STATE: + if (getSetting(settings.DOWNLOAD_DEFAULT_PATH) === '') { + const defaultPath = app.getPath('downloads') + appActions.changeSetting(settings.DOWNLOAD_DEFAULT_PATH, defaultPath) + } + break case appConstants.APP_DOWNLOAD_REVEALED: fs.access(download.get('savePath'), fs.constants.F_OK, (err) => { if (err) { diff --git a/test/unit/app/browser/reducers/downloadsReducerTest.js b/test/unit/app/browser/reducers/downloadsReducerTest.js index 0baffce3ad9..ae9f38371f5 100644 --- a/test/unit/app/browser/reducers/downloadsReducerTest.js +++ b/test/unit/app/browser/reducers/downloadsReducerTest.js @@ -6,10 +6,12 @@ const process = require('process') const assert = require('assert') const uuid = require('uuid') const path = require('path') +const appActions = require('../../../../../js/actions/appActions') const fakeElectron = require('../../../lib/fakeElectron') const appConstants = require('../../../../../js/constants/appConstants') const {PENDING, IN_PROGRESS, RESUMING, PAUSED, COMPLETED, CANCELLED, INTERRUPTED} = require('../../../../../js/constants/downloadStates') +const settings = require('../../../../../js/constants/settings') const {CANCEL} = require('../../../../../app/common/constants/electronDownloadItemActions') require('../../../braveUnit') @@ -32,17 +34,35 @@ const oneDownloadWithState = (state) => Immutable.fromJS({ describe('downloadsReducer', function () { let downloadsReducer + let fakeSettings + let downloadDefaultPath + let changeSettingSpy before(function () { mockery.enable({ warnOnReplace: false, warnOnUnregistered: false, useCleanCache: true }) + + fakeSettings = { + getSetting: (settingKey, settingsCollection) => { + switch (settingKey) { + case settings.DOWNLOAD_DEFAULT_PATH: + return downloadDefaultPath + } + } + } + + changeSettingSpy = sinon.spy(appActions, 'changeSetting') + mockery.registerMock('electron', fakeElectron) + mockery.registerMock('../../../js/settings', fakeSettings) + mockery.registerMock('../../../js/actions/appActions', appActions) downloadsReducer = require('../../../../../app/browser/reducers/downloadsReducer') }) after(function () { + changeSettingSpy.restore() mockery.disable() }) @@ -257,4 +277,19 @@ describe('downloadsReducer', function () { }) }) }) + + describe('APP_SET_STATE', function () { + it('DOWNLOAD_DEFAULT_PATH is empty', function () { + changeSettingSpy.reset() + downloadDefaultPath = '' + downloadsReducer({}, {actionType: appConstants.APP_SET_STATE}) + assert(changeSettingSpy.withArgs(settings.DOWNLOAD_DEFAULT_PATH, `${process.cwd()}/downloads`).calledOnce) + }) + it('DOWNLOAD_DEFAULT_PATH is not empty', function () { + changeSettingSpy.reset() + downloadDefaultPath = '123' + downloadsReducer({}, {actionType: appConstants.APP_SET_STATE}) + assert(changeSettingSpy.notCalled) + }) + }) })