Skip to content

Commit

Permalink
Merging #1171
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Sadler committed Dec 22, 2018
1 parent 83cb307 commit 16a6566
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const getWindowId = (id: number) => {
return `id_${id}`
}

let currentPublishers: string[] = []

export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, action: any) => {
if (state === undefined) {
state = storage.load()
Expand Down Expand Up @@ -58,28 +60,34 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a
}
break
case types.ON_TAB_RETRIEVED:
const tab: chrome.tabs.Tab = payload.tab
if (
!tab ||
!tab.url ||
tab.incognito ||
!tab.active ||
!state.walletCreated
) {
break
}
{
const tab: chrome.tabs.Tab = payload.tab
if (
!tab ||
!tab.url ||
tab.incognito ||
!tab.active ||
!state.walletCreated
) {
break
}

chrome.braveRewards.getPublisherData(tab.windowId, tab.url, tab.favIconUrl || '')
const id = getWindowId(tab.windowId)
let publishers: Record<string, RewardsExtension.Publisher> = state.publishers
if (publishers[id]) {
delete publishers[id]
}
state = {
...state,
publishers
chrome.braveRewards.getPublisherData(tab.windowId, tab.url, tab.favIconUrl || '')
const id = getWindowId(tab.windowId)
let publishers: Record<string, RewardsExtension.Publisher> = state.publishers

if (publishers[id] && currentPublishers[id] !== tab.url) {
delete publishers[id]
}

currentPublishers[id] = tab.url

state = {
...state,
publishers
}
break
}
break
case types.ON_PUBLISHER_DATA:
{
const publisher = payload.publisher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { debounce } from '../../../../../common/debounce'

const keyName = 'rewards-panel-data'

const defaultState: RewardsExtension.State = {
export const defaultState: RewardsExtension.State = {
walletCreated: false,
walletCreateFailed: false,
publishers: {},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* global chrome */

import reducers from '../../../../../../brave_rewards/resources/extension/brave_rewards/background/reducers'
import { types } from '../../../../../../brave_rewards/resources/extension/brave_rewards/constants/rewards_panel_types'
import { defaultState } from '../../../../../../brave_rewards/resources/extension/brave_rewards/background/storage'

describe('rewards panel reducer', () => {
const constantDate = new Date('2018-01-01T12:00:00')

beforeAll(() => {
(global as any).Date = class extends Date {
constructor () {
super()
return constantDate
}
}
})

describe('ON_TAB_RETRIEVED', () => {
describe('persist publisher info', () => {
it.skip('url is the same', () => {
const initState: Rewards.State = { ...defaultState, walletCreated: true }
const expectedState1: Rewards.State = { ...defaultState, walletCreated: true }
const expectedState2: Rewards.State = {
...defaultState,
walletCreated: true,
publishers: { 1: 'clifton.io' }
}

const payload = {
tab: {
url: 'https://clifton.io',
incognito: false,
active: true,
windowId: 1
}
}

// first visit
let state = reducers({ rewardsPanelData: initState }, {
type: types.ON_TAB_RETRIEVED,
payload
})

expect(state.rewardsPanelData).toEqual(expectedState1)

// imitates ON_PUBLISHER_DATA
state.rewardsPanelData.publishers = { id_1: 'clifton.io' }

// second visit
state = reducers(state, {
type: types.ON_TAB_RETRIEVED,
payload
})

expect(state.rewardsPanelData).toEqual(expectedState2)
})

it('url is not the same', () => {
const initState: Rewards.State = { ...defaultState, walletCreated: true }
const expectedState: Rewards.State = { ...defaultState, walletCreated: true }

// first visit
let state = reducers({ rewardsPanelData: initState }, {
type: types.ON_TAB_RETRIEVED,
payload: {
tab: {
url: 'https://clifton.io',
incognito: false,
active: true,
windowId: 1
}
}
})

expect(state.rewardsPanelData).toEqual(expectedState)

// imitates ON_PUBLISHER_DATA
state.rewardsPanelData.publishers = { id_1: 'clifton.io' }

// second visit
state = reducers(state, {
type: types.ON_TAB_RETRIEVED,
payload: {
tab: {
url: 'https://brave.com',
incognito: false,
active: true,
windowId: 1
}
}
})

expect(state.rewardsPanelData).toEqual(expectedState)
})
})
})
})
3 changes: 3 additions & 0 deletions components/test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export const getMockChrome = () => {
onMessageExternal: new ChromeEvent(),
onConnectExternal: new ChromeEvent()
},
braveRewards: {
getPublisherData: (id: number, url: string, favicon: string) => undefined
}
}
}

Expand Down

0 comments on commit 16a6566

Please sign in to comment.