Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Delaying promoCode read by one minute
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanml committed Jul 6, 2018
1 parent c42cf87 commit 32adb33
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
41 changes: 29 additions & 12 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ let runTimeoutId
let promotionTimeoutId
let togglePromotionTimeoutId
let verifiedTimeoutId = false
let promoCodeReadTimeoutId = false

// Database
let v2RulesetDB
Expand Down Expand Up @@ -2343,17 +2344,7 @@ const initialize = (state, paymentsEnabled) => {
}

if (updateState.getUpdateProp(state, 'referralDownloadId') == null) {
promoCodeFirstRunStorage
.readFirstRunPromoCode()
.then((code) => {
onReferralCodeRead(code)
})
.catch(error => {
if (clientOptions.verboseP) {
console.error('read error: ' + error.toString())
}
fetchReferralHeaders()
})
schedulePromoCodeRead()
} else {
fetchReferralHeaders()
}
Expand Down Expand Up @@ -2419,6 +2410,30 @@ const initialize = (state, paymentsEnabled) => {
}
}

const schedulePromoCodeRead = () => {
if (promoCodeReadTimeoutId) {
clearTimeout(promoCodeReadTimeoutId)
}

promoCodeReadTimeoutId = setTimeout(() => {
module.exports.firstRunPromoCode()
}, ledgerUtil.milliseconds.minute)
}

const firstRunPromoCode = () => {
promoCodeFirstRunStorage
.readFirstRunPromoCode()
.then((code) => {
onReferralCodeRead(code)
})
.catch(error => {
if (clientOptions.verboseP) {
console.error('read error: ' + error.toString())
}
fetchReferralHeaders()
})
}

const getContributionAmount = (state) => {
return ledgerState.getContributionAmount(state)
}
Expand Down Expand Up @@ -3355,7 +3370,9 @@ const getMethods = () => {
cacheRuleSet,
disablePayments,
runPromotionCheck,
onRunPromotionCheck
onRunPromotionCheck,
firstRunPromoCode,
schedulePromoCodeRead
}

let privateMethods = {}
Expand Down
34 changes: 34 additions & 0 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2909,6 +2909,40 @@ describe('ledger api unit tests', function () {
})
})

describe('schedulePromoCodeRead', function () {
const minute = 60000
let fakeClock, firstRunPromoCodeSpy, onReferralCodeReadSpy

before(function () {
fakeClock = sinon.useFakeTimers()
firstRunPromoCodeSpy = sinon.stub(ledgerApi, 'firstRunPromoCode')
onReferralCodeReadSpy = sinon.stub(ledgerApi, 'onReferralCodeRead')
})

after(function () {
fakeClock.restore()
firstRunPromoCodeSpy.restore()
onReferralCodeReadSpy.restore()
})

afterEach(function () {
firstRunPromoCodeSpy.reset()
onReferralCodeReadSpy.reset()
})

it('waits for a minute to pass before calling firstRunPromoCode', function () {
ledgerApi.schedulePromoCodeRead()
fakeClock.tick(minute)
assert(firstRunPromoCodeSpy.calledOnce)
})

it('referral code read is not called before firstRunPromoCode has completed', function () {
ledgerApi.schedulePromoCodeRead()
fakeClock.tick(59999)
assert.equal(false, onReferralCodeReadSpy.calledOnce)
})
})

describe('checkReferralActivity', function () {
let checkForUpdateSpy, roundtripSpy, fakeClock

Expand Down

0 comments on commit 32adb33

Please sign in to comment.