humanFileSize: use toLocaleString
humanFileSize: add test with locale humanFileSize: use canonical locale humanFileSize: skip test w/o toLocaleString support humanFileSize: stub getCanonicalLocale OC.getCanonicalLocale: cover undefined case humanFileSize: fix getCanonicalLocale stub Signed-off-by: Claas Augner <git@caugner.de>
This commit is contained in:
parent
7c0cd4f9ff
commit
00b8be60f2
|
@ -770,6 +770,16 @@ var OCP = {},
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the user's locale as a BCP 47 compliant language tag
|
||||||
|
*
|
||||||
|
* @return {String} locale string
|
||||||
|
*/
|
||||||
|
getCanonicalLocale: function() {
|
||||||
|
var locale = this.getLocale();
|
||||||
|
return typeof locale === 'string' ? locale.replace(/_/g, '-') : locale;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the user's locale
|
* Returns the user's locale
|
||||||
*
|
*
|
||||||
|
@ -1843,6 +1853,9 @@ function humanFileSize(size, skipSmallSizes) {
|
||||||
else if(relativeSize.substr(relativeSize.length-2,2)==='.0'){
|
else if(relativeSize.substr(relativeSize.length-2,2)==='.0'){
|
||||||
relativeSize=relativeSize.substr(0,relativeSize.length-2);
|
relativeSize=relativeSize.substr(0,relativeSize.length-2);
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
relativeSize = parseFloat(relativeSize).toLocaleString(OC.getCanonicalLocale());
|
||||||
|
}
|
||||||
return relativeSize + ' ' + readableFormat;
|
return relativeSize + ' ' + readableFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -273,6 +273,25 @@ describe('Core base tests', function() {
|
||||||
expect(OC.filePath('files', 'ajax', 'test.php')).toEqual('http://localhost/index.php/apps/files/ajax/test.php');
|
expect(OC.filePath('files', 'ajax', 'test.php')).toEqual('http://localhost/index.php/apps/files/ajax/test.php');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('getCanonicalLocale', function() {
|
||||||
|
var localeStub;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
localeStub = sinon.stub(OC, 'getLocale');
|
||||||
|
});
|
||||||
|
afterEach(function() {
|
||||||
|
localeStub.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Returns primary locales as is", function() {
|
||||||
|
expect(OC.getCanonicalLocale('de')).toEqual('de');
|
||||||
|
expect(OC.getCanonicalLocale('zu')).toEqual('zu');
|
||||||
|
});
|
||||||
|
it("Returns extended locales with hyphens", function() {
|
||||||
|
expect(OC.getCanonicalLocale('az_Cyrl_AZ')).toEqual('az-Cyrl-AZ');
|
||||||
|
expect(OC.getCanonicalLocale('de_DE')).toEqual('de-DE');
|
||||||
|
});
|
||||||
|
});
|
||||||
describe('Link functions', function() {
|
describe('Link functions', function() {
|
||||||
var TESTAPP = 'testapp';
|
var TESTAPP = 'testapp';
|
||||||
var TESTAPP_ROOT = OC.getRootPath() + '/appsx/testapp';
|
var TESTAPP_ROOT = OC.getRootPath() + '/appsx/testapp';
|
||||||
|
@ -560,7 +579,26 @@ describe('Core base tests', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('Util', function() {
|
describe('Util', function() {
|
||||||
|
var locale;
|
||||||
|
var localeStub;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
locale = OC.getCanonicalLocale();
|
||||||
|
localeStub = sinon.stub(OC, 'getCanonicalLocale');
|
||||||
|
localeStub.returns(locale);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
localeStub.restore();
|
||||||
|
});
|
||||||
|
|
||||||
describe('humanFileSize', function() {
|
describe('humanFileSize', function() {
|
||||||
|
// cit() will skip tests if toLocaleString() is not supported.
|
||||||
|
// See https://github.com/ariya/phantomjs/issues/12581
|
||||||
|
//
|
||||||
|
// Please run these tests in Chrome/Firefox manually.
|
||||||
|
var cit = 4.2.toLocaleString("de") !== "4,2" ? xit : it;
|
||||||
|
|
||||||
it('renders file sizes with the correct unit', function() {
|
it('renders file sizes with the correct unit', function() {
|
||||||
var data = [
|
var data = [
|
||||||
[0, '0 B'],
|
[0, '0 B'],
|
||||||
|
@ -589,6 +627,22 @@ describe('Core base tests', function() {
|
||||||
expect(OC.Util.humanFileSize(data[i][0], true)).toEqual(data[i][1]);
|
expect(OC.Util.humanFileSize(data[i][0], true)).toEqual(data[i][1]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
cit('renders file sizes with the correct locale', function() {
|
||||||
|
localeStub.returns("de");
|
||||||
|
var data = [
|
||||||
|
[0, '0 B'],
|
||||||
|
["0", '0 B'],
|
||||||
|
["A", 'NaN B'],
|
||||||
|
[125, '125 B'],
|
||||||
|
[128000, '125 KB'],
|
||||||
|
[128000000, '122,1 MB'],
|
||||||
|
[128000000000, '119,2 GB'],
|
||||||
|
[128000000000000, '116,4 TB']
|
||||||
|
];
|
||||||
|
for (var i = 0; i < data.length; i++) {
|
||||||
|
expect(OC.Util.humanFileSize(data[i][0])).toEqual(data[i][1]);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
describe('computerFileSize', function() {
|
describe('computerFileSize', function() {
|
||||||
it('correctly parses file sizes from a human readable formated string', function() {
|
it('correctly parses file sizes from a human readable formated string', function() {
|
||||||
|
|
Loading…
Reference in New Issue