You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

systemtagsfilelistSpec.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 GNU AGPL version 3 or any later version
  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-container">' +
  30. // init horrible parameters
  31. '<input type="hidden" id="dir" value="/"></input>' +
  32. '<input type="hidden" id="permissions" value="31"></input>' +
  33. '<div id="controls"></div>' +
  34. // dummy table
  35. // TODO: at some point this will be rendered by the fileList class itself!
  36. '<table id="filestable">' +
  37. '<thead><tr>' +
  38. '<th id="headerName" class="hidden column-name">' +
  39. '<input type="checkbox" id="select_all_files" class="select-all">' +
  40. '<a class="name columntitle" data-sort="name"><span>Name</span><span class="sort-indicator"></span></a>' +
  41. '<span class="selectedActions hidden">' +
  42. '</th>' +
  43. '<th class="hidden column-mtime">' +
  44. '<a class="columntitle" data-sort="mtime"><span class="sort-indicator"></span></a>' +
  45. '</th>' +
  46. '</tr></thead>' +
  47. '<tbody id="fileList"></tbody>' +
  48. '<tfoot></tfoot>' +
  49. '</table>' +
  50. '<div id="emptycontent">Empty content message</div>' +
  51. '</div>'
  52. );
  53. });
  54. afterEach(function() {
  55. fileList.destroy();
  56. fileList = undefined;
  57. });
  58. describe('filter field', function() {
  59. var select2Stub, oldCollection, fetchTagsStub;
  60. var $tagsField;
  61. beforeEach(function() {
  62. fetchTagsStub = sinon.stub(OC.SystemTags.SystemTagsCollection.prototype, 'fetch');
  63. select2Stub = sinon.stub($.fn, 'select2');
  64. oldCollection = OC.SystemTags.collection;
  65. OC.SystemTags.collection = new OC.SystemTags.SystemTagsCollection([
  66. {
  67. id: '123',
  68. name: 'abc'
  69. },
  70. {
  71. id: '456',
  72. name: 'def'
  73. }
  74. ]);
  75. fileList = new OCA.SystemTags.FileList(
  76. $('#app-content-container'), {
  77. systemTagIds: []
  78. }
  79. );
  80. $tagsField = fileList.$el.find('[name=tags]');
  81. });
  82. afterEach(function() {
  83. select2Stub.restore();
  84. fetchTagsStub.restore();
  85. OC.SystemTags.collection = oldCollection;
  86. });
  87. it('inits select2 on filter field', function() {
  88. expect(select2Stub.calledOnce).toEqual(true);
  89. });
  90. it('uses global system tags collection', function() {
  91. var callback = sinon.stub();
  92. var opts = select2Stub.firstCall.args[0];
  93. $tagsField.val('123');
  94. opts.initSelection($tagsField, callback);
  95. expect(callback.notCalled).toEqual(true);
  96. expect(fetchTagsStub.calledOnce).toEqual(true);
  97. fetchTagsStub.yieldTo('success', fetchTagsStub.thisValues[0]);
  98. expect(callback.calledOnce).toEqual(true);
  99. expect(callback.lastCall.args[0]).toEqual([
  100. OC.SystemTags.collection.get('123').toJSON()
  101. ]);
  102. });
  103. it('fetches tag list from the global collection', function() {
  104. var callback = sinon.stub();
  105. var opts = select2Stub.firstCall.args[0];
  106. $tagsField.val('123');
  107. opts.query({
  108. term: 'de',
  109. callback: callback
  110. });
  111. expect(fetchTagsStub.calledOnce).toEqual(true);
  112. expect(callback.notCalled).toEqual(true);
  113. fetchTagsStub.yieldTo('success', fetchTagsStub.thisValues[0]);
  114. expect(callback.calledOnce).toEqual(true);
  115. expect(callback.lastCall.args[0]).toEqual({
  116. results: [
  117. OC.SystemTags.collection.get('456').toJSON()
  118. ]
  119. });
  120. });
  121. it('reloads file list after selection', function() {
  122. var reloadStub = sinon.stub(fileList, 'reload');
  123. $tagsField.val('456,123').change();
  124. expect(reloadStub.calledOnce).toEqual(true);
  125. reloadStub.restore();
  126. });
  127. it('updates URL after selection', function() {
  128. var handler = sinon.stub();
  129. fileList.$el.on('changeDirectory', handler);
  130. $tagsField.val('456,123').change();
  131. expect(handler.calledOnce).toEqual(true);
  132. expect(handler.lastCall.args[0].dir).toEqual('456/123');
  133. });
  134. it('updates tag selection when url changed', function() {
  135. fileList.$el.trigger(new $.Event('urlChanged', {dir: '456/123'}));
  136. expect(select2Stub.lastCall.args[0]).toEqual('val');
  137. expect(select2Stub.lastCall.args[1]).toEqual(['456', '123']);
  138. });
  139. });
  140. describe('loading results', function() {
  141. var getFilteredFilesSpec, requestDeferred;
  142. beforeEach(function() {
  143. requestDeferred = new $.Deferred();
  144. getFilteredFilesSpec = sinon.stub(OC.Files.Client.prototype, 'getFilteredFiles')
  145. .returns(requestDeferred.promise());
  146. });
  147. afterEach(function() {
  148. getFilteredFilesSpec.restore();
  149. });
  150. it('renders empty message when no tags were set', function() {
  151. fileList = new OCA.SystemTags.FileList(
  152. $('#app-content-container'), {
  153. systemTagIds: []
  154. }
  155. );
  156. fileList.reload();
  157. expect(fileList.$el.find('#emptycontent').hasClass('hidden')).toEqual(false);
  158. expect(getFilteredFilesSpec.notCalled).toEqual(true);
  159. });
  160. it('render files', function(done) {
  161. fileList = new OCA.SystemTags.FileList(
  162. $('#app-content-container'), {
  163. systemTagIds: ['123', '456']
  164. }
  165. );
  166. var reloading = fileList.reload();
  167. expect(getFilteredFilesSpec.calledOnce).toEqual(true);
  168. expect(getFilteredFilesSpec.lastCall.args[0].systemTagIds).toEqual(['123', '456']);
  169. var testFiles = [new FileInfo({
  170. id: 1,
  171. type: 'file',
  172. name: 'One.txt',
  173. mimetype: 'text/plain',
  174. mtime: 123456789,
  175. size: 12,
  176. etag: 'abc',
  177. permissions: OC.PERMISSION_ALL
  178. }), new FileInfo({
  179. id: 2,
  180. type: 'file',
  181. name: 'Two.jpg',
  182. mimetype: 'image/jpeg',
  183. mtime: 234567890,
  184. size: 12049,
  185. etag: 'def',
  186. permissions: OC.PERMISSION_ALL
  187. }), new FileInfo({
  188. id: 3,
  189. type: 'file',
  190. name: 'Three.pdf',
  191. mimetype: 'application/pdf',
  192. mtime: 234560000,
  193. size: 58009,
  194. etag: '123',
  195. permissions: OC.PERMISSION_ALL
  196. }), new FileInfo({
  197. id: 4,
  198. type: 'dir',
  199. name: 'somedir',
  200. mimetype: 'httpd/unix-directory',
  201. mtime: 134560000,
  202. size: 250,
  203. etag: '456',
  204. permissions: OC.PERMISSION_ALL
  205. })];
  206. requestDeferred.resolve(207, testFiles);
  207. return reloading.then(function() {
  208. expect(fileList.$el.find('#emptycontent').hasClass('hidden')).toEqual(true);
  209. expect(fileList.$el.find('tbody>tr').length).toEqual(4);
  210. }).then(done, done);
  211. });
  212. });
  213. });