systemtagsfilelistSpec.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * Copyright (c) 2016 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author Vincent Petry <vincent@nextcloud.com>
  6. *
  7. * @license AGPL-3.0-or-later
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. describe('OCA.SystemTags.FileList tests', function() {
  24. var FileInfo = OC.Files.FileInfo;
  25. var fileList;
  26. beforeEach(function() {
  27. // init parameters and test table elements
  28. $('#testArea').append(
  29. '<div id="app-content">' +
  30. // init horrible parameters
  31. '<input type="hidden" id="permissions" value="31"></input>' +
  32. '<div class="files-controls"></div>' +
  33. // dummy table
  34. // TODO: at some point this will be rendered by the fileList class itself!
  35. '<table class="files-filestable">' +
  36. '<thead><tr>' +
  37. '<th class="hidden column-name">' +
  38. '<input type="checkbox" id="select_all_files" class="select-all">' +
  39. '<a class="name columntitle" data-sort="name"><span>Name</span><span class="sort-indicator"></span></a>' +
  40. '<span class="selectedActions hidden"></span>' +
  41. '</th>' +
  42. '<th class="hidden column-mtime">' +
  43. '<a class="columntitle" data-sort="mtime"><span class="sort-indicator"></span></a>' +
  44. '</th>' +
  45. '</tr></thead>' +
  46. '<tbody class="files-fileList"></tbody>' +
  47. '<tfoot></tfoot>' +
  48. '</table>' +
  49. '<div class="emptyfilelist emptycontent">Empty content message</div>' +
  50. '</div>'
  51. );
  52. });
  53. afterEach(function() {
  54. fileList.destroy();
  55. fileList = undefined;
  56. });
  57. describe('filter field', function() {
  58. var select2Stub, oldCollection, fetchTagsStub;
  59. var $tagsField;
  60. beforeEach(function() {
  61. fetchTagsStub = sinon.stub(OC.SystemTags.SystemTagsCollection.prototype, 'fetch');
  62. select2Stub = sinon.stub($.fn, 'select2');
  63. oldCollection = OC.SystemTags.collection;
  64. OC.SystemTags.collection = new OC.SystemTags.SystemTagsCollection([
  65. {
  66. id: '123',
  67. name: 'abc'
  68. },
  69. {
  70. id: '456',
  71. name: 'def'
  72. }
  73. ]);
  74. fileList = new OCA.SystemTags.FileList(
  75. $('#app-content'), {
  76. systemTagIds: []
  77. }
  78. );
  79. $tagsField = fileList.$el.find('[name=tags]');
  80. });
  81. afterEach(function() {
  82. select2Stub.restore();
  83. fetchTagsStub.restore();
  84. OC.SystemTags.collection = oldCollection;
  85. });
  86. it('inits select2 on filter field', function() {
  87. expect(select2Stub.calledOnce).toEqual(true);
  88. });
  89. it('uses global system tags collection', function() {
  90. var callback = sinon.stub();
  91. var opts = select2Stub.firstCall.args[0];
  92. $tagsField.val('123');
  93. opts.initSelection($tagsField, callback);
  94. expect(callback.notCalled).toEqual(true);
  95. expect(fetchTagsStub.calledOnce).toEqual(true);
  96. fetchTagsStub.yieldTo('success', fetchTagsStub.thisValues[0]);
  97. expect(callback.calledOnce).toEqual(true);
  98. expect(callback.lastCall.args[0]).toEqual([
  99. OC.SystemTags.collection.get('123').toJSON()
  100. ]);
  101. });
  102. it('fetches tag list from the global collection', function() {
  103. var callback = sinon.stub();
  104. var opts = select2Stub.firstCall.args[0];
  105. $tagsField.val('123');
  106. opts.query({
  107. term: 'de',
  108. callback: callback
  109. });
  110. expect(fetchTagsStub.calledOnce).toEqual(true);
  111. expect(callback.notCalled).toEqual(true);
  112. fetchTagsStub.yieldTo('success', fetchTagsStub.thisValues[0]);
  113. expect(callback.calledOnce).toEqual(true);
  114. expect(callback.lastCall.args[0]).toEqual({
  115. results: [
  116. OC.SystemTags.collection.get('456').toJSON()
  117. ]
  118. });
  119. });
  120. it('reloads file list after selection', function() {
  121. var reloadStub = sinon.stub(fileList, 'reload');
  122. $tagsField.val('456,123').change();
  123. expect(reloadStub.calledOnce).toEqual(true);
  124. reloadStub.restore();
  125. });
  126. it('updates URL after selection', function() {
  127. var handler = sinon.stub();
  128. fileList.$el.on('changeDirectory', handler);
  129. $tagsField.val('456,123').change();
  130. expect(handler.calledOnce).toEqual(true);
  131. expect(handler.lastCall.args[0].dir).toEqual('456/123');
  132. });
  133. it('updates tag selection when url changed', function() {
  134. fileList.$el.trigger(new $.Event('urlChanged', {dir: '456/123'}));
  135. expect(select2Stub.lastCall.args[0]).toEqual('val');
  136. expect(select2Stub.lastCall.args[1]).toEqual(['456', '123']);
  137. });
  138. });
  139. describe('loading results', function() {
  140. var getFilteredFilesSpec, requestDeferred;
  141. beforeEach(function() {
  142. requestDeferred = new $.Deferred();
  143. getFilteredFilesSpec = sinon.stub(OC.Files.Client.prototype, 'getFilteredFiles')
  144. .returns(requestDeferred.promise());
  145. });
  146. afterEach(function() {
  147. getFilteredFilesSpec.restore();
  148. });
  149. it('renders empty message when no tags were set', function() {
  150. fileList = new OCA.SystemTags.FileList(
  151. $('#app-content'), {
  152. systemTagIds: []
  153. }
  154. );
  155. fileList.reload();
  156. expect(fileList.$el.find('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(false);
  157. expect(getFilteredFilesSpec.notCalled).toEqual(true);
  158. });
  159. it('render files', function(done) {
  160. fileList = new OCA.SystemTags.FileList(
  161. $('#app-content'), {
  162. systemTagIds: ['123', '456']
  163. }
  164. );
  165. var reloading = fileList.reload();
  166. expect(getFilteredFilesSpec.calledOnce).toEqual(true);
  167. expect(getFilteredFilesSpec.lastCall.args[0].systemTagIds).toEqual(['123', '456']);
  168. var testFiles = [new FileInfo({
  169. id: 1,
  170. type: 'file',
  171. name: 'One.txt',
  172. mimetype: 'text/plain',
  173. mtime: 123456789,
  174. size: 12,
  175. etag: 'abc',
  176. permissions: OC.PERMISSION_ALL
  177. }), new FileInfo({
  178. id: 2,
  179. type: 'file',
  180. name: 'Two.jpg',
  181. mimetype: 'image/jpeg',
  182. mtime: 234567890,
  183. size: 12049,
  184. etag: 'def',
  185. permissions: OC.PERMISSION_ALL
  186. }), new FileInfo({
  187. id: 3,
  188. type: 'file',
  189. name: 'Three.pdf',
  190. mimetype: 'application/pdf',
  191. mtime: 234560000,
  192. size: 58009,
  193. etag: '123',
  194. permissions: OC.PERMISSION_ALL
  195. }), new FileInfo({
  196. id: 4,
  197. type: 'dir',
  198. name: 'somedir',
  199. mimetype: 'httpd/unix-directory',
  200. mtime: 134560000,
  201. size: 250,
  202. etag: '456',
  203. permissions: OC.PERMISSION_ALL
  204. })];
  205. requestDeferred.resolve(207, testFiles);
  206. return reloading.then(function() {
  207. expect(fileList.$el.find('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(true);
  208. expect(fileList.$el.find('tbody>tr').length).toEqual(4);
  209. }).then(done, done);
  210. });
  211. });
  212. });