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

Return 0% when download totalBytes is 0 or undefined #10652

Merged
merged 2 commits into from
Sep 9, 2017
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
17 changes: 12 additions & 5 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 @@ -56,13 +56,20 @@ const getL10nId = (download) => {
return ''
}

const getPercentageComplete = (download) =>
Math.ceil(download.get('receivedBytes') / download.get('totalBytes') * 100) + '%'
const getPercentageComplete = (download) => {
const totalBytes = download && download.get('totalBytes')
if (!totalBytes) {
// Most likely totalBytes has not been calculated yet. Avoid
// division by 0.
return '0%'
}
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
77 changes: 77 additions & 0 deletions test/unit/state/downloadUtilTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* 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,
receivedBytes: 10
})), '10%')
assert.equal(downloadUtil.getPercentageComplete(new Immutable.Map({
totalBytes: 1,
receivedBytes: 1
})), '100%')
assert.equal(downloadUtil.getPercentageComplete(new Immutable.Map({
totalBytes: 0,
receivedBytes: 1
})), '0%')
})
it('returns percentage complete for falsey totalBytes', function () {
assert.equal(downloadUtil.getPercentageComplete(new Immutable.Map({
totalBytes: 0,
receivedBytes: 10
})), '0%')
assert.equal(downloadUtil.getPercentageComplete(new Immutable.Map({
totalBytes: 0,
receivedBytes: 0
})), '0%')
assert.equal(downloadUtil.getPercentageComplete(new Immutable.Map({
receivedBytes: 0
})), '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())
})
})
})