breadcrumbSpec.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. describe('OCA.Files.BreadCrumb tests', function() {
  22. var BreadCrumb = OCA.Files.BreadCrumb;
  23. describe('Rendering', function() {
  24. var bc;
  25. beforeEach(function() {
  26. bc = new BreadCrumb({
  27. getCrumbUrl: function(part, index) {
  28. // for testing purposes
  29. return part.dir + '#' + index;
  30. }
  31. });
  32. });
  33. afterEach(function() {
  34. bc = null;
  35. });
  36. it('Renders its own container', function() {
  37. bc.render();
  38. expect(bc.$el.hasClass('breadcrumb')).toEqual(true);
  39. });
  40. it('Renders root by default', function() {
  41. var $crumbs;
  42. bc.render();
  43. $crumbs = bc.$el.find('.crumb');
  44. expect($crumbs.length).toEqual(1);
  45. expect($crumbs.eq(0).find('a').attr('href')).toEqual('/#0');
  46. expect($crumbs.eq(0).find('img').length).toEqual(1);
  47. expect($crumbs.eq(0).attr('data-dir')).toEqual('/');
  48. });
  49. it('Renders root when switching to root', function() {
  50. var $crumbs;
  51. bc.setDirectory('/somedir');
  52. bc.setDirectory('/');
  53. $crumbs = bc.$el.find('.crumb');
  54. expect($crumbs.length).toEqual(1);
  55. expect($crumbs.eq(0).attr('data-dir')).toEqual('/');
  56. });
  57. it('Renders last crumb with "last" class', function() {
  58. bc.setDirectory('/abc/def');
  59. expect(bc.$el.find('.crumb:last').hasClass('last')).toEqual(true);
  60. });
  61. it('Renders single path section', function() {
  62. var $crumbs;
  63. bc.setDirectory('/somedir');
  64. $crumbs = bc.$el.find('.crumb');
  65. expect($crumbs.length).toEqual(2);
  66. expect($crumbs.eq(0).find('a').attr('href')).toEqual('/#0');
  67. expect($crumbs.eq(0).find('img').length).toEqual(1);
  68. expect($crumbs.eq(0).attr('data-dir')).toEqual('/');
  69. expect($crumbs.eq(1).find('a').attr('href')).toEqual('/somedir#1');
  70. expect($crumbs.eq(1).find('img').length).toEqual(0);
  71. expect($crumbs.eq(1).attr('data-dir')).toEqual('/somedir');
  72. });
  73. it('Renders multiple path sections and special chars', function() {
  74. var $crumbs;
  75. bc.setDirectory('/somedir/with space/abc');
  76. $crumbs = bc.$el.find('.crumb');
  77. expect($crumbs.length).toEqual(4);
  78. expect($crumbs.eq(0).find('a').attr('href')).toEqual('/#0');
  79. expect($crumbs.eq(0).find('img').length).toEqual(1);
  80. expect($crumbs.eq(0).attr('data-dir')).toEqual('/');
  81. expect($crumbs.eq(1).find('a').attr('href')).toEqual('/somedir#1');
  82. expect($crumbs.eq(1).find('img').length).toEqual(0);
  83. expect($crumbs.eq(1).attr('data-dir')).toEqual('/somedir');
  84. expect($crumbs.eq(2).find('a').attr('href')).toEqual('/somedir/with space#2');
  85. expect($crumbs.eq(2).find('img').length).toEqual(0);
  86. expect($crumbs.eq(2).attr('data-dir')).toEqual('/somedir/with space');
  87. expect($crumbs.eq(3).find('a').attr('href')).toEqual('/somedir/with space/abc#3');
  88. expect($crumbs.eq(3).find('img').length).toEqual(0);
  89. expect($crumbs.eq(3).attr('data-dir')).toEqual('/somedir/with space/abc');
  90. });
  91. it('Renders backslashes as regular directory separator', function() {
  92. var $crumbs;
  93. bc.setDirectory('/somedir\\with/mixed\\separators');
  94. $crumbs = bc.$el.find('.crumb');
  95. expect($crumbs.length).toEqual(5);
  96. expect($crumbs.eq(0).find('a').attr('href')).toEqual('/#0');
  97. expect($crumbs.eq(0).find('img').length).toEqual(1);
  98. expect($crumbs.eq(0).attr('data-dir')).toEqual('/');
  99. expect($crumbs.eq(1).find('a').attr('href')).toEqual('/somedir#1');
  100. expect($crumbs.eq(1).find('img').length).toEqual(0);
  101. expect($crumbs.eq(1).attr('data-dir')).toEqual('/somedir');
  102. expect($crumbs.eq(2).find('a').attr('href')).toEqual('/somedir/with#2');
  103. expect($crumbs.eq(2).find('img').length).toEqual(0);
  104. expect($crumbs.eq(2).attr('data-dir')).toEqual('/somedir/with');
  105. expect($crumbs.eq(3).find('a').attr('href')).toEqual('/somedir/with/mixed#3');
  106. expect($crumbs.eq(3).find('img').length).toEqual(0);
  107. expect($crumbs.eq(3).attr('data-dir')).toEqual('/somedir/with/mixed');
  108. expect($crumbs.eq(4).find('a').attr('href')).toEqual('/somedir/with/mixed/separators#4');
  109. expect($crumbs.eq(4).find('img').length).toEqual(0);
  110. expect($crumbs.eq(4).attr('data-dir')).toEqual('/somedir/with/mixed/separators');
  111. });
  112. });
  113. describe('Events', function() {
  114. it('Calls onClick handler when clicking on a crumb', function() {
  115. var handler = sinon.stub();
  116. var bc = new BreadCrumb({
  117. onClick: handler
  118. });
  119. bc.setDirectory('/one/two/three/four');
  120. bc.$el.find('.crumb:eq(3)').click();
  121. expect(handler.calledOnce).toEqual(true);
  122. expect(handler.getCall(0).thisValue).toEqual(bc.$el.find('.crumb').get(3));
  123. handler.reset();
  124. bc.$el.find('.crumb:eq(0) a').click();
  125. expect(handler.calledOnce).toEqual(true);
  126. expect(handler.getCall(0).thisValue).toEqual(bc.$el.find('.crumb').get(0));
  127. });
  128. it('Calls onDrop handler when dropping on a crumb', function() {
  129. var droppableStub = sinon.stub($.fn, 'droppable');
  130. var handler = sinon.stub();
  131. var bc = new BreadCrumb({
  132. onDrop: handler
  133. });
  134. bc.setDirectory('/one/two/three/four');
  135. expect(droppableStub.calledOnce).toEqual(true);
  136. expect(droppableStub.getCall(0).args[0].drop).toBeDefined();
  137. // simulate drop
  138. droppableStub.getCall(0).args[0].drop({dummy: true});
  139. expect(handler.calledOnce).toEqual(true);
  140. expect(handler.getCall(0).args[0]).toEqual({dummy: true});
  141. droppableStub.restore();
  142. });
  143. });
  144. describe('Resizing', function() {
  145. var bc, dummyDir, widths, oldUpdateTotalWidth;
  146. beforeEach(function() {
  147. dummyDir = '/short name/longer name/looooooooooooonger/' +
  148. 'even longer long long long longer long/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/last one';
  149. // using hard-coded widths (pre-measured) to avoid getting different
  150. // results on different browsers due to font engine differences
  151. widths = [41, 106, 112, 160, 257, 251, 91];
  152. oldUpdateTotalWidth = BreadCrumb.prototype._updateTotalWidth;
  153. BreadCrumb.prototype._updateTotalWidth = function() {
  154. // pre-set a width to simulate consistent measurement
  155. $('div.crumb').each(function(index){
  156. $(this).css('width', widths[index]);
  157. });
  158. return oldUpdateTotalWidth.apply(this, arguments);
  159. };
  160. bc = new BreadCrumb();
  161. // append dummy navigation and controls
  162. // as they are currently used for measurements
  163. $('#testArea').append(
  164. '<div id="controls"></div>'
  165. );
  166. $('#controls').append(bc.$el);
  167. });
  168. afterEach(function() {
  169. BreadCrumb.prototype._updateTotalWidth = oldUpdateTotalWidth;
  170. bc = null;
  171. });
  172. it('Hides breadcrumbs to fit max allowed width', function() {
  173. var $crumbs;
  174. bc.setMaxWidth(500);
  175. // triggers resize implicitly
  176. bc.setDirectory(dummyDir);
  177. $crumbs = bc.$el.find('.crumb');
  178. // first one is always visible
  179. expect($crumbs.eq(0).hasClass('hidden')).toEqual(false);
  180. // second one has ellipsis
  181. expect($crumbs.eq(1).hasClass('hidden')).toEqual(false);
  182. expect($crumbs.eq(1).find('.ellipsis').length).toEqual(1);
  183. // there is only one ellipsis in total
  184. expect($crumbs.find('.ellipsis').length).toEqual(1);
  185. // subsequent elements are hidden
  186. expect($crumbs.eq(2).hasClass('hidden')).toEqual(true);
  187. expect($crumbs.eq(3).hasClass('hidden')).toEqual(true);
  188. expect($crumbs.eq(4).hasClass('hidden')).toEqual(true);
  189. expect($crumbs.eq(5).hasClass('hidden')).toEqual(true);
  190. expect($crumbs.eq(6).hasClass('hidden')).toEqual(false);
  191. expect($crumbs.eq(7).hasClass('hidden')).toEqual(false);
  192. });
  193. it('Updates the breadcrumbs when reducing max allowed width', function() {
  194. var $crumbs;
  195. // enough space
  196. bc.setMaxWidth(1800);
  197. expect(bc.$el.find('.ellipsis').length).toEqual(0);
  198. // triggers resize implicitly
  199. bc.setDirectory(dummyDir);
  200. // simulate increase
  201. bc.setMaxWidth(950);
  202. $crumbs = bc.$el.find('.crumb');
  203. // first one is always visible
  204. expect($crumbs.eq(0).hasClass('hidden')).toEqual(false);
  205. // second one has ellipsis
  206. expect($crumbs.eq(1).hasClass('hidden')).toEqual(false);
  207. expect($crumbs.eq(1).find('.ellipsis').length).toEqual(1);
  208. // there is only one ellipsis in total
  209. expect($crumbs.find('.ellipsis').length).toEqual(1);
  210. // subsequent elements are hidden
  211. expect($crumbs.eq(2).hasClass('hidden')).toEqual(true);
  212. expect($crumbs.eq(3).hasClass('hidden')).toEqual(true);
  213. // the rest is visible
  214. expect($crumbs.eq(4).hasClass('hidden')).toEqual(false);
  215. expect($crumbs.eq(5).hasClass('hidden')).toEqual(false);
  216. expect($crumbs.eq(6).hasClass('hidden')).toEqual(false);
  217. });
  218. it('Removes the ellipsis when there is enough space', function() {
  219. var $crumbs;
  220. bc.setMaxWidth(500);
  221. // triggers resize implicitly
  222. bc.setDirectory(dummyDir);
  223. $crumbs = bc.$el.find('.crumb');
  224. // ellipsis
  225. expect(bc.$el.find('.ellipsis').length).toEqual(1);
  226. // simulate increase
  227. bc.setMaxWidth(1800);
  228. // no ellipsis
  229. expect(bc.$el.find('.ellipsis').length).toEqual(0);
  230. // all are visible
  231. expect($crumbs.eq(0).hasClass('hidden')).toEqual(false);
  232. expect($crumbs.eq(1).hasClass('hidden')).toEqual(false);
  233. expect($crumbs.eq(2).hasClass('hidden')).toEqual(false);
  234. expect($crumbs.eq(3).hasClass('hidden')).toEqual(false);
  235. expect($crumbs.eq(4).hasClass('hidden')).toEqual(false);
  236. expect($crumbs.eq(5).hasClass('hidden')).toEqual(false);
  237. expect($crumbs.eq(6).hasClass('hidden')).toEqual(false);
  238. });
  239. });
  240. });