jquery.contactsmenuSpec.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com>
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. describe('jquery.contactsMenu tests', function() {
  11. var $selector1, $selector2, $appendTo;
  12. beforeEach(function() {
  13. $('#testArea').append($('<div id="selector1">'));
  14. $('#testArea').append($('<div id="selector2">'));
  15. $('#testArea').append($('<div id="appendTo">'));
  16. $selector1 = $('#selector1');
  17. $selector2 = $('#selector2');
  18. $appendTo = $('#appendTo');
  19. });
  20. afterEach(function() {
  21. $selector1.remove();
  22. $selector2.remove();
  23. $appendTo.remove();
  24. });
  25. describe('shareType', function() {
  26. it('stops if type not supported', function() {
  27. $selector1.contactsMenu('user', 1, $appendTo);
  28. expect($appendTo.children().length).toEqual(0);
  29. $selector1.contactsMenu('user', 2, $appendTo);
  30. expect($appendTo.children().length).toEqual(0);
  31. $selector1.contactsMenu('user', 3, $appendTo);
  32. expect($appendTo.children().length).toEqual(0);
  33. $selector1.contactsMenu('user', 5, $appendTo);
  34. expect($appendTo.children().length).toEqual(0);
  35. });
  36. it('append list if shareType supported', function() {
  37. $selector1.contactsMenu('user', 0, $appendTo);
  38. expect($appendTo.children().length).toEqual(1);
  39. 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>');
  40. });
  41. });
  42. describe('open on click', function() {
  43. it('with one selector', function() {
  44. $selector1.contactsMenu('user', 0, $appendTo);
  45. expect($appendTo.children().length).toEqual(1);
  46. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(true);
  47. $selector1.click();
  48. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(false);
  49. });
  50. it('with multiple selectors - 1', function() {
  51. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  52. expect($appendTo.children().length).toEqual(1);
  53. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(true);
  54. $selector1.click();
  55. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(false);
  56. });
  57. it('with multiple selectors - 2', function() {
  58. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  59. expect($appendTo.children().length).toEqual(1);
  60. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(true);
  61. $selector2.click();
  62. expect($appendTo.find('div.contactsmenu-popover').hasClass('hidden')).toEqual(false);
  63. });
  64. it ('should close when clicking the selector again - 1', function() {
  65. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  66. expect($appendTo.children().length).toEqual(1);
  67. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  68. $selector1.click();
  69. expect($appendTo.find('div').hasClass('hidden')).toEqual(false);
  70. $selector1.click();
  71. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  72. });
  73. it ('should close when clicking the selector again - 1', function() {
  74. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  75. expect($appendTo.children().length).toEqual(1);
  76. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  77. $selector1.click();
  78. expect($appendTo.find('div').hasClass('hidden')).toEqual(false);
  79. $selector2.click();
  80. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  81. });
  82. });
  83. describe('send requests to the server and render', function() {
  84. it('load a topaction only', function() {
  85. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  86. $selector1.click();
  87. fakeServer.requests[0].respond(
  88. 200,
  89. { 'Content-Type': 'application/json; charset=utf-8' },
  90. JSON.stringify({
  91. "id": null,
  92. "fullName": "Name 123",
  93. "topAction": {
  94. "title": "bar@baz.wtf",
  95. "icon": "foo.svg",
  96. "hyperlink": "mailto:bar%40baz.wtf"},
  97. "actions": []
  98. })
  99. );
  100. expect(fakeServer.requests[0].method).toEqual('POST');
  101. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
  102. 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>');
  103. });
  104. it('load topaction and more actions', function() {
  105. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  106. $selector1.click();
  107. fakeServer.requests[0].respond(
  108. 200,
  109. { 'Content-Type': 'application/json; charset=utf-8' },
  110. JSON.stringify({
  111. "id": null,
  112. "fullName": "Name 123",
  113. "topAction": {
  114. "title": "bar@baz.wtf",
  115. "icon": "foo.svg",
  116. "hyperlink": "mailto:bar%40baz.wtf"},
  117. "actions": [{
  118. "title": "Details",
  119. "icon": "details.svg",
  120. "hyperlink": "http:\/\/localhost\/index.php\/apps\/contacts"
  121. }]
  122. })
  123. );
  124. expect(fakeServer.requests[0].method).toEqual('POST');
  125. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
  126. 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>');
  127. });
  128. it('load no actions', function() {
  129. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  130. $selector1.click();
  131. fakeServer.requests[0].respond(
  132. 200,
  133. { 'Content-Type': 'application/json; charset=utf-8' },
  134. JSON.stringify({
  135. "id": null,
  136. "fullName": "Name 123",
  137. "topAction": null,
  138. "actions": []
  139. })
  140. );
  141. expect(fakeServer.requests[0].method).toEqual('POST');
  142. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
  143. 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>');
  144. });
  145. it('should throw an error', function() {
  146. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  147. $selector1.click();
  148. fakeServer.requests[0].respond(
  149. 400,
  150. { 'Content-Type': 'application/json; charset=utf-8' },
  151. JSON.stringify([])
  152. );
  153. expect(fakeServer.requests[0].method).toEqual('POST');
  154. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
  155. 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>');
  156. });
  157. it('should handle 404', function() {
  158. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  159. $selector1.click();
  160. fakeServer.requests[0].respond(
  161. 404,
  162. { 'Content-Type': 'application/json; charset=utf-8' },
  163. JSON.stringify([])
  164. );
  165. expect(fakeServer.requests[0].method).toEqual('POST');
  166. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
  167. 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>');
  168. });
  169. });
  170. it('click anywhere else to close the menu', function() {
  171. $('#selector1, #selector2').contactsMenu('user', 0, $appendTo);
  172. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  173. $selector1.click();
  174. expect($appendTo.find('div').hasClass('hidden')).toEqual(false);
  175. $(document).click();
  176. expect($appendTo.find('div').hasClass('hidden')).toEqual(true);
  177. });
  178. });