1
0

filesummarySpec.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. describe('OCA.Files.FileSummary tests', function() {
  7. var FileSummary = OCA.Files.FileSummary;
  8. var $container;
  9. beforeEach(function() {
  10. $container = $('<table><tr></tr></table>').find('tr');
  11. });
  12. afterEach(function() {
  13. $container = null;
  14. });
  15. it('renders summary as text', function() {
  16. var s = new FileSummary($container);
  17. s.setSummary({
  18. totalDirs: 5,
  19. totalFiles: 2,
  20. totalSize: 256000
  21. });
  22. expect($container.hasClass('hidden')).toEqual(false);
  23. expect($container.find('.dirinfo').text()).toEqual('5 folders');
  24. expect($container.find('.fileinfo').text()).toEqual('2 files');
  25. expect($container.find('.filesize').text()).toEqual('250 KB');
  26. });
  27. it('hides summary when no files or folders', function() {
  28. var s = new FileSummary($container);
  29. s.setSummary({
  30. totalDirs: 0,
  31. totalFiles: 0,
  32. totalSize: 0
  33. });
  34. expect($container.hasClass('hidden')).toEqual(true);
  35. });
  36. it('increases summary when adding files', function() {
  37. var s = new FileSummary($container);
  38. s.setSummary({
  39. totalDirs: 5,
  40. totalFiles: 2,
  41. totalSize: 256000
  42. });
  43. s.add({type: 'file', size: 256000});
  44. s.add({type: 'dir', size: 100});
  45. s.update();
  46. expect($container.hasClass('hidden')).toEqual(false);
  47. expect($container.find('.dirinfo').text()).toEqual('6 folders');
  48. expect($container.find('.fileinfo').text()).toEqual('3 files');
  49. expect($container.find('.filesize').text()).toEqual('500 KB');
  50. expect(s.summary.totalDirs).toEqual(6);
  51. expect(s.summary.totalFiles).toEqual(3);
  52. expect(s.summary.totalSize).toEqual(512100);
  53. });
  54. it('decreases summary when removing files', function() {
  55. var s = new FileSummary($container);
  56. s.setSummary({
  57. totalDirs: 5,
  58. totalFiles: 2,
  59. totalSize: 256000
  60. });
  61. s.remove({type: 'file', size: 128000});
  62. s.remove({type: 'dir', size: 100});
  63. s.update();
  64. expect($container.hasClass('hidden')).toEqual(false);
  65. expect($container.find('.dirinfo').text()).toEqual('4 folders');
  66. expect($container.find('.fileinfo').text()).toEqual('1 file');
  67. expect($container.find('.filesize').text()).toEqual('125 KB');
  68. expect(s.summary.totalDirs).toEqual(4);
  69. expect(s.summary.totalFiles).toEqual(1);
  70. expect(s.summary.totalSize).toEqual(127900);
  71. });
  72. it('renders filtered summary as text', function() {
  73. var s = new FileSummary($container);
  74. s.setSummary({
  75. totalDirs: 5,
  76. totalFiles: 2,
  77. totalSize: 256000,
  78. filter: 'foo'
  79. });
  80. expect($container.hasClass('hidden')).toEqual(false);
  81. expect($container.find('.dirinfo').text()).toEqual('5 folders');
  82. expect($container.find('.fileinfo').text()).toEqual('2 files');
  83. expect($container.find('.filter').text()).toEqual(' match "foo"');
  84. expect($container.find('.filesize').text()).toEqual('250 KB');
  85. });
  86. it('hides filtered summary when no files or folders', function() {
  87. var s = new FileSummary($container);
  88. s.setSummary({
  89. totalDirs: 0,
  90. totalFiles: 0,
  91. totalSize: 0,
  92. filter: 'foo'
  93. });
  94. expect($container.hasClass('hidden')).toEqual(true);
  95. });
  96. it('increases filtered summary when adding files', function() {
  97. var s = new FileSummary($container);
  98. s.setSummary({
  99. totalDirs: 5,
  100. totalFiles: 2,
  101. totalSize: 256000,
  102. filter: 'foo'
  103. });
  104. s.add({name: 'bar.txt', type: 'file', size: 256000});
  105. s.add({name: 'foo.txt', type: 'file', size: 256001});
  106. s.add({name: 'bar', type: 'dir', size: 100});
  107. s.add({name: 'foo', type: 'dir', size: 102});
  108. s.update();
  109. expect($container.hasClass('hidden')).toEqual(false);
  110. expect($container.find('.dirinfo').text()).toEqual('6 folders');
  111. expect($container.find('.fileinfo').text()).toEqual('3 files');
  112. expect($container.find('.filter').text()).toEqual(' match "foo"');
  113. expect($container.find('.filesize').text()).toEqual('500 KB');
  114. expect(s.summary.totalDirs).toEqual(6);
  115. expect(s.summary.totalFiles).toEqual(3);
  116. expect(s.summary.totalSize).toEqual(512103);
  117. });
  118. it('decreases filtered summary when removing files', function() {
  119. var s = new FileSummary($container);
  120. s.setSummary({
  121. totalDirs: 5,
  122. totalFiles: 2,
  123. totalSize: 256000,
  124. filter: 'foo'
  125. });
  126. s.remove({name: 'bar.txt', type: 'file', size: 128000});
  127. s.remove({name: 'foo.txt', type: 'file', size: 127999});
  128. s.remove({name: 'bar', type: 'dir', size: 100});
  129. s.remove({name: 'foo', type: 'dir', size: 98});
  130. s.update();
  131. expect($container.hasClass('hidden')).toEqual(false);
  132. expect($container.find('.dirinfo').text()).toEqual('4 folders');
  133. expect($container.find('.fileinfo').text()).toEqual('1 file');
  134. expect($container.find('.filter').text()).toEqual(' match "foo"');
  135. expect($container.find('.filesize').text()).toEqual('125 KB');
  136. expect(s.summary.totalDirs).toEqual(4);
  137. expect(s.summary.totalFiles).toEqual(1);
  138. expect(s.summary.totalSize).toEqual(127903);
  139. });
  140. it('properly sum up pending folder sizes after adding', function() {
  141. var s = new FileSummary($container);
  142. s.setSummary({
  143. totalDirs: 0,
  144. totalFiles: 0,
  145. totalSize: 0
  146. });
  147. s.add({type: 'dir', size: -1});
  148. s.update();
  149. expect($container.hasClass('hidden')).toEqual(false);
  150. expect($container.find('.dirinfo').text()).toEqual('1 folder');
  151. expect($container.find('.fileinfo').hasClass('hidden')).toEqual(true);
  152. expect($container.find('.filesize').text()).toEqual('Pending');
  153. expect(s.summary.totalDirs).toEqual(1);
  154. expect(s.summary.totalFiles).toEqual(0);
  155. expect(s.summary.totalSize).toEqual(0);
  156. });
  157. it('properly sum up pending folder sizes after remove', function() {
  158. var s = new FileSummary($container);
  159. s.setSummary({
  160. totalDirs: 0,
  161. totalFiles: 0,
  162. totalSize: 0
  163. });
  164. s.add({type: 'dir', size: -1});
  165. s.remove({type: 'dir', size: -1});
  166. s.update();
  167. expect($container.hasClass('hidden')).toEqual(true);
  168. expect(s.summary.totalDirs).toEqual(0);
  169. expect(s.summary.totalFiles).toEqual(0);
  170. expect(s.summary.totalSize).toEqual(0);
  171. });
  172. describe('hidden files', function() {
  173. var config;
  174. var summary;
  175. beforeEach(function() {
  176. config = new OC.Backbone.Model();
  177. summary = new FileSummary($container, {
  178. config: config
  179. });
  180. });
  181. it('renders hidden count section when hidden files are hidden', function() {
  182. window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: false });
  183. summary.add({name: 'abc', type: 'file', size: 256000});
  184. summary.add({name: 'def', type: 'dir', size: 100});
  185. summary.add({name: '.hidden', type: 'dir', size: 512000});
  186. summary.update();
  187. expect($container.hasClass('hidden')).toEqual(false);
  188. expect($container.find('.dirinfo').text()).toEqual('2 folders');
  189. expect($container.find('.fileinfo').text()).toEqual('1 file');
  190. expect($container.find('.hiddeninfo').hasClass('hidden')).toEqual(false);
  191. expect($container.find('.hiddeninfo').text()).toEqual(' (including 1 hidden)');
  192. expect($container.find('.filesize').text()).toEqual('750 KB');
  193. });
  194. it('does not render hidden count section when hidden files exist but are visible', function() {
  195. window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: true });
  196. summary.add({name: 'abc', type: 'file', size: 256000});
  197. summary.add({name: 'def', type: 'dir', size: 100});
  198. summary.add({name: '.hidden', type: 'dir', size: 512000});
  199. summary.update();
  200. expect($container.hasClass('hidden')).toEqual(false);
  201. expect($container.find('.dirinfo').text()).toEqual('2 folders');
  202. expect($container.find('.fileinfo').text()).toEqual('1 file');
  203. expect($container.find('.hiddeninfo').hasClass('hidden')).toEqual(true);
  204. expect($container.find('.filesize').text()).toEqual('750 KB');
  205. });
  206. it('does not render hidden count section when no hidden files exist', function() {
  207. window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: false });
  208. summary.add({name: 'abc', type: 'file', size: 256000});
  209. summary.add({name: 'def', type: 'dir', size: 100});
  210. summary.update();
  211. expect($container.hasClass('hidden')).toEqual(false);
  212. expect($container.find('.dirinfo').text()).toEqual('1 folder');
  213. expect($container.find('.fileinfo').text()).toEqual('1 file');
  214. expect($container.find('.hiddeninfo').hasClass('hidden')).toEqual(true);
  215. expect($container.find('.filesize').text()).toEqual('250 KB');
  216. });
  217. });
  218. });