diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index 1b78f80ebf7..c662e50796d 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -1325,13 +1325,11 @@ const checkPromotions = () => { }, random.randomInt({min: 20 * ledgerUtil.milliseconds.hour, max: 24 * ledgerUtil.milliseconds.hour})) } -const enable = (state, paymentsEnabled) => { - if (paymentsEnabled) { - if (!getSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED)) { - appActions.changeSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED, true) - } - } +const runPromotionCheck = () => { + appActions.runPromotionCheck() +} +const onRunPromotionCheck = (state, paymentsEnabled) => { if (paymentsEnabled === getSetting(settings.PAYMENTS_ENABLED)) { // on start if (togglePromotionTimeoutId) { @@ -1340,7 +1338,10 @@ const enable = (state, paymentsEnabled) => { togglePromotionTimeoutId = setTimeout(() => { checkPromotions() - }, random.randomInt({min: 10 * ledgerUtil.milliseconds.second, max: 15 * ledgerUtil.milliseconds.second})) + }, process.env.LEDGER_ENVIRONMENT === 'staging' + ? random.randomInt({min: 10 * ledgerUtil.milliseconds.second, max: 15 * ledgerUtil.milliseconds.second}) + : random.randomInt({min: 45 * ledgerUtil.milliseconds.second, max: 60 * ledgerUtil.milliseconds.second}) + ) } else if (paymentsEnabled) { // toggle on if (togglePromotionTimeoutId) { @@ -1358,6 +1359,15 @@ const enable = (state, paymentsEnabled) => { // toggle off state = ledgerState.setPromotionProp(state, 'promotionStatus', null) } + return state +} + +const enable = (state, paymentsEnabled) => { + if (paymentsEnabled) { + if (!getSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED)) { + appActions.changeSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED, true) + } + } if (synopsis) { return updatePublisherInfo(state, null, true) @@ -3343,7 +3353,9 @@ const getMethods = () => { getPaymentInfo, synopsisNormalizer, cacheRuleSet, - disablePayments + disablePayments, + runPromotionCheck, + onRunPromotionCheck } let privateMethods = {} diff --git a/app/browser/reducers/ledgerReducer.js b/app/browser/reducers/ledgerReducer.js index 58be91796db..013c8d3792f 100644 --- a/app/browser/reducers/ledgerReducer.js +++ b/app/browser/reducers/ledgerReducer.js @@ -12,6 +12,7 @@ const windowConstants = require('../../../js/constants/windowConstants') const settings = require('../../../js/constants/settings') const tabActionConstants = require('../../common/constants/tabAction') const ledgerStatuses = require('../../common/constants/ledgerStatuses') +const messages = require('../../../js/constants/messages') // State const ledgerState = require('../../common/state/ledgerState') @@ -576,8 +577,17 @@ const ledgerReducer = (state, action, immutableAction) => { state = ledgerApi.pageDataChanged(state, viewData, true) break } + case appConstants.APP_RUN_PROMOTION_CHECK: + { + state = ledgerApi.onRunPromotionCheck(state, getSetting(settings.PAYMENTS_ENABLED)) + break + } } return state } +process.on(messages.APP_INITIALIZED, () => { + ledgerApi.runPromotionCheck() +}) + module.exports = ledgerReducer diff --git a/js/actions/appActions.js b/js/actions/appActions.js index 06e4bda5d5e..c2886c250fc 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -2106,6 +2106,12 @@ const appActions = { tabId, index }) + }, + + runPromotionCheck: function () { + dispatch({ + actionType: appConstants.APP_RUN_PROMOTION_CHECK + }) } } diff --git a/js/constants/appConstants.js b/js/constants/appConstants.js index 98bb7b1ba8a..b33c8c5bcea 100644 --- a/js/constants/appConstants.js +++ b/js/constants/appConstants.js @@ -215,7 +215,8 @@ const appConstants = { APP_ON_TOR_INIT_PERCENTAGE: _, APP_SET_TOR_NEW_IDENTITY: _, APP_RESTART_TOR: _, - APP_RECREATE_TOR_TAB: _ + APP_RECREATE_TOR_TAB: _, + APP_RUN_PROMOTION_CHECK: _ } module.exports = mapValuesByKeys(appConstants) diff --git a/test/unit/app/browser/api/ledgerTest.js b/test/unit/app/browser/api/ledgerTest.js index 1060dd6f177..e8e7e3daddc 100644 --- a/test/unit/app/browser/api/ledgerTest.js +++ b/test/unit/app/browser/api/ledgerTest.js @@ -4161,4 +4161,20 @@ describe('ledger api unit tests', function () { assert(onLedgerFuzzingSpy.withArgs(1000, true).calledOnce) }) }) + + describe('runPromotionCheck', function () { + let onRunPromotionCheckSpy + + before(function () { + onRunPromotionCheckSpy = sinon.spy(appActions, 'runPromotionCheck') + }) + + afterEach(function () { + onRunPromotionCheckSpy.reset() + }) + it('calls runPromotionCheck', function () { + ledgerApi.runPromotionCheck() + assert(onRunPromotionCheckSpy.calledOnce) + }) + }) }) diff --git a/test/unit/app/browser/reducers/ledgerReducerTest.js b/test/unit/app/browser/reducers/ledgerReducerTest.js index 15836af8544..34483896151 100644 --- a/test/unit/app/browser/reducers/ledgerReducerTest.js +++ b/test/unit/app/browser/reducers/ledgerReducerTest.js @@ -67,7 +67,8 @@ describe('ledgerReducer unit tests', function () { clearPaymentHistory: () => {}, deleteWallet: () => {}, addNewLocation: dummyModifyState, - synopsisNormalizer: dummyModifyState + synopsisNormalizer: dummyModifyState, + onRunPromotionCheck: () => {} } fakeLedgerState = { resetPublishers: dummyModifyState, @@ -1125,4 +1126,24 @@ describe('ledgerReducer unit tests', function () { assert(deleteWalletSpy.calledOnce) }) }) + + describe('APP_RUN_PROMOTION_CHECK', function () { + let onRunPromotionCheckSpy + before(function () { + onRunPromotionCheckSpy = sinon.spy(fakeLedgerApi, 'onRunPromotionCheck') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_RUN_PROMOTION_CHECK, + stateResult: 'state-result-goes-here' + })) + }) + after(function () { + onRunPromotionCheckSpy.restore() + }) + it('calls ledgerApi.onRunPromotionCheck', function () { + assert(onRunPromotionCheckSpy.calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) })