Skip to content

Commit

Permalink
Fixes brave/brave-browser#4224, Ensures that adsData is written to re…
Browse files Browse the repository at this point in the history
…gardless of default storage
  • Loading branch information
ryanml committed Apr 24, 2019
1 parent 4096639 commit c0590fb
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
11 changes: 11 additions & 0 deletions components/brave_rewards/resources/ui/reducers/rewards_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Reducer } from 'redux'

// Constant
import { types } from '../constants/rewards_types'
import { defaultState } from '../storage'

const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State, action) => {
switch (action.type) {
Expand Down Expand Up @@ -118,6 +119,11 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
}

state = { ...state }

if (!state.adsData) {
state.adsData = defaultState.adsData
}

state.adsData.adsEnabled = action.payload.adsData.adsEnabled
state.adsData.adsPerHour = action.payload.adsData.adsPerHour
state.adsData.adsUIEnabled = action.payload.adsData.adsUIEnabled
Expand Down Expand Up @@ -150,6 +156,11 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
}

state = { ...state }

if (!state.adsData) {
state.adsData = defaultState.adsData
}

const data = action.payload.data
state.adsData.adsNotificationsReceived = data.adsNotificationsReceived
state.adsData.adsEstimatedEarnings = data.adsEstimatedEarnings
Expand Down
75 changes: 75 additions & 0 deletions components/test/brave_rewards/ui/reducers/rewards_reducer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,79 @@ describe('rewards reducer', () => {
})
})
})

describe('ON_ADS_DATA', () => {
describe('updates ads data', () => {
it('updates existing properties', () => {
const initState: Rewards.State = { ...defaultState }
initState.adsData = {
adsEnabled: false,
adsPerHour: 2,
adsUIEnabled: false,
adsNotificationsReceived: 0,
adsEstimatedEarnings: 0,
adsIsSupported: false
}

const expectedState: Rewards.State = { ...defaultState }
expectedState.adsData = {
adsEnabled: true,
adsPerHour: 5,
adsUIEnabled: true,
adsNotificationsReceived: 0,
adsEstimatedEarnings: 0,
adsIsSupported: true
}

const assertion = reducers({
rewardsData: initState
}, {
type: types.ON_ADS_DATA,
payload: {
adsData: {
adsEnabled: true,
adsPerHour: 5,
adsUIEnabled: true,
adsIsSupported: true
}
}
})
expect(assertion).toEqual({
rewardsData: expectedState
})
})

it('updates properties when state member doesn\'t exist', () => {
const initState: Rewards.State = { ...defaultState }
delete initState.adsData

const expectedState: Rewards.State = { ...defaultState }
expectedState.adsData = {
adsEnabled: false,
adsPerHour: 2,
adsUIEnabled: true,
adsNotificationsReceived: 0,
adsEstimatedEarnings: 0,
adsIsSupported: true
}

const assertion = reducers({
rewardsData: initState
}, {
type: types.ON_ADS_DATA,
payload: {
adsData: {
adsEnabled: false,
adsPerHour: 2,
adsUIEnabled: true,
adsIsSupported: true
}
}
})
expect(assertion).toEqual({
rewardsData: expectedState
})
})
})
})
})

0 comments on commit c0590fb

Please sign in to comment.