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

Commit

Permalink
Added error handling logic and unit tests for the new getTotalBlocks …
Browse files Browse the repository at this point in the history
…method

Auditors: @NejcZdovc
  • Loading branch information
bsclifton committed Mar 27, 2017
1 parent 6a9cb8a commit c71826f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
21 changes: 18 additions & 3 deletions js/components/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const LongPressButton = require('./longPressButton')
const Menubar = require('../../app/renderer/components/menubar')
const WindowCaptionButtons = require('../../app/renderer/components/windowCaptionButtons')
const CheckDefaultBrowserDialog = require('../../app/renderer/components/checkDefaultBrowserDialog')

// Constants
const appConfig = require('../constants/appConfig')
const messages = require('../constants/messages')
Expand Down Expand Up @@ -76,6 +77,7 @@ const debounce = require('../lib/debounce')
const {currentWindow, isMaximized, isFocused, isFullScreen} = require('../../app/renderer/currentWindow')
const emptyMap = new Immutable.Map()
const emptyList = new Immutable.List()
const {makeImmutable} = require('../../app/common/state/immutableUtil')

class Main extends ImmutableComponent {
constructor () {
Expand Down Expand Up @@ -891,13 +893,26 @@ class Main extends ImmutableComponent {
}

getTotalBlocks (frames) {
if (!frames) {
return false
}

frames = makeImmutable(frames)

const ads = frames.getIn(['adblock', 'blocked'])
const trackers = frames.getIn(['trackingProtection', 'blocked'])
const scripts = frames.getIn(['noScript', 'blocked'])
const fingerprint = frames.getIn(['fingerprintingProtection', 'blocked'])
const blocked = (ads ? ads.size : 0) + (trackers ? trackers.size : 0) + (scripts ? scripts.size : 0) + (fingerprint ? fingerprint.size : 0)

return (blocked.size === 0) ? false : ((blocked > 99) ? '99+' : blocked)
const blocked = (ads && ads.size ? ads.size : 0) +
(trackers && trackers.size ? trackers.size : 0) +
(scripts && scripts.size ? scripts.size : 0) +
(fingerprint && fingerprint.size ? fingerprint.size : 0)

return (blocked === 0)
? false
: ((blocked > 99)
? '99+'
: blocked)
}

render () {
Expand Down
75 changes: 75 additions & 0 deletions test/unit/js/components/mainTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,79 @@ describe('Main component unit tests', function () {
assert.equal(node.props.disabled, true)
})
})

describe('getTotalBlocks', function () {
let instance

before(function () {
let wrapper = shallow(
<Main windowState={windowState} appState={appState} />
)
instance = wrapper.instance()
})

it('returns false if there are no units blocked', function () {
const frames = Immutable.fromJS({
adblock: { blocked: [] },
trackingProtection: { blocked: [] },
noScript: { blocked: [] },
fingerprintingProtection: { blocked: [] }
})
const result = instance.getTotalBlocks(frames)
assert.equal(result, false)
})

it('returns total of items (ads / trackers / scripts / fingerprints) blocked', function () {
const frames = Immutable.fromJS({
adblock: { blocked: [1] },
trackingProtection: { blocked: [1, 2] },
noScript: { blocked: [1, 2, 3, 4] },
fingerprintingProtection: { blocked: [1, 2, 3, 4, 5, 6, 7, 8] }
})
const result = instance.getTotalBlocks(frames)
assert.equal(result, 15)
})

it('defaults values to 0 if element is not a list or is not present', function () {
const frames = Immutable.fromJS({
adblock: { blocked: 'not a list' },
trackingProtection: {},
noScript: { blocked: [1] },
fingerprintingProtection: { blocked: {} }
})
const result = instance.getTotalBlocks(frames)
assert.equal(result, 1)
})

it('returns false if the input is falsey', function () {
assert.equal(instance.getTotalBlocks(), false)
assert.equal(instance.getTotalBlocks(undefined), false)
assert.equal(instance.getTotalBlocks(null), false)
assert.equal(instance.getTotalBlocks(false), false)
})

it('converts the input to an immutable object', function () {
const mutableFrames = {
adblock: { blocked: [1] },
trackingProtection: { blocked: [1, 2] },
noScript: { blocked: [1, 2, 3, 4] },
fingerprintingProtection: { blocked: [1, 2, 3, 4, 5, 6, 7, 8] }
}
const result = instance.getTotalBlocks(mutableFrames)
assert.equal(result, 15)
})

it('returns "99+" if tracker count is > 99', function () {
const mutableFrames = {
adblock: { blocked: [] }
}

for (let i = 1; i < 101; i++) {
mutableFrames.adblock.blocked.push(i)
}

const result = instance.getTotalBlocks(mutableFrames)
assert.equal(result, '99+')
})
})
})

0 comments on commit c71826f

Please sign in to comment.