From 921a0f85aa3ce00886b2851e7a90343f9a90eb01 Mon Sep 17 00:00:00 2001 From: Maaz Syed Adeeb Date: Sat, 4 Mar 2017 15:10:32 +0530 Subject: [PATCH] Adding decimal place for time saved stats Fixes #6650 Time saved in hours will now have 1 decimal place. And time saved in days will have 2 decimal places Auditors: @bbondy @bsclifton --- js/about/newTabComponents/stats.js | 6 +- test/unit/about/newTabPageTest.js | 122 +++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 2 deletions(-) diff --git a/js/about/newTabComponents/stats.js b/js/about/newTabComponents/stats.js index bb4ccf23ec..ab067e4738 100644 --- a/js/about/newTabComponents/stats.js +++ b/js/about/newTabComponents/stats.js @@ -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' } diff --git a/test/unit/about/newTabPageTest.js b/test/unit/about/newTabPageTest.js index a753ac6f39..81005d2e33 100644 --- a/test/unit/about/newTabPageTest.js +++ b/test/unit/about/newTabPageTest.js @@ -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') @@ -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( @@ -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().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().instance() + assert.equal(statsInstance.estimatedTimeSaved.value, run.expectedOutput.expectedTimeSaved) + }) + }) + }) })