Alert.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. 'use strict';
  2. var common = require('../common');
  3. describe('Alert', function () {
  4. describe('showStatus', function () {
  5. before(function () {
  6. cleanup();
  7. });
  8. jsc.property(
  9. 'shows a status message',
  10. jsc.array(common.jscAlnumString()),
  11. jsc.array(common.jscAlnumString()),
  12. function (icon, message) {
  13. icon = icon.join('');
  14. message = message.join('');
  15. var expected = '<div id="status" role="alert" ' +
  16. 'class="statusmessage alert alert-info"><span ' +
  17. 'class="glyphicon glyphicon-' + icon +
  18. '" aria-hidden="true"></span> ' + message + '</div>';
  19. $('body').html(
  20. '<div id="status" role="alert" class="statusmessage ' +
  21. 'alert alert-info hidden"><span class="glyphicon ' +
  22. 'glyphicon-info-sign" aria-hidden="true"></span> </div>'
  23. );
  24. $.PrivateBin.Alert.init();
  25. $.PrivateBin.Alert.showStatus(message, icon);
  26. var result = $('body').html();
  27. return expected === result;
  28. }
  29. );
  30. });
  31. describe('showError', function () {
  32. before(function () {
  33. cleanup();
  34. });
  35. jsc.property(
  36. 'shows an error message',
  37. jsc.array(common.jscAlnumString()),
  38. jsc.array(common.jscAlnumString()),
  39. function (icon, message) {
  40. icon = icon.join('');
  41. message = message.join('');
  42. var expected = '<div id="errormessage" role="alert" ' +
  43. 'class="statusmessage alert alert-danger"><span ' +
  44. 'class="glyphicon glyphicon-' + icon +
  45. '" aria-hidden="true"></span> ' + message + '</div>';
  46. $('body').html(
  47. '<div id="errormessage" role="alert" class="statusmessage ' +
  48. 'alert alert-danger hidden"><span class="glyphicon ' +
  49. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  50. );
  51. $.PrivateBin.Alert.init();
  52. $.PrivateBin.Alert.showError(message, icon);
  53. var result = $('body').html();
  54. return expected === result;
  55. }
  56. );
  57. });
  58. describe('showRemaining', function () {
  59. before(function () {
  60. cleanup();
  61. });
  62. jsc.property(
  63. 'shows remaining time',
  64. jsc.array(common.jscAlnumString()),
  65. jsc.array(common.jscAlnumString()),
  66. 'integer',
  67. function (message, string, number) {
  68. message = message.join('');
  69. string = string.join('');
  70. var expected = '<div id="remainingtime" role="alert" ' +
  71. 'class="alert alert-info"><span ' +
  72. 'class="glyphicon glyphicon-fire" aria-hidden="true">' +
  73. '</span> ' + string + message + number + '</div>';
  74. $('body').html(
  75. '<div id="remainingtime" role="alert" class="hidden ' +
  76. 'alert alert-info"><span class="glyphicon ' +
  77. 'glyphicon-fire" aria-hidden="true"></span> </div>'
  78. );
  79. $.PrivateBin.Alert.init();
  80. $.PrivateBin.Alert.showRemaining(['%s' + message + '%d', string, number]);
  81. var result = $('body').html();
  82. return expected === result;
  83. }
  84. );
  85. });
  86. describe('showLoading', function () {
  87. before(function () {
  88. cleanup();
  89. });
  90. jsc.property(
  91. 'shows a loading message',
  92. jsc.array(common.jscAlnumString()),
  93. jsc.array(common.jscAlnumString()),
  94. function (message, icon) {
  95. message = message.join('');
  96. icon = icon.join('');
  97. var defaultMessage = 'Loading…';
  98. if (message.length === 0) {
  99. message = defaultMessage;
  100. }
  101. var expected = '<ul class="nav navbar-nav"><li ' +
  102. 'id="loadingindicator" class="navbar-text"><span ' +
  103. 'class="glyphicon glyphicon-' + icon +
  104. '" aria-hidden="true"></span> ' + message + '</li></ul>';
  105. $('body').html(
  106. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  107. 'class="navbar-text hidden"><span class="glyphicon ' +
  108. 'glyphicon-time" aria-hidden="true"></span> ' +
  109. defaultMessage + '</li></ul>'
  110. );
  111. $.PrivateBin.Alert.init();
  112. $.PrivateBin.Alert.showLoading(message, icon);
  113. var result = $('body').html();
  114. return expected === result;
  115. }
  116. );
  117. });
  118. describe('hideLoading', function () {
  119. before(function () {
  120. cleanup();
  121. });
  122. it(
  123. 'hides the loading message',
  124. function() {
  125. $('body').html(
  126. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  127. 'class="navbar-text"><span class="glyphicon ' +
  128. 'glyphicon-time" aria-hidden="true"></span> ' +
  129. 'Loading…</li></ul>'
  130. );
  131. $('body').addClass('loading');
  132. $.PrivateBin.Alert.init();
  133. $.PrivateBin.Alert.hideLoading();
  134. return !$('body').hasClass('loading') &&
  135. $('#loadingindicator').hasClass('hidden');
  136. }
  137. );
  138. });
  139. describe('hideMessages', function () {
  140. before(function () {
  141. cleanup();
  142. });
  143. it(
  144. 'hides all messages',
  145. function() {
  146. $('body').html(
  147. '<div id="status" role="alert" class="statusmessage ' +
  148. 'alert alert-info"><span class="glyphicon ' +
  149. 'glyphicon-info-sign" aria-hidden="true"></span> </div>' +
  150. '<div id="errormessage" role="alert" class="statusmessage ' +
  151. 'alert alert-danger"><span class="glyphicon ' +
  152. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  153. );
  154. $.PrivateBin.Alert.init();
  155. $.PrivateBin.Alert.hideMessages();
  156. return $('#statusmessage').hasClass('hidden') &&
  157. $('#errormessage').hasClass('hidden');
  158. }
  159. );
  160. });
  161. describe('setCustomHandler', function () {
  162. before(function () {
  163. cleanup();
  164. });
  165. jsc.property(
  166. 'calls a given handler function',
  167. 'nat 3',
  168. jsc.array(common.jscAlnumString()),
  169. function (trigger, message) {
  170. message = message.join('');
  171. var handlerCalled = false,
  172. defaultMessage = 'Loading…',
  173. functions = [
  174. $.PrivateBin.Alert.showStatus,
  175. $.PrivateBin.Alert.showError,
  176. $.PrivateBin.Alert.showRemaining,
  177. $.PrivateBin.Alert.showLoading
  178. ];
  179. if (message.length === 0) {
  180. message = defaultMessage;
  181. }
  182. $('body').html(
  183. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  184. 'class="navbar-text hidden"><span class="glyphicon ' +
  185. 'glyphicon-time" aria-hidden="true"></span> ' +
  186. defaultMessage + '</li></ul>' +
  187. '<div id="remainingtime" role="alert" class="hidden ' +
  188. 'alert alert-info"><span class="glyphicon ' +
  189. 'glyphicon-fire" aria-hidden="true"></span> </div>' +
  190. '<div id="status" role="alert" class="statusmessage ' +
  191. 'alert alert-info"><span class="glyphicon ' +
  192. 'glyphicon-info-sign" aria-hidden="true"></span> </div>' +
  193. '<div id="errormessage" role="alert" class="statusmessage ' +
  194. 'alert alert-danger"><span class="glyphicon ' +
  195. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  196. );
  197. $.PrivateBin.Alert.init();
  198. $.PrivateBin.Alert.setCustomHandler(function(id, $element) {
  199. handlerCalled = true;
  200. return jsc.random(0, 1) ? true : $element;
  201. });
  202. functions[trigger](message);
  203. return handlerCalled;
  204. }
  205. );
  206. });
  207. });