Skip to content

Commit

Permalink
Merge pull request #3149 from brave/ca-5609
Browse files Browse the repository at this point in the history
[brave-extension] improve tests on shields
  • Loading branch information
bsclifton authored Aug 20, 2019
2 parents 05c7b61 + 8a66468 commit b3b7461
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 1,387 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const requestShieldPanelData = (tabId: number) =>
.then((details: ShieldDetails) => {
actions.shieldsPanelDataUpdated(details)
})
.catch((error: any) => console.error('[Shields]: Can\'t request shields panel data.', error))

/**
* Changes the brave shields setting at origin to be allowed or blocked.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
* 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/. */

// Types
import * as types from '../../../brave_extension/extension/brave_extension/constants/cosmeticFilterTypes'

// Actions
import * as actions from '../../../brave_extension/extension/brave_extension/actions/cosmeticFilterActions'

describe('cosmeticFilterActions', () => {
Expand Down
23 changes: 12 additions & 11 deletions components/test/brave_extension/actions/shieldsPanelActions_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ import {
// Actions
import * as actions from '../../../brave_extension/extension/brave_extension/actions/shieldsPanelActions'

const details: ShieldDetails = {
ads: 'allow',
trackers: 'block',
httpUpgradableResources: 'allow',
origin: 'https://www.brave.com',
hostname: 'www.brave.com',
id: 1,
javascript: 'allow',
fingerprinting: 'allow',
cookies: 'allow'
}

describe('shieldsPanelActions', () => {
it('shieldsPanelDataUpdated', () => {
const details: ShieldDetails = {
ads: 'allow',
trackers: 'block',
httpUpgradableResources: 'allow',
origin: 'https://www.brave.com',
hostname: 'www.brave.com',
id: 1,
javascript: 'allow',
fingerprinting: 'allow',
cookies: 'allow'
}
expect(actions.shieldsPanelDataUpdated(details)).toEqual({
type: types.SHIELDS_PANEL_DATA_UPDATED,
details
Expand Down
43 changes: 17 additions & 26 deletions components/test/brave_extension/actions/tabActions_test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
/* global describe, it */
/* 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/. */

import * as types from '../../../brave_extension/extension/brave_extension/constants/tabTypes'
import * as actions from '../../../brave_extension/extension/brave_extension/actions/tabActions'

const windowId = 1
const tabId = 1
const changeInfo = {}

const tab: chrome.tabs.Tab = {
id: 1,
index: 1,
pinned: false,
highlighted: false,
windowId: 1,
active: true,
incognito: false,
selected: false,
discarded: false,
autoDiscardable: false
}

describe('tabActions', () => {
it('activeTabChanged', () => {
const windowId = 1
const tabId = 1
expect(actions.activeTabChanged(windowId, tabId)).toEqual({
type: types.ACTIVE_TAB_CHANGED,
windowId: windowId,
Expand All @@ -18,36 +32,13 @@ describe('tabActions', () => {
})

it('tabCreated', () => {
const tab: chrome.tabs.Tab = {
id: 1,
index: 1,
pinned: false,
highlighted: false,
windowId: 1,
active: true,
incognito: false,
selected: false
}

expect(actions.tabCreated(tab)).toEqual({
type: types.TAB_CREATED,
tab
})
})

it('tabDataChanged', () => {
const tabId = 1
const changeInfo = {}
const tab: chrome.tabs.Tab = {
id: 1,
index: 1,
pinned: false,
highlighted: false,
windowId: 1,
active: true,
incognito: false,
selected: false
}
expect(actions.tabDataChanged(tabId, changeInfo, tab)).toEqual({
type: types.TAB_DATA_CHANGED,
tabId,
Expand Down
3 changes: 3 additions & 0 deletions components/test/brave_extension/actions/windowActions_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
* 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/. */

// Types
import * as types from '../../../brave_extension/extension/brave_extension/constants/windowTypes'

// Actions
import * as actions from '../../../brave_extension/extension/brave_extension/actions/windowActions'

describe('windowActions', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as sinon from 'sinon'
import * as cosmeticFilterAPI from '../../../../brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI'

describe('cosmeticFilterTestSuite', () => {
describe('cosmeticFilter API', () => {
describe('addSiteCosmeticFilter', () => {
const url = 'https://www.brave.com'
const filter = '#cssFilter'
Expand Down Expand Up @@ -233,7 +233,6 @@ describe('cosmeticFilterTestSuite', () => {
cssOrigin: 'user',
runAt: 'document_start'
})

})
// chrome.local.storage.get() always returns an empty object if nothing exists
it('doesn\'t apply filters if storage for host is implicitly undefined', () => {
Expand Down
22 changes: 13 additions & 9 deletions components/test/brave_extension/background/api/shieldsAPI_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import actions from '../../../../brave_extension/extension/brave_extension/background/actions/shieldsPanelActions'
import * as shieldsAPI from '../../../../brave_extension/extension/brave_extension/background/api/shieldsAPI'
import { activeTabData } from '../../../testData'
// import { Tab as TabType } from '../../../types/state/shieldsPannelState'
import { ShieldDetails } from '../../../../brave_extension/extension/brave_extension/types/actions/shieldsPanelActions'

describe('Shields API', () => {
describe('getShieldSettingsForTabData', () => {
Expand All @@ -26,7 +26,9 @@ describe('Shields API', () => {
windowId: 1,
active: true,
incognito: false,
selected: false
selected: false,
discarded: false,
autoDiscardable: false
}

expect.assertions(1)
Expand Down Expand Up @@ -91,15 +93,13 @@ describe('Shields API', () => {
spy.mockRestore()
})
it('resolves and calls requestShieldPanelData', (cb) => {
const tab = {
url: 'https://www.brave.com/test',
origin: 'https://www.brave.com',
hostname: 'www.brave.com',
id: 2,
braveShields: 'block',
const details: ShieldDetails = {
ads: 'block',
trackers: 'block',
httpUpgradableResources: 'block',
origin: 'https://www.brave.com',
hostname: 'www.brave.com',
id: 2,
javascript: 'block',
fingerprinting: 'block',
cookies: 'block'
Expand All @@ -109,7 +109,11 @@ describe('Shields API', () => {
shieldsAPI.requestShieldPanelData(tabId)
.then(() => {
expect(spy).toBeCalledTimes(1)
expect(spy.mock.calls[0][0]).toEqual(tab)
expect(spy.mock.calls[0][0]).toEqual({
...details,
url: 'https://www.brave.com/test',
braveShields: 'block'
})
cb()
})
.catch((e: Error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import '../../../../brave_extension/extension/brave_extension/background/events/runtimeEvents'
import windowActions from '../../../../brave_extension/extension/brave_extension/background/actions/windowActions'
import tabActions from '../../../../brave_extension/extension/brave_extension/background/actions/tabActions'
import * as windowTypes from '../../../../brave_extension/extension/brave_extension/constants/windowTypes'
import * as tabTypes from '../../../../brave_extension/extension/brave_extension/constants/tabTypes'
import { ChromeEvent } from '../../../testData'

interface InputWindows {
Expand Down Expand Up @@ -41,12 +43,36 @@ describe('runtimeEvents events', () => {
const p = new Promise((resolve, reject) => {
deferred = resolve
})
const window: chrome.windows.Window = {
id: 1,
state: 'normal',
focused: true,
alwaysOnTop: false,
incognito: false,
type: 'normal'
}
const tab: chrome.tabs.Tab = {
id: 3,
index: 0,
pinned: false,
highlighted: false,
windowId: 1,
active: true,
incognito: false,
selected: true,
discarded: false,
autoDiscardable: false
}
let windowCreatedSpy: jest.SpyInstance
let tabCreatedSpy: jest.SpyInstance
let windowGetAllSpy: jest.SpyInstance
beforeEach((cb) => {
windowCreatedSpy = jest.spyOn(windowActions, 'windowCreated')
// ensure return value is also mocked so a warning about lack of tabId is not thrown
.mockReturnValue({ type: windowTypes.WINDOW_CREATED, window })
tabCreatedSpy = jest.spyOn(tabActions, 'tabCreated')
// ensure return value is also mocked so a warning about lack of tabId is not thrown
.mockReturnValue({ type: tabTypes.TAB_CREATED, tab })
windowGetAllSpy = jest.spyOn(chrome.windows, 'getAllAsync').mockImplementation(() => {
deferred(inputWindows)
return p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import '../../../../brave_extension/extension/brave_extension/background/events/tabsEvents'
import actions from '../../../../brave_extension/extension/brave_extension/background/actions/tabActions'
import * as types from '../../../../brave_extension/extension/brave_extension/constants/tabTypes'

interface TabActivatedEvent extends chrome.events.Event<(activeInfo: chrome.tabs.TabActiveInfo) => void> {
emit: (detail: chrome.tabs.TabActiveInfo) => void
Expand All @@ -22,6 +23,8 @@ describe('tabsEvents events', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(actions, 'activeTabChanged')
// ensure return value is also mocked so a warning about lack of tabId is not thrown
.mockReturnValue({ type: types.ACTIVE_TAB_CHANGED, windowId: 1, tabId: 2 })
})
afterEach(() => {
spy.mockRestore()
Expand All @@ -43,18 +46,22 @@ describe('tabsEvents events', () => {
})
describe('chrome.tabs.onCreated', () => {
let spy: jest.SpyInstance
const inputTab = {
const inputTab: chrome.tabs.Tab = {
id: 3,
index: 0,
pinned: false,
highlighted: false,
windowId: 1,
active: true,
incognito: false,
selected: true
selected: true,
discarded: false,
autoDiscardable: false
}
beforeEach(() => {
spy = jest.spyOn(actions, 'tabCreated')
// ensure return value is also mocked so a warning about lack of tabId is not thrown
.mockReturnValue({ type: types.TAB_CREATED, tab: inputTab })
})
afterEach(() => {
spy.mockRestore()
Expand All @@ -76,18 +83,27 @@ describe('tabsEvents events', () => {
let spy: jest.SpyInstance
const inputTabId = 3
const inputChangeInfo = {}
const inputTab = {
const inputTab: chrome.tabs.Tab = {
id: 3,
index: 0,
pinned: false,
highlighted: false,
windowId: 1,
active: true,
incognito: false,
selected: true
selected: true,
discarded: false,
autoDiscardable: false
}
beforeEach(() => {
spy = jest.spyOn(actions, 'tabDataChanged')
// ensure return value is also mocked so a warning about lack of tabId is not thrown
.mockReturnValue({
type: types.TAB_DATA_CHANGED,
tab: inputTab,
tabId: inputTabId,
changeInfo: inputChangeInfo
})
})
afterEach(() => {
spy.mockRestore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import '../../../../brave_extension/extension/brave_extension/background/events/windowsEvents'
import actions from '../../../../brave_extension/extension/brave_extension/background/actions/windowActions'
import * as types from '../../../../brave_extension/extension/brave_extension/constants/windowTypes'

interface WindowIdEvent extends chrome.events.Event<(windowId: number) => void> {
emit: (windowId: number) => void
Expand All @@ -18,6 +19,8 @@ describe('windowsEvents events', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(actions, 'windowFocusChanged')
// ensure return value is also mocked so a warning about lack of tabId is not thrown
.mockReturnValue({ type: types.WINDOW_FOCUS_CHANGED, windowId: 1 })
})
afterEach(() => {
spy.mockRestore()
Expand All @@ -35,7 +38,7 @@ describe('windowsEvents events', () => {
})
describe('chrome.windows.onCreated', () => {
let spy: jest.SpyInstance
const window = {
const window: chrome.windows.Window = {
id: 1,
state: 'normal',
focused: true,
Expand All @@ -45,6 +48,8 @@ describe('windowsEvents events', () => {
}
beforeEach(() => {
spy = jest.spyOn(actions, 'windowCreated')
// ensure return value is also mocked so a warning about lack of tabId is not thrown
.mockReturnValue({ type: types.WINDOW_CREATED, window })
})
afterEach(() => {
spy.mockRestore()
Expand All @@ -65,6 +70,8 @@ describe('windowsEvents events', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(actions, 'windowRemoved')
// ensure return value is also mocked so a warning about lack of tabId is not thrown
.mockReturnValue({ type: types.WINDOW_REMOVED, windowId: 1 })
})
afterEach(() => {
spy.mockRestore()
Expand Down
Loading

0 comments on commit b3b7461

Please sign in to comment.