Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist publisher info in the panel #1171

Merged
merged 1 commit into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -33,6 +33,9 @@ export const getMockChrome = () => {
onStartup: new ChromeEvent(),
onMessageExternal: new ChromeEvent(),
onConnectExternal: new ChromeEvent()
},
braveRewards: {
getPublisherData: (id: number, url: string, favicon: string) => undefined
}
}
}
Expand Down