jquery.contactsmenuSpec.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. describe('jquery.contactsMenu tests', function() {
  6. var $selector1, $selector2, $appendTo;
  7. beforeEach(function() {
  8. $('#testArea').append($('<div id="selector1">'));
  9. $('#testArea').append($('<div id="selector2">'));
  10. $('#testArea').append($('<div id="appendTo">'));
  11. $selector1 = $('#selector1');
  12. $selector2 = $('#selector2');
  13. $appendTo = $('#appendTo');
  14. });
  15. afterEach(function() {
  16. $selector1.off();
  17. $selector1.remove();
  18. $selector2.off();
  19. $selector2.remove();
  20. $appendTo.remove();
  21. });
  22. describe('shareType', function() {
  23. it('stops if type not supported', function() {
  24. $selector1.contactsMenu('user', 1, $appendTo);
  25. expect($appendTo.children().length).toEqual(0);
  26. $selector1.contactsMenu('user', 2, $appendTo);
  27. expect($appendTo.children().length).toEqual(0);
  28. $selector1.contactsMenu('user', 3, $appendTo);
  29. expect($appendTo.children().length).toEqual(0);
  30. $selector1.contactsMenu('user', 5, $appendTo);
  31. expect($appendTo.children().length).toEqual(0);
  32. });
  33. it('append list if shareType supported', function() {
  34. $selector1.contactsMenu('user', 0, $appendTo);
  35. expect($appendTo.children().length).toEqual(1);
  36. expect($appendTo.html().replace(/[\r\n\t]?(\ \ +)?/g, '')).toEqual('<div class="menu popovermenu menu-left hidden contactsmenu-popover"><ul><li><a><span class="icon-loading-small"></span></a></li></ul></div>');
  37. });
  38. });
  39. describe('open on click', function() {
  40. it('with one selector', function() {
  41. $selector1.contactsMenu('user', 0, $appendTo);
  42. expect($appendTo.children().length).toEqual(1);
  43. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(true);
  44. $selector1.click();
  45. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(false);
  46. });
  47. it('with multiple selectors - 1', function() {
  48. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  49. expect($appendTo.children().length).toEqual(1);
  50. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(true);
  51. $selector1.click();
  52. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(false);
  53. });
  54. it('with multiple selectors - 2', function() {
  55. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  56. expect($appendTo.children().length).toEqual(1);
  57. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(true);
  58. $selector2.click();
  59. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(false);
  60. });
  61. it ('should close when clicking the selector again - 1', function() {
  62. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  63. expect($appendTo.children().length).toEqual(1);
  64. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  65. $selector1.click();
  66. expect($appendTo.find('div').hasClass('hidden')).toEqual(false);
  67. $selector1.click();
  68. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  69. });
  70. it ('should close when clicking the selector again - 1', function() {
  71. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  72. expect($appendTo.children().length).toEqual(1);
  73. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  74. $selector1.click();
  75. expect($appendTo.find('div').hasClass('hidden')).toEqual(false);
  76. $selector2.click();
  77. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  78. });
  79. });
  80. describe('send requests to the server and render', function() {
  81. it('load a topaction only', function(done) {
  82. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  83. $selector1.click();
  84. expect(fakeServer.requests[0].method).toEqual('POST');
  85. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
  86. fakeServer.requests[0].respond(
  87. 200,
  88. { 'Content-Type': 'application/json; charset=utf-8' },
  89. JSON.stringify({
  90. "id": null,
  91. "fullName": "Name 123",
  92. "topAction": {
  93. "title": "bar@baz.wtf",
  94. "icon": "foo.svg",
  95. "hyperlink": "mailto:bar%40baz.wtf"},
  96. "actions": []
  97. })
  98. );
  99. $selector1.on('load', function() {
  100. // FIXME: don't compare HTML one to one but check specific text in the output
  101. expect($appendTo.html().replace(/[\r\n\t]?(\ \ +)?/g, '')).toEqual('<div class="menu popovermenu menu-left contactsmenu-popover loaded" style="display: block;"><ul><li class="hidden"><a><span class="icon-loading-small"></span></a></li><li><a href="mailto:bar%40baz.wtf"><img src="foo.svg"><span>bar@baz.wtf</span></a></li></ul></div>');
  102. done();
  103. });
  104. });
  105. it('load topaction and more actions', function(done) {
  106. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  107. $selector1.click();
  108. fakeServer.requests[0].respond(
  109. 200,
  110. { 'Content-Type': 'application/json; charset=utf-8' },
  111. JSON.stringify({
  112. "id": null,
  113. "fullName": "Name 123",
  114. "topAction": {
  115. "title": "bar@baz.wtf",
  116. "icon": "foo.svg",
  117. "hyperlink": "mailto:bar%40baz.wtf"},
  118. "actions": [{
  119. "title": "Details",
  120. "icon": "details.svg",
  121. "hyperlink": "http:\/\/localhost\/index.php\/apps\/contacts"
  122. }]
  123. })
  124. );
  125. expect(fakeServer.requests[0].method).toEqual('POST');
  126. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
  127. $selector1.on('load', function() {
  128. // FIXME: don't compare HTML one to one but check specific text in the output
  129. expect($appendTo.html().replace(/[\r\n\t]?(\ \ +)?/g, '')).toEqual('<div class="menu popovermenu menu-left contactsmenu-popover loaded" style="display: block;"><ul><li class="hidden"><a><span class="icon-loading-small"></span></a></li><li><a href="mailto:bar%40baz.wtf"><img src="foo.svg"><span>bar@baz.wtf</span></a></li><li><a href="http://localhost/index.php/apps/contacts"><img src="details.svg"><span>Details</span></a></li></ul></div>');
  130. done();
  131. });
  132. });
  133. it('load no actions', function(done) {
  134. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  135. $selector1.click();
  136. fakeServer.requests[0].respond(
  137. 200,
  138. { 'Content-Type': 'application/json; charset=utf-8' },
  139. JSON.stringify({
  140. "id": null,
  141. "fullName": "Name 123",
  142. "topAction": null,
  143. "actions": []
  144. })
  145. );
  146. expect(fakeServer.requests[0].method).toEqual('POST');
  147. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
  148. $selector1.on('load', function() {
  149. // FIXME: don't compare HTML one to one but check specific text in the output
  150. expect($appendTo.html().replace(/[\r\n\t]?(\ \ +)?/g, '')).toEqual('<div class="menu popovermenu menu-left contactsmenu-popover loaded" style="display: block;"><ul><li class="hidden"><a><span class="icon-loading-small"></span></a></li><li><a href="#"><span>No action available</span></a></li></ul></div>');
  151. done();
  152. });
  153. });
  154. it('should throw an error', function(done) {
  155. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  156. $selector1.click();
  157. fakeServer.requests[0].respond(
  158. 400,
  159. { 'Content-Type': 'application/json; charset=utf-8' },
  160. JSON.stringify([])
  161. );
  162. expect(fakeServer.requests[0].method).toEqual('POST');
  163. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
  164. $selector1.on('loaderror', function() {
  165. // FIXME: don't compare HTML one to one but check specific text in the output
  166. expect($appendTo.html().replace(/[\r\n\t]?(\ \ +)?/g, '')).toEqual('<div class="menu popovermenu menu-left contactsmenu-popover loaded" style="display: block;"><ul><li class="hidden"><a><span class="icon-loading-small"></span></a></li><li><a href="#"><span>Error fetching contact actions</span></a></li></ul></div>');
  167. done();
  168. });
  169. });
  170. it('should handle 404', function(done) {
  171. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  172. $selector1.click();
  173. fakeServer.requests[0].respond(
  174. 404,
  175. { 'Content-Type': 'application/json; charset=utf-8' },
  176. JSON.stringify([])
  177. );
  178. expect(fakeServer.requests[0].method).toEqual('POST');
  179. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
  180. $selector1.on('loaderror', function() {
  181. // FIXME: don't compare HTML one to one but check specific text in the output
  182. expect($appendTo.html().replace(/[\r\n\t]?(\ \ +)?/g, '')).toEqual('<div class="menu popovermenu menu-left contactsmenu-popover loaded" style="display: block;"><ul><li class="hidden"><a><span class="icon-loading-small"></span></a></li><li><a href="#"><span>No action available</span></a></li></ul></div>');
  183. done();
  184. });
  185. });
  186. it('click anywhere else to close the menu', function() {
  187. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  188. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  189. $selector1.click();
  190. expect($appendTo.find('div').hasClass('hidden')).toEqual(false);
  191. $(document).click();
  192. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  193. });
  194. });
  195. });