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

Commit

Permalink
Merge pull request #11722 from bsclifton/contribution-amount-migration
Browse files Browse the repository at this point in the history
Migrates the contribution amounts from old USD setting to new BAT setting
  • Loading branch information
NejcZdovc committed Oct 31, 2017
1 parent 50c2b8d commit a42cac1
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 16 deletions.
23 changes: 21 additions & 2 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -1867,7 +1867,7 @@ const onCallback = (state, result, delayTime) => {

if (client && result.getIn(['properties', 'wallet'])) {
if (!ledgerState.getInfoProp(state, 'created')) {
setPaymentInfo(getSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT))
module.exports.setPaymentInfo(getSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT))
}

state = getStateInfo(state, regularResults) // TODO optimize if possible
Expand Down Expand Up @@ -2014,6 +2014,24 @@ const initialize = (state, paymentsEnabled) => {
}
}

const getContributionAmount = () => {
const amount = getSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT)
// if amount is 5, 10, 15, or 20... the amount wasn't updated when changing
// from BTC to BAT (see https://github.com/brave/browser-laptop/issues/11719)
let updatedAmount
switch (amount) {
case 5: updatedAmount = 25; break
case 10: updatedAmount = 50; break
case 15: updatedAmount = 75; break
case 20: updatedAmount = 100; break
}
if (updatedAmount) {
appActions.changeSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT, updatedAmount)
return updatedAmount
}
return amount
}

const onInitRead = (state, parsedData) => {
state = getStateInfo(state, parsedData)

Expand Down Expand Up @@ -2071,7 +2089,8 @@ const onInitRead = (state, parsedData) => {
state = ledgerState.setInfoProp(state, 'address', client.getWalletAddress())
}

setPaymentInfo(getSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT))
const contributionAmount = getContributionAmount()
module.exports.setPaymentInfo(contributionAmount)
getBalance(state)

// Show relevant browser notifications on launch
Expand Down
128 changes: 114 additions & 14 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ describe('ledger api unit tests', function () {
let paymentsNotifications
let isBusy = false
let ledgerClient
let contributionAmount = 25

// spies
let ledgerTransitionSpy
let ledgerTransitionedSpy
let onBitcoinToBatTransitionedSpy
let onLedgerCallbackSpy
let onBitcoinToBatBeginTransitionSpy
let onChangeSettingSpy

before(function () {
this.clock = sinon.useFakeTimers()
Expand All @@ -44,11 +46,13 @@ describe('ledger api unit tests', function () {
mockery.registerMock('ad-block', fakeAdBlock)
mockery.registerMock('../../../js/settings', {
getSetting: (settingKey, settingsCollection, value) => {
if (settingKey === settings.PAYMENTS_ENABLED) {
return paymentsEnabled
}
if (settingKey === settings.PAYMENTS_NOTIFICATIONS) {
return paymentsNotifications
switch (settingKey) {
case settings.PAYMENTS_ENABLED:
return paymentsEnabled
case settings.PAYMENTS_NOTIFICATIONS:
return paymentsNotifications
case settings.PAYMENTS_CONTRIBUTION_AMOUNT:
return contributionAmount
}
return false
}
Expand All @@ -57,6 +61,7 @@ describe('ledger api unit tests', function () {
onBitcoinToBatTransitionedSpy = sinon.spy(appActions, 'onBitcoinToBatTransitioned')
onLedgerCallbackSpy = sinon.spy(appActions, 'onLedgerCallback')
onBitcoinToBatBeginTransitionSpy = sinon.spy(appActions, 'onBitcoinToBatBeginTransition')
onChangeSettingSpy = sinon.spy(appActions, 'changeSetting')

// ledger client stubbing
ledgerClient = sinon.stub()
Expand All @@ -70,6 +75,15 @@ describe('ledger api unit tests', function () {
}
}
},
getWalletAddresses: function () {
return {
'BAT': '0xADDRESS_HERE',
'BTC': 'ADDRESS_HERE',
'CARD_ID': 'ADDRESS-GUID-GOES-IN-HERE',
'ETH': '0xADDRESS_HERE',
'LTC': 'ADDRESS_HERE'
}
},
getWalletProperties: function (amount, currency, callback) {
callback(null, {})
},
Expand All @@ -87,6 +101,13 @@ describe('ledger api unit tests', function () {
transitioned: function () {
return {}
},
setBraveryProperties: function (clientProperties, callback) {
if (typeof callback === 'function') {
const err = undefined
const result = {}
callback(err, result)
}
},
state: {
transactions: []
},
Expand All @@ -106,6 +127,9 @@ describe('ledger api unit tests', function () {
})
after(function () {
onBitcoinToBatTransitionedSpy.restore()
onLedgerCallbackSpy.restore()
onBitcoinToBatBeginTransitionSpy.restore()
onChangeSettingSpy.restore()
this.clock.restore()
mockery.deregisterAll()
mockery.disable()
Expand All @@ -126,25 +150,101 @@ describe('ledger api unit tests', function () {
})

describe('onInitRead', function () {
let parsedLedgerData
let onLaunchSpy
beforeEach(function () {
onLaunchSpy = sinon.spy(ledgerApi.notifications, 'onLaunch')
})
afterEach(function () {
onLaunchSpy.restore()
})
it('calls notifications.onLaunch', function () {
ledgerApi.onInitRead(defaultAppState, {
let setPaymentInfoSpy
before(function () {
parsedLedgerData = {
paymentInfo: {
},
properties: {
wallet: {
paymentId: 12345
}
}
})
}
contributionAmount = 25
})
before(function () {
onLaunchSpy = sinon.spy(ledgerApi.notifications, 'onLaunch')
setPaymentInfoSpy = sinon.spy(ledgerApi, 'setPaymentInfo')
})
after(function () {
onLaunchSpy.restore()
setPaymentInfoSpy.restore()
})
it('calls notifications.onLaunch', function () {
onLaunchSpy.reset()
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
assert(onLaunchSpy.calledOnce)
})
it('calls setPaymentInfo with contribution amount', function () {
setPaymentInfoSpy.reset()
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
assert(setPaymentInfoSpy.withArgs(25).calledOnce)
})

describe('when contribution amount is still set to the USD amount (before BAT Mercury)', function () {
after(function () {
contributionAmount = 25
})
describe('when set to 5 USD', function () {
before(function () {
setPaymentInfoSpy.reset()
onChangeSettingSpy.reset()
contributionAmount = 5
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
})
it('converts to 25 BAT', function () {
assert(setPaymentInfoSpy.withArgs(25).calledOnce)
})
it('updates the setting', function () {
assert(onChangeSettingSpy.withArgs(settings.PAYMENTS_CONTRIBUTION_AMOUNT, 25).calledOnce)
})
})
describe('when set to 10 USD', function () {
before(function () {
setPaymentInfoSpy.reset()
onChangeSettingSpy.reset()
contributionAmount = 10
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
})
it('converts to 50 BAT', function () {
assert(setPaymentInfoSpy.withArgs(50).calledOnce)
})
it('updates the setting', function () {
assert(onChangeSettingSpy.withArgs(settings.PAYMENTS_CONTRIBUTION_AMOUNT, 50).calledOnce)
})
})
describe('when set to 15 USD', function () {
before(function () {
setPaymentInfoSpy.reset()
onChangeSettingSpy.reset()
contributionAmount = 15
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
})
it('converts to 75 BAT', function () {
assert(setPaymentInfoSpy.withArgs(75).calledOnce)
})
it('updates the setting', function () {
assert(onChangeSettingSpy.withArgs(settings.PAYMENTS_CONTRIBUTION_AMOUNT, 75).calledOnce)
})
})
describe('when set to 20 USD', function () {
before(function () {
setPaymentInfoSpy.reset()
onChangeSettingSpy.reset()
contributionAmount = 20
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
})
it('converts to 100 BAT', function () {
assert(setPaymentInfoSpy.withArgs(100).calledOnce)
})
it('updates the setting', function () {
assert(onChangeSettingSpy.withArgs(settings.PAYMENTS_CONTRIBUTION_AMOUNT, 100).calledOnce)
})
})
})
})

describe('transitionWalletToBat', function () {
Expand Down

0 comments on commit a42cac1

Please sign in to comment.