commentstabviewSpec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2016 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. * comment 3 of the License, or any later comment.
  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.Comments.CommentsTabView tests', function() {
  22. var view, fileInfoModel;
  23. var fetchStub;
  24. var testComments;
  25. var clock;
  26. /**
  27. * Creates a dummy message with the given length
  28. *
  29. * @param {int} len length
  30. * @return {string} message
  31. */
  32. function createMessageWithLength(len) {
  33. var bigMessage = '';
  34. for (var i = 0; i < len; i++) {
  35. bigMessage += 'a';
  36. }
  37. return bigMessage;
  38. }
  39. beforeEach(function() {
  40. clock = sinon.useFakeTimers(Date.UTC(2016, 1, 3, 10, 5, 9));
  41. fetchStub = sinon.stub(OCA.Comments.CommentCollection.prototype, 'fetchNext');
  42. view = new OCA.Comments.CommentsTabView();
  43. fileInfoModel = new OCA.Files.FileInfoModel({
  44. id: 5,
  45. name: 'One.txt',
  46. mimetype: 'text/plain',
  47. permissions: 31,
  48. path: '/subdir',
  49. size: 123456789,
  50. etag: 'abcdefg',
  51. mtime: Date.UTC(2016, 1, 0, 0, 0, 0)
  52. });
  53. view.render();
  54. var comment1 = new OCA.Comments.CommentModel({
  55. id: 1,
  56. actorType: 'users',
  57. actorId: 'user1',
  58. actorDisplayName: 'User One',
  59. objectType: 'files',
  60. objectId: 5,
  61. message: 'First',
  62. creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 0)).toUTCString()
  63. });
  64. var comment2 = new OCA.Comments.CommentModel({
  65. id: 2,
  66. actorType: 'users',
  67. actorId: 'user2',
  68. actorDisplayName: 'User Two',
  69. objectType: 'files',
  70. objectId: 5,
  71. message: 'Second\nNewline',
  72. creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 0, 0)).toUTCString()
  73. });
  74. var comment3 = new OCA.Comments.CommentModel({
  75. id: 3,
  76. actorId: 'anotheruser',
  77. actorDisplayName: 'Another User',
  78. actorType: 'users',
  79. verb: 'comment',
  80. message: 'Hail to thee, @macbeth. Yours faithfully, @banquo',
  81. creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 9)).toUTCString(),
  82. mentions: {
  83. 0: {
  84. mentionDisplayName: "Thane of Cawdor",
  85. mentionId: "macbeth",
  86. mentionTye: "user"
  87. },
  88. 1: {
  89. mentionDisplayName: "Lord Banquo",
  90. mentionId: "banquo",
  91. mentionTye: "user"
  92. }
  93. }
  94. });
  95. testComments = [comment1, comment2, comment3];
  96. });
  97. afterEach(function() {
  98. view.remove();
  99. view = undefined;
  100. fetchStub.restore();
  101. clock.restore();
  102. });
  103. describe('rendering', function() {
  104. it('reloads matching comments when setting file info model', function() {
  105. view.setFileInfo(fileInfoModel);
  106. expect(fetchStub.calledOnce).toEqual(true);
  107. });
  108. it('renders loading icon while fetching comments', function() {
  109. view.setFileInfo(fileInfoModel);
  110. view.collection.trigger('request');
  111. expect(view.$el.find('.loading').length).toEqual(1);
  112. expect(view.$el.find('.comments li').length).toEqual(0);
  113. });
  114. it('renders comments', function() {
  115. view.setFileInfo(fileInfoModel);
  116. view.collection.set(testComments);
  117. var $comments = view.$el.find('.comments>li');
  118. expect($comments.length).toEqual(3);
  119. var $item = $comments.eq(0);
  120. expect($item.find('.author').text()).toEqual('User One');
  121. expect($item.find('.date').text()).toEqual('seconds ago');
  122. expect($item.find('.message').text()).toEqual('First');
  123. $item = $comments.eq(1);
  124. expect($item.find('.author').text()).toEqual('User Two');
  125. expect($item.find('.date').text()).toEqual('5 minutes ago');
  126. expect($item.find('.message').html()).toEqual('Second<br>Newline');
  127. });
  128. it('renders comments from deleted user differently', function() {
  129. testComments[0].set('actorType', 'deleted_users', {silent: true});
  130. view.collection.set(testComments);
  131. var $item = view.$el.find('.comment[data-id=1]');
  132. expect($item.find('.author').text()).toEqual('[Deleted user]');
  133. expect($item.find('.avatar').attr('data-username')).not.toBeDefined();
  134. });
  135. it('renders mentioned user id to avatar and displayname', function() {
  136. view.collection.set(testComments);
  137. var $comment = view.$el.find('.comment[data-id=3] .message');
  138. expect($comment.length).toEqual(1);
  139. expect($comment.find('.avatar[data-user=macbeth]').length).toEqual(1);
  140. expect($comment.find('strong:first').text()).toEqual('Thane of Cawdor');
  141. expect($comment.find('.avatar[data-user=banquo]').length).toEqual(1);
  142. expect($comment.find('strong:last-child').text()).toEqual('Lord Banquo');
  143. });
  144. });
  145. describe('more comments', function() {
  146. var hasMoreResultsStub;
  147. beforeEach(function() {
  148. view.collection.set(testComments);
  149. hasMoreResultsStub = sinon.stub(OCA.Comments.CommentCollection.prototype, 'hasMoreResults');
  150. });
  151. afterEach(function() {
  152. hasMoreResultsStub.restore();
  153. });
  154. it('shows "More comments" button when more comments are available', function() {
  155. hasMoreResultsStub.returns(true);
  156. view.collection.trigger('sync');
  157. expect(view.$el.find('.showMore').hasClass('hidden')).toEqual(false);
  158. });
  159. it('does not show "More comments" button when more comments are available', function() {
  160. hasMoreResultsStub.returns(false);
  161. view.collection.trigger('sync');
  162. expect(view.$el.find('.showMore').hasClass('hidden')).toEqual(true);
  163. });
  164. it('fetches and appends the next page when clicking the "More" button', function() {
  165. hasMoreResultsStub.returns(true);
  166. expect(fetchStub.notCalled).toEqual(true);
  167. view.$el.find('.showMore').click();
  168. expect(fetchStub.calledOnce).toEqual(true);
  169. });
  170. it('appends comment to the list when added to collection', function() {
  171. var comment4 = new OCA.Comments.CommentModel({
  172. id: 4,
  173. actorType: 'users',
  174. actorId: 'user3',
  175. actorDisplayName: 'User Three',
  176. objectType: 'files',
  177. objectId: 5,
  178. message: 'Third',
  179. creationDateTime: new Date(Date.UTC(2016, 1, 3, 5, 0, 0)).toUTCString()
  180. });
  181. view.collection.add(comment4);
  182. expect(view.$el.find('.comments>li').length).toEqual(4);
  183. var $item = view.$el.find('.comments>li').eq(3);
  184. expect($item.find('.author').text()).toEqual('User Three');
  185. expect($item.find('.date').text()).toEqual('5 hours ago');
  186. expect($item.find('.message').html()).toEqual('Third');
  187. });
  188. });
  189. describe('posting comments', function() {
  190. var createStub;
  191. var currentUserStub;
  192. beforeEach(function() {
  193. view.collection.set(testComments);
  194. createStub = sinon.stub(OCA.Comments.CommentCollection.prototype, 'create');
  195. currentUserStub = sinon.stub(OC, 'getCurrentUser');
  196. currentUserStub.returns({
  197. uid: 'testuser',
  198. displayName: 'Test User'
  199. });
  200. });
  201. afterEach(function() {
  202. createStub.restore();
  203. currentUserStub.restore();
  204. });
  205. it('creates a new comment when clicking post button', function() {
  206. view.$el.find('.message').val('New message');
  207. view.$el.find('form').submit();
  208. expect(createStub.calledOnce).toEqual(true);
  209. expect(createStub.lastCall.args[0]).toEqual({
  210. actorId: 'testuser',
  211. actorDisplayName: 'Test User',
  212. actorType: 'users',
  213. verb: 'comment',
  214. message: 'New message',
  215. creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 9)).toUTCString()
  216. });
  217. });
  218. it('does not create a comment if the field is empty', function() {
  219. view.$el.find('.message').val(' ');
  220. view.$el.find('form').submit();
  221. expect(createStub.notCalled).toEqual(true);
  222. });
  223. it('does not create a comment if the field length is too large', function() {
  224. var bigMessage = '';
  225. for (var i = 0; i < view._commentMaxLength * 2; i++) {
  226. bigMessage += 'a';
  227. }
  228. view.$el.find('.message').val(bigMessage);
  229. view.$el.find('form').submit();
  230. expect(createStub.notCalled).toEqual(true);
  231. });
  232. describe('limit indicator', function() {
  233. var tooltipStub;
  234. var $message;
  235. var $submitButton;
  236. beforeEach(function() {
  237. tooltipStub = sinon.stub($.fn, 'tooltip');
  238. $message = view.$el.find('.message');
  239. $submitButton = view.$el.find('.submit');
  240. });
  241. afterEach(function() {
  242. tooltipStub.restore();
  243. });
  244. it('does not displays tooltip when limit is far away', function() {
  245. $message.val(createMessageWithLength(3));
  246. $message.trigger('change');
  247. expect(tooltipStub.calledWith('show')).toEqual(false);
  248. expect($submitButton.prop('disabled')).toEqual(false);
  249. expect($message.hasClass('error')).toEqual(false);
  250. });
  251. it('displays tooltip when limit is almost reached', function() {
  252. $message.val(createMessageWithLength(view._commentMaxLength - 2));
  253. $message.trigger('change');
  254. expect(tooltipStub.calledWith('show')).toEqual(true);
  255. expect($submitButton.prop('disabled')).toEqual(false);
  256. expect($message.hasClass('error')).toEqual(false);
  257. });
  258. it('displays tooltip and disabled button when limit is exceeded', function() {
  259. $message.val(createMessageWithLength(view._commentMaxLength + 2));
  260. $message.trigger('change');
  261. expect(tooltipStub.calledWith('show')).toEqual(true);
  262. expect($submitButton.prop('disabled')).toEqual(true);
  263. expect($message.hasClass('error')).toEqual(true);
  264. });
  265. });
  266. });
  267. describe('editing comments', function() {
  268. var saveStub;
  269. var fetchStub;
  270. var avatarStub;
  271. var currentUserStub;
  272. beforeEach(function() {
  273. saveStub = sinon.stub(OCA.Comments.CommentModel.prototype, 'save');
  274. fetchStub = sinon.stub(OCA.Comments.CommentModel.prototype, 'fetch');
  275. avatarStub = sinon.stub($.fn, 'avatar');
  276. currentUserStub = sinon.stub(OC, 'getCurrentUser');
  277. currentUserStub.returns({
  278. uid: 'testuser',
  279. displayName: 'Test User'
  280. });
  281. view.collection.add({
  282. id: 1,
  283. actorId: 'testuser',
  284. actorDisplayName: 'Test User',
  285. actorType: 'users',
  286. verb: 'comment',
  287. message: 'New message',
  288. creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 9)).toUTCString()
  289. });
  290. view.collection.add({
  291. id: 2,
  292. actorId: 'anotheruser',
  293. actorDisplayName: 'Another User',
  294. actorType: 'users',
  295. verb: 'comment',
  296. message: 'New message from another user',
  297. creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 9)).toUTCString(),
  298. });
  299. });
  300. afterEach(function() {
  301. saveStub.restore();
  302. fetchStub.restore();
  303. avatarStub.restore();
  304. currentUserStub.restore();
  305. });
  306. it('shows edit link for owner comments', function() {
  307. var $comment = view.$el.find('.comment[data-id=1]');
  308. expect($comment.length).toEqual(1);
  309. expect($comment.find('.action.edit').length).toEqual(1);
  310. });
  311. it('does not show edit link for other user\'s comments', function() {
  312. var $comment = view.$el.find('.comment[data-id=2]');
  313. expect($comment.length).toEqual(1);
  314. expect($comment.find('.action.edit').length).toEqual(0);
  315. });
  316. it('shows edit form when clicking edit', function() {
  317. var $comment = view.$el.find('.comment[data-id=1]');
  318. $comment.find('.action.edit').click();
  319. expect($comment.hasClass('hidden')).toEqual(true);
  320. var $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
  321. expect($formRow.length).toEqual(1);
  322. });
  323. it('saves message and updates comment item when clicking save', function() {
  324. var $comment = view.$el.find('.comment[data-id=1]');
  325. $comment.find('.action.edit').click();
  326. var $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
  327. expect($formRow.length).toEqual(1);
  328. $formRow.find('textarea').val('modified message');
  329. $formRow.find('form').submit();
  330. expect(saveStub.calledOnce).toEqual(true);
  331. expect(saveStub.lastCall.args[0]).toEqual({
  332. message: 'modified message'
  333. });
  334. var model = view.collection.get(1);
  335. // simulate the fact that save sets the attribute
  336. model.set('message', 'modified\nmessage');
  337. saveStub.yieldTo('success', model);
  338. expect(fetchStub.calledOnce).toEqual(true);
  339. fetchStub.yieldTo('success', model);
  340. // original comment element is visible again
  341. expect($comment.hasClass('hidden')).toEqual(false);
  342. // and its message was updated
  343. expect($comment.find('.message').html()).toEqual('modified<br>message');
  344. // form row is gone
  345. $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
  346. expect($formRow.length).toEqual(0);
  347. });
  348. it('restores original comment when cancelling', function() {
  349. var $comment = view.$el.find('.comment[data-id=1]');
  350. $comment.find('.action.edit').click();
  351. var $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
  352. expect($formRow.length).toEqual(1);
  353. $formRow.find('textarea').val('modified\nmessage');
  354. $formRow.find('.cancel').click();
  355. expect(saveStub.notCalled).toEqual(true);
  356. // original comment element is visible again
  357. expect($comment.hasClass('hidden')).toEqual(false);
  358. // and its message was not updated
  359. expect($comment.find('.message').html()).toEqual('New message');
  360. // form row is gone
  361. $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
  362. expect($formRow.length).toEqual(0);
  363. });
  364. it('destroys model when clicking delete', function() {
  365. var destroyStub = sinon.stub(OCA.Comments.CommentModel.prototype, 'destroy');
  366. var $comment = view.$el.find('.comment[data-id=1]');
  367. $comment.find('.action.edit').click();
  368. var $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
  369. expect($formRow.length).toEqual(1);
  370. $formRow.find('.delete').click();
  371. expect(destroyStub.calledOnce).toEqual(true);
  372. expect(destroyStub.thisValues[0].id).toEqual(1);
  373. destroyStub.yieldTo('success');
  374. // original comment element is gone
  375. $comment = view.$el.find('.comment[data-id=1]');
  376. expect($comment.length).toEqual(0);
  377. // form row is gone
  378. $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
  379. expect($formRow.length).toEqual(0);
  380. destroyStub.restore();
  381. });
  382. it('does not submit comment if the field is empty', function() {
  383. var $comment = view.$el.find('.comment[data-id=1]');
  384. $comment.find('.action.edit').click();
  385. $comment.find('.message').val(' ');
  386. $comment.find('form').submit();
  387. expect(saveStub.notCalled).toEqual(true);
  388. });
  389. it('does not submit comment if the field length is too large', function() {
  390. var $comment = view.$el.find('.comment[data-id=1]');
  391. $comment.find('.action.edit').click();
  392. $comment.find('.message').val(createMessageWithLength(view._commentMaxLength * 2));
  393. $comment.find('form').submit();
  394. expect(saveStub.notCalled).toEqual(true);
  395. });
  396. });
  397. describe('read marker', function() {
  398. var updateMarkerStub;
  399. beforeEach(function() {
  400. updateMarkerStub = sinon.stub(OCA.Comments.CommentCollection.prototype, 'updateReadMarker');
  401. });
  402. afterEach(function() {
  403. updateMarkerStub.restore();
  404. });
  405. it('resets the read marker after REPORT', function() {
  406. testComments[0].set('isUnread', true, {silent: true});
  407. testComments[1].set('isUnread', true, {silent: true});
  408. view.collection.set(testComments);
  409. view.collection.trigger('sync', 'REPORT');
  410. expect(updateMarkerStub.calledOnce).toEqual(true);
  411. expect(updateMarkerStub.lastCall.args[0]).toBeFalsy();
  412. });
  413. it('does not reset the read marker if there was no unread comments', function() {
  414. view.collection.set(testComments);
  415. view.collection.trigger('sync', 'REPORT');
  416. expect(updateMarkerStub.notCalled).toEqual(true);
  417. });
  418. it('does not reset the read marker when posting comments', function() {
  419. testComments[0].set('isUnread', true, {silent: true});
  420. testComments[1].set('isUnread', true, {silent: true});
  421. view.collection.set(testComments);
  422. view.collection.trigger('sync', 'POST');
  423. expect(updateMarkerStub.notCalled).toEqual(true);
  424. });
  425. });
  426. });