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

Commit

Permalink
Updated code to handle falsey inputs + added more unit tests :)
Browse files Browse the repository at this point in the history
Auditors: @diracdeltas

Test Plan:
`npm run unittest -- --grep="downloadUtil"`
  • Loading branch information
bsclifton committed Sep 9, 2017
1 parent e74d310 commit 2289c40
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
8 changes: 4 additions & 4 deletions js/state/downloadUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const stopStates = [downloadStates.CANCELLED, downloadStates.INTERRUPTED, downlo
const notErrorStates = [downloadStates.IN_PROGRESS, downloadStates.PAUSED, downloadStates.COMPLETED]

const downloadIsInState = (download, list) =>
list.includes(download.get('state'))
(download && list && list.includes(download.get('state'))) || false

const isPendingState = (download) =>
downloadIsInState(download, pendingStates)
Expand Down Expand Up @@ -57,7 +57,7 @@ const getL10nId = (download) => {
}

const getPercentageComplete = (download) => {
const totalBytes = download.get('totalBytes')
const totalBytes = download && download.get('totalBytes')
if (!totalBytes) {
// Most likely totalBytes has not been calculated yet. Avoid
// division by 0.
Expand All @@ -66,10 +66,10 @@ const getPercentageComplete = (download) => {
return Math.ceil(download.get('receivedBytes') / totalBytes * 100) + '%'
}

const shouldAllowCopyLink = (download) => !!download.get('url')
const shouldAllowCopyLink = (download) => (download && !!download.get('url')) || false

const getDownloadItems = (state) => {
if (!state.get('downloads')) {
if (!state || !state.get('downloads')) {
return Immutable.List()
}

Expand Down
40 changes: 40 additions & 0 deletions test/unit/state/downloadUtilTest.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
/* global describe, it */

const downloadUtil = require('../../../js/state/downloadUtil')
const downloadStates = require('../../../js/constants/downloadStates')
const assert = require('assert')
const Immutable = require('immutable')

describe('downloadUtil', function () {
describe('shouldAllowPause', function () {
it('handles falsey input', function () {
assert.equal(downloadUtil.shouldAllowPause(undefined), false)
})
it('returns true if state is `downloadStates.IN_PROGRESS`', function () {
assert.equal(downloadUtil.shouldAllowPause(new Immutable.Map({
state: downloadStates.IN_PROGRESS
}), [downloadStates.IN_PROGRESS]), true)
})
})

describe('getPercentageComplete', function () {
it('handles falsey input', function () {
assert.equal(downloadUtil.getPercentageComplete(undefined), '0%')
})
it('returns percentage complete for nonzero totalBytes', function () {
assert.equal(downloadUtil.getPercentageComplete(new Immutable.Map({
totalBytes: 100,
Expand Down Expand Up @@ -34,4 +49,29 @@ describe('downloadUtil', function () {
})), '0%')
})
})

describe('shouldAllowCopyLink', function () {
it('returns false if input is falsey', function () {
assert.equal(downloadUtil.shouldAllowCopyLink(undefined), false)
})
it('returns false if `download.url` does not have a value', function () {
assert.equal(downloadUtil.shouldAllowCopyLink(new Immutable.Map({
totalBytes: 0
})), false)
})
it('returns true if `download.url` has a value', function () {
assert.equal(downloadUtil.shouldAllowCopyLink(new Immutable.Map({
url: 'https://clifton.io/robots.txt'
})), true)
})
})

describe('getDownloadItems', function () {
it('returns an empty Immutable.List if intput is falsey', function () {
assert.deepEqual(downloadUtil.getDownloadItems(undefined), Immutable.List())
})
it('returns an empty Immutable.List if `state.downloads` is falsey', function () {
assert.deepEqual(downloadUtil.getDownloadItems(new Immutable.Map({})), Immutable.List())
})
})
})

0 comments on commit 2289c40

Please sign in to comment.