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.

jquery.contactsmenuSpec.js 9.1KB

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