Skip to content

Commit

Permalink
Adding decimal place for time saved stats
Browse files Browse the repository at this point in the history
Fixes brave#6650

Time saved in hours will now have 1 decimal place. And time saved
in days will have 2 decimal places

Auditors: @bbondy @bsclifton
  • Loading branch information
maazadeeb committed Mar 4, 2017
1 parent 39ffa5f commit 921a0f8
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
6 changes: 4 additions & 2 deletions js/about/newTabComponents/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ class Stats extends ImmutableComponent {
counter = Math.ceil(estimatedMillisecondsSaved / 1000 / 60)
text = 'minutes'
} else if (hours) {
counter = Math.ceil(estimatedMillisecondsSaved / 1000 / 60 / 60)
// Refer to http://stackoverflow.com/a/12830454/2950032 for the detailed reasoning behind the + after
// toFixed is applied. In a nutshell, + is used to discard unnecessary trailing 0s after calling toFixed
counter = +((estimatedMillisecondsSaved / 1000 / 60 / 60).toFixed(1))
text = 'hours'
} else {
// Otherwise the output is in days
counter = Math.ceil(estimatedMillisecondsSaved / 1000 / 60 / 60 / 24)
counter = +((estimatedMillisecondsSaved / 1000 / 60 / 60 / 24).toFixed(2))
text = 'days'
}

Expand Down
122 changes: 122 additions & 0 deletions test/unit/about/newTabPageTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const sinon = require('sinon')
const assert = require('assert')
const Immutable = require('immutable')
const fakeElectron = require('../lib/fakeElectron')
const _ = require('underscore')
let NewTabPage, randomSpy, Clock, Stats, FooterInfo
require('../braveUnit')

Expand Down Expand Up @@ -47,6 +48,33 @@ describe('NewTab component unit tests', function () {
backgroundImage: 'url(testing123.jpg)'
}
}
const TIME_UNIT = {
SECOND: 'S',
MINUTE: 'M',
HOUR: 'H',
DAY: 'D'
}
const calculateSavedCount = function (estimatedTimeValue, estimatedTimeUnit, millisecondsPerItem) {
let milliseconds
switch (estimatedTimeUnit) {
case TIME_UNIT.SECOND:
milliseconds = estimatedTimeValue * 1000
break
case TIME_UNIT.MINUTE:
milliseconds = estimatedTimeValue * 60 * 1000
break
case TIME_UNIT.HOUR:
milliseconds = estimatedTimeValue * 60 * 60 * 1000
break
case TIME_UNIT.DAY:
milliseconds = estimatedTimeValue * 24 * 60 * 60 * 1000
break
default:
milliseconds = 0
break
}
return milliseconds / millisecondsPerItem
}

beforeEach(function () {
wrapper = shallow(
Expand Down Expand Up @@ -190,4 +218,98 @@ describe('NewTab component unit tests', function () {
})
})
})

describe('Time saved stats, when time saved', function () {
const runs = [{
message: '== 1 second',
input: {
estimatedTimeValue: 1,
estimatedTimeUnit: TIME_UNIT.SECOND
},
expectedOutput: {
expectedTimeSaved: 1
}
}, {
message: '== 1.5 seconds',
input: {
estimatedTimeValue: 1.5,
estimatedTimeUnit: TIME_UNIT.SECOND
},
expectedOutput: {
expectedTimeSaved: 2
}
}, {
message: '== 1 minute',
input: {
estimatedTimeValue: 1,
estimatedTimeUnit: TIME_UNIT.MINUTE
},
expectedOutput: {
expectedTimeSaved: 1
}
}, {
message: '== 1.5 minutes',
input: {
estimatedTimeValue: 1.5,
estimatedTimeUnit: TIME_UNIT.MINUTE
},
expectedOutput: {
expectedTimeSaved: 2
}
}, {
message: '== 1 hour',
input: {
estimatedTimeValue: 1,
estimatedTimeUnit: TIME_UNIT.HOUR
},
expectedOutput: {
expectedTimeSaved: 1
}
}, {
message: '== 1.55 hours',
input: {
estimatedTimeValue: 1.55,
estimatedTimeUnit: TIME_UNIT.HOUR
},
expectedOutput: {
expectedTimeSaved: 1.6
}
}, {
message: '== 1 day',
input: {
estimatedTimeValue: 1,
estimatedTimeUnit: TIME_UNIT.DAY
},
expectedOutput: {
expectedTimeSaved: 1
}
}, {
message: '== 2.555 days',
input: {
estimatedTimeValue: 2.555,
estimatedTimeUnit: TIME_UNIT.DAY
},
expectedOutput: {
expectedTimeSaved: 2.56
}
}]
let millisecondsPerItem

// This is just there to get the millisecondsPerItem in a generic way
before(function () {
const dummyStatsInstace = shallow(<Stats newTabData={Immutable.fromJS({})} />).instance()
millisecondsPerItem = dummyStatsInstace.millisecondsPerItem
})

_.each(runs, function (run) {
it(run.message, function () {
const timeSavedCount = calculateSavedCount(run.input.estimatedTimeValue, run.input.estimatedTimeUnit, millisecondsPerItem)
const newTabData = Immutable.fromJS({
adblockCount: timeSavedCount
})
const statsInstance = shallow(<Stats newTabData={newTabData} />).instance()
assert.equal(statsInstance.estimatedTimeSaved.value, run.expectedOutput.expectedTimeSaved)
})
})
})
})

0 comments on commit 921a0f8

Please sign in to comment.