From ec4cf96f0d4f170b21d77378e4f11b88b01aaebc Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Fri, 23 May 2014 19:02:50 +0200 Subject: Breadcrumb width calculation fix Rewrote the breadcrumb calculation to be more readable. Breadcrumb now has a setMaxWidth() method to set the maximum allowed width which is used to fit the breadcrumbs. The breadcrumb width is now based on the container width, passed through setMaxWidth() by the FileList class. Now using fixed widths for the test crumbs to simulate consistent widths across browsers which rendering engines might usually yield different results. --- apps/files/tests/js/breadcrumbSpec.js | 79 ++++++++++++++++------------------- 1 file changed, 37 insertions(+), 42 deletions(-) (limited to 'apps/files/tests/js/breadcrumbSpec.js') diff --git a/apps/files/tests/js/breadcrumbSpec.js b/apps/files/tests/js/breadcrumbSpec.js index e3d9c757a7c..30784fd70ad 100644 --- a/apps/files/tests/js/breadcrumbSpec.js +++ b/apps/files/tests/js/breadcrumbSpec.js @@ -19,7 +19,6 @@ * */ -/* global BreadCrumb */ describe('OCA.Files.BreadCrumb tests', function() { var BreadCrumb = OCA.Files.BreadCrumb; @@ -131,48 +130,42 @@ describe('OCA.Files.BreadCrumb tests', function() { }); }); describe('Resizing', function() { - var bc, widthStub, dummyDir, - oldUpdateTotalWidth; + var bc, dummyDir, widths, oldUpdateTotalWidth; beforeEach(function() { - dummyDir = '/short name/longer name/looooooooooooonger/even longer long long long longer long/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/last one'; + dummyDir = '/short name/longer name/looooooooooooonger/' + + 'even longer long long long longer long/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/last one'; + + // using hard-coded widths (pre-measured) to avoid getting different + // results on different browsers due to font engine differences + widths = [41, 106, 112, 160, 257, 251, 91]; oldUpdateTotalWidth = BreadCrumb.prototype._updateTotalWidth; BreadCrumb.prototype._updateTotalWidth = function() { - // need to set display:block for correct offsetWidth (no CSS loaded here) - $('div.crumb').css({ - 'display': 'block', - 'float': 'left' + // pre-set a width to simulate consistent measurement + $('div.crumb').each(function(index){ + $(this).css('width', widths[index]); }); return oldUpdateTotalWidth.apply(this, arguments); }; bc = new BreadCrumb(); - widthStub = sinon.stub($.fn, 'width'); // append dummy navigation and controls // as they are currently used for measurements $('#testArea').append( - '', '
' ); - - // make sure we know the test screen width - $('#testArea').css('width', 1280); - - // use test area as we need it for measurements $('#controls').append(bc.$el); - $('#controls').append('
Dummy action with a given width
'); }); afterEach(function() { BreadCrumb.prototype._updateTotalWidth = oldUpdateTotalWidth; - widthStub.restore(); bc = null; }); - it('Hides breadcrumbs to fit window', function() { + it('Hides breadcrumbs to fit max allowed width', function() { var $crumbs; - widthStub.returns(500); + bc.setMaxWidth(500); // triggers resize implicitly bc.setDirectory(dummyDir); $crumbs = bc.$el.find('.crumb'); @@ -190,19 +183,23 @@ describe('OCA.Files.BreadCrumb tests', function() { expect($crumbs.eq(4).hasClass('hidden')).toEqual(true); expect($crumbs.eq(5).hasClass('hidden')).toEqual(true); expect($crumbs.eq(6).hasClass('hidden')).toEqual(false); + expect($crumbs.eq(7).hasClass('hidden')).toEqual(false); }); - it('Updates ellipsis on window size increase', function() { + it('Updates the breadcrumbs when reducing max allowed width', function() { var $crumbs; - widthStub.returns(500); + // enough space + bc.setMaxWidth(1800); + + expect(bc.$el.find('.ellipsis').length).toEqual(0); + // triggers resize implicitly bc.setDirectory(dummyDir); - $crumbs = bc.$el.find('.crumb'); // simulate increase - $('#testArea').css('width', 1800); - bc.resize(1800); + bc.setMaxWidth(950); + $crumbs = bc.$el.find('.crumb'); // first one is always visible expect($crumbs.eq(0).hasClass('hidden')).toEqual(false); // second one has ellipsis @@ -213,37 +210,35 @@ describe('OCA.Files.BreadCrumb tests', function() { // subsequent elements are hidden expect($crumbs.eq(2).hasClass('hidden')).toEqual(true); expect($crumbs.eq(3).hasClass('hidden')).toEqual(true); - expect($crumbs.eq(4).hasClass('hidden')).toEqual(true); // the rest is visible + expect($crumbs.eq(4).hasClass('hidden')).toEqual(false); expect($crumbs.eq(5).hasClass('hidden')).toEqual(false); expect($crumbs.eq(6).hasClass('hidden')).toEqual(false); }); - it('Updates ellipsis on window size decrease', function() { + it('Removes the ellipsis when there is enough space', function() { var $crumbs; - $('#testArea').css('width', 2000); - widthStub.returns(2000); + bc.setMaxWidth(500); // triggers resize implicitly bc.setDirectory(dummyDir); $crumbs = bc.$el.find('.crumb'); - // simulate decrease - bc.resize(500); - $('#testArea').css('width', 500); + // ellipsis + expect(bc.$el.find('.ellipsis').length).toEqual(1); - // first one is always visible + // simulate increase + bc.setMaxWidth(1800); + + // no ellipsis + expect(bc.$el.find('.ellipsis').length).toEqual(0); + + // all are visible expect($crumbs.eq(0).hasClass('hidden')).toEqual(false); - // second one has ellipsis expect($crumbs.eq(1).hasClass('hidden')).toEqual(false); - expect($crumbs.eq(1).find('.ellipsis').length).toEqual(1); - // there is only one ellipsis in total - expect($crumbs.find('.ellipsis').length).toEqual(1); - // subsequent elements are hidden - expect($crumbs.eq(2).hasClass('hidden')).toEqual(true); - expect($crumbs.eq(3).hasClass('hidden')).toEqual(true); - expect($crumbs.eq(4).hasClass('hidden')).toEqual(true); - // the rest is visible - expect($crumbs.eq(5).hasClass('hidden')).toEqual(true); + expect($crumbs.eq(2).hasClass('hidden')).toEqual(false); + expect($crumbs.eq(3).hasClass('hidden')).toEqual(false); + expect($crumbs.eq(4).hasClass('hidden')).toEqual(false); + expect($crumbs.eq(5).hasClass('hidden')).toEqual(false); expect($crumbs.eq(6).hasClass('hidden')).toEqual(false); }); }); -- cgit v1.2.3