Skip to content

Commit

Permalink
tests(i18n): direct test for _byteFormatterForGranularity
Browse files Browse the repository at this point in the history
  • Loading branch information
acutmore committed Sep 30, 2020
1 parent c534d3d commit f03d5e3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lighthouse-core/report/html/renderer/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ class I18n {
* @return {Intl.NumberFormat}
*/
_byteFormatterForGranularity(granularity) {
if (granularity >= 1) {
return this._numberFormatter;
let numberOfFractionDigits = 0;
if (granularity < 1) {
// assumes granularities above 1 will not contain fractional parts, i.e. will never be 1.5
numberOfFractionDigits = -Math.floor(Math.log10(granularity));
}
const numberOfFractionDigits = -Math.floor(Math.log10(granularity));
return new Intl.NumberFormat(this._numberDateLocale, {
maximumFractionDigits: numberOfFractionDigits,
minimumFractionDigits: numberOfFractionDigits,
Expand Down
27 changes: 27 additions & 0 deletions lighthouse-core/test/report/html/renderer/i18n-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,31 @@ describe('util helpers', () => {
timestamp.includes('Apr 29, 2017')
);
});

describe('_byteFormatterForGranularity', () => {
it('returns a formatter that outputs a consistent number of fractional digits', () => {
const i18n = new I18n('en', {...Util.UIStrings});

const formatterTen = i18n._byteFormatterForGranularity(10);
const formatterOne = i18n._byteFormatterForGranularity(1);
const formatterHalf = i18n._byteFormatterForGranularity(0.5);
const formatterTenth = i18n._byteFormatterForGranularity(0.1);
const formatterHundredth = i18n._byteFormatterForGranularity(0.01);

assert.strictEqual(formatterTen.format(15.0), '15');
assert.strictEqual(formatterTen.format(15.12345), '15');

assert.strictEqual(formatterOne.format(15.0), '15');
assert.strictEqual(formatterOne.format(15.12345), '15');

assert.strictEqual(formatterHalf.format(15.0), '15.0');
assert.strictEqual(formatterHalf.format(15.12345), '15.1');

assert.strictEqual(formatterTenth.format(15.0), '15.0');
assert.strictEqual(formatterTenth.format(15.12345), '15.1');

assert.strictEqual(formatterHundredth.format(15.0), '15.00');
assert.strictEqual(formatterHundredth.format(15.12345), '15.12');
});
});
});

0 comments on commit f03d5e3

Please sign in to comment.