toasterContainerControllerSpec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* global describe global it global beforeEach global angular global jasmine global inject global expect global spyOn */
  2. 'use strict';
  3. var rootScope, toaster, $compile;
  4. describe('toasterContainer controller', function () {
  5. beforeEach(function () {
  6. module('toaster');
  7. // inject the toaster service
  8. inject(function (_toaster_, _$rootScope_, _$compile_) {
  9. toaster = _toaster_;
  10. rootScope = _$rootScope_;
  11. $compile = _$compile_;
  12. });
  13. });
  14. it('should stop timer if config.mouseoverTimer is true', function () {
  15. var container = angular.element(
  16. '<toaster-container toaster-options="{ \'mouseover-timer-stop\': true }"></toaster-container>');
  17. $compile(container)(rootScope);
  18. rootScope.$digest();
  19. var scope = container.scope();
  20. expect(scope.config.mouseoverTimer).toBe(true);
  21. toaster.pop({ type: 'info' });
  22. rootScope.$digest();
  23. expect(scope.toasters[0].timeoutPromise).toBeDefined();
  24. scope.stopTimer(scope.toasters[0]);
  25. rootScope.$digest();
  26. expect(scope.toasters[0].timeoutPromise).toBe(null);
  27. });
  28. it('should do nothing if config.mouseoverTimer is true and stopTimer is called again', function () {
  29. var container = angular.element(
  30. '<toaster-container toaster-options="{ \'mouseover-timer-stop\': true }"></toaster-container>');
  31. $compile(container)(rootScope);
  32. rootScope.$digest();
  33. var scope = container.scope();
  34. expect(scope.config.mouseoverTimer).toBe(true);
  35. toaster.pop({ type: 'info' });
  36. rootScope.$digest();
  37. scope.stopTimer(scope.toasters[0]);
  38. rootScope.$digest();
  39. expect(scope.toasters[0].timeoutPromise).toBe(null);
  40. scope.stopTimer(scope.toasters[0]);
  41. rootScope.$digest();
  42. expect(scope.toasters[0].timeoutPromise).toBe(null);
  43. });
  44. it('should not stop timer if config.mouseoverTimer is false', function () {
  45. var container = angular.element(
  46. '<toaster-container toaster-options="{ \'mouseover-timer-stop\': false }"></toaster-container>');
  47. $compile(container)(rootScope);
  48. rootScope.$digest();
  49. var scope = container.scope();
  50. expect(scope.config.mouseoverTimer).toBe(false);
  51. toaster.pop({ type: 'info' });
  52. rootScope.$digest();
  53. expect(scope.toasters[0].timeoutPromise).toBeDefined();
  54. scope.stopTimer(scope.toasters[0]);
  55. rootScope.$digest();
  56. expect(scope.toasters[0].timeoutPromise).toBeDefined();
  57. });
  58. it('should restart timer if config.mouseoverTimer is true and timeoutPromise is falsy', function () {
  59. var container = angular.element(
  60. '<toaster-container toaster-options="{ \'mouseover-timer-stop\': true }"></toaster-container>');
  61. $compile(container)(rootScope);
  62. rootScope.$digest();
  63. var scope = container.scope();
  64. toaster.pop({ type: 'info' });
  65. rootScope.$digest();
  66. expect(scope.toasters[0].timeoutPromise).toBeDefined();
  67. scope.stopTimer(scope.toasters[0]);
  68. expect(scope.toasters[0].timeoutPromise).toBe(null);
  69. scope.restartTimer(scope.toasters[0]);
  70. expect(scope.toasters[0].timeoutPromise).toBeDefined();
  71. });
  72. it('should not restart timer if config.mouseoverTimer is true and timeoutPromise is truthy', function () {
  73. var container = angular.element(
  74. '<toaster-container toaster-options="{ \'mouseover-timer-stop\': true }"></toaster-container>');
  75. $compile(container)(rootScope);
  76. rootScope.$digest();
  77. var scope = container.scope();
  78. toaster.pop({ type: 'info' });
  79. rootScope.$digest();
  80. expect(scope.toasters[0].timeoutPromise).toBeDefined();
  81. spyOn(scope, 'configureTimer').and.callThrough();
  82. scope.restartTimer(scope.toasters[0]);
  83. expect(scope.toasters[0].timeoutPromise).toBeDefined();
  84. expect(scope.configureTimer).not.toHaveBeenCalled();
  85. });
  86. it('should not restart timer and should remove toast if config.mouseoverTimer is not true and timeoutPromise is null', function () {
  87. var container = angular.element(
  88. '<toaster-container toaster-options="{ \'mouseover-timer-stop\': 2 }"></toaster-container>');
  89. $compile(container)(rootScope);
  90. rootScope.$digest();
  91. var scope = container.scope();
  92. toaster.pop({ type: 'info' });
  93. rootScope.$digest();
  94. expect(scope.config.mouseoverTimer).toBe(2);
  95. scope.toasters[0].timeoutPromise = null;
  96. spyOn(scope, 'configureTimer').and.callThrough();
  97. spyOn(scope, 'removeToast').and.callThrough();
  98. scope.restartTimer(scope.toasters[0]);
  99. expect(scope.configureTimer).not.toHaveBeenCalled();
  100. expect(scope.removeToast).toHaveBeenCalled();
  101. expect(scope.toasters.length).toBe(0)
  102. });
  103. it('should not restart timer or remove toast if config.mouseoverTimer is not true and timeoutPromise is not null', function () {
  104. var container = angular.element(
  105. '<toaster-container toaster-options="{ \'mouseover-timer-stop\': 2 }"></toaster-container>');
  106. $compile(container)(rootScope);
  107. rootScope.$digest();
  108. var scope = container.scope();
  109. toaster.pop({ type: 'info' });
  110. rootScope.$digest();
  111. expect(scope.config.mouseoverTimer).toBe(2);
  112. spyOn(scope, 'configureTimer').and.callThrough();
  113. spyOn(scope, 'removeToast').and.callThrough();
  114. scope.restartTimer(scope.toasters[0]);
  115. expect(scope.configureTimer).not.toHaveBeenCalled();
  116. expect(scope.removeToast).not.toHaveBeenCalled();
  117. expect(scope.toasters.length).toBe(1)
  118. });
  119. describe('click', function () {
  120. it('should do nothing if config.tap is not true and toast.showCloseButton is not true', function () {
  121. var container = angular.element(
  122. '<toaster-container toaster-options="{ \'tap-to-dismiss\': false, \'close-button\': false }"></toaster-container>');
  123. $compile(container)(rootScope);
  124. rootScope.$digest();
  125. var scope = container.scope();
  126. spyOn(scope, 'removeToast').and.callThrough();
  127. toaster.pop({ type: 'info' });
  128. rootScope.$digest();
  129. scope.click(scope.toasters[0]);
  130. expect(scope.toasters.length).toBe(1);
  131. expect(scope.removeToast).not.toHaveBeenCalled();
  132. });
  133. it('should do nothing if config.tap is not true and toast.showCloseButton is true', function () {
  134. var container = angular.element(
  135. '<toaster-container toaster-options="{ \'tap-to-dismiss\': false, \'close-button\': true }"></toaster-container>');
  136. $compile(container)(rootScope);
  137. rootScope.$digest();
  138. var scope = container.scope();
  139. spyOn(scope, 'removeToast').and.callThrough();
  140. toaster.pop({ type: 'info' });
  141. rootScope.$digest();
  142. scope.click(scope.toasters[0]);
  143. expect(scope.toasters.length).toBe(1);
  144. expect(scope.removeToast).not.toHaveBeenCalled();
  145. });
  146. it('should do nothing if config.tap is not true and isCloseButton is not true', function () {
  147. var container = angular.element(
  148. '<toaster-container toaster-options="{ \'tap-to-dismiss\': false, \'close-button\': true }"></toaster-container>');
  149. $compile(container)(rootScope);
  150. rootScope.$digest();
  151. var scope = container.scope();
  152. spyOn(scope, 'removeToast').and.callThrough();
  153. toaster.pop({ type: 'info' });
  154. rootScope.$digest();
  155. scope.click(scope.toasters[0], false);
  156. expect(scope.toasters.length).toBe(1);
  157. expect(scope.removeToast).not.toHaveBeenCalled();
  158. });
  159. it('should remove toast if config.tap is true', function () {
  160. var container = angular.element(
  161. '<toaster-container toaster-options="{ \'tap-to-dismiss\': true, \'close-button\': true }"></toaster-container>');
  162. $compile(container)(rootScope);
  163. rootScope.$digest();
  164. var scope = container.scope();
  165. spyOn(scope, 'removeToast').and.callThrough();
  166. toaster.pop({ type: 'info' });
  167. rootScope.$digest();
  168. scope.click(scope.toasters[0]);
  169. expect(scope.toasters.length).toBe(0);
  170. expect(scope.removeToast).toHaveBeenCalled();
  171. });
  172. it('should remove toast if config.tap is true and the click handler function returns true', function () {
  173. var container = angular.element(
  174. '<toaster-container toaster-options="{ \'tap-to-dismiss\': true, \'close-button\': true }"></toaster-container>');
  175. $compile(container)(rootScope);
  176. rootScope.$digest();
  177. var scope = container.scope();
  178. spyOn(scope, 'removeToast').and.callThrough();
  179. toaster.pop({ type: 'info', clickHandler: function (toast, isCloseButton) { return true; } });
  180. rootScope.$digest();
  181. scope.click(scope.toasters[0]);
  182. expect(scope.toasters.length).toBe(0);
  183. expect(scope.removeToast).toHaveBeenCalled();
  184. });
  185. it('should not remove toast if config.tap is true and the click handler function does not return true', function () {
  186. var container = angular.element(
  187. '<toaster-container toaster-options="{ \'tap-to-dismiss\': true, \'close-button\': true }"></toaster-container>');
  188. $compile(container)(rootScope);
  189. rootScope.$digest();
  190. var scope = container.scope();
  191. spyOn(scope, 'removeToast').and.callThrough();
  192. toaster.pop({ type: 'info', clickHandler: function (toast, isCloseButton) { } });
  193. rootScope.$digest();
  194. scope.click(scope.toasters[0]);
  195. expect(scope.toasters.length).toBe(1);
  196. expect(scope.removeToast).not.toHaveBeenCalled();
  197. });
  198. it('should remove toast if config.tap is true and the click handler exists on the parent returning true', function () {
  199. var container = angular.element(
  200. '<toaster-container toaster-options="{ \'tap-to-dismiss\': true, \'close-button\': true }"></toaster-container>');
  201. $compile(container)(rootScope);
  202. rootScope.$digest();
  203. var scope = container.scope();
  204. scope.$parent.clickHandler = function () { return true; };
  205. spyOn(scope, 'removeToast').and.callThrough();
  206. toaster.pop({ type: 'info', clickHandler: 'clickHandler' });
  207. rootScope.$digest();
  208. scope.click(scope.toasters[0]);
  209. expect(scope.toasters.length).toBe(0);
  210. expect(scope.removeToast).toHaveBeenCalled();
  211. });
  212. it('should not remove toast if config.tap is true and the click handler exists on the parent not returning true', function () {
  213. var container = angular.element(
  214. '<toaster-container toaster-options="{ \'tap-to-dismiss\': true, \'close-button\': true }"></toaster-container>');
  215. $compile(container)(rootScope);
  216. rootScope.$digest();
  217. var scope = container.scope();
  218. scope.$parent.clickHandler = function () { };
  219. spyOn(scope, 'removeToast').and.callThrough();
  220. toaster.pop({ type: 'info', clickHandler: 'clickHandler' });
  221. rootScope.$digest();
  222. scope.click(scope.toasters[0]);
  223. expect(scope.toasters.length).toBe(1);
  224. expect(scope.removeToast).not.toHaveBeenCalled();
  225. });
  226. it('should remove toast if config.tap is true and the click handler does not exist on the parent', function () {
  227. // TODO: this functionality seems counter-intuitive.
  228. // Need to identify use cases to see if this is actually correct.
  229. var container = angular.element(
  230. '<toaster-container toaster-options="{ \'tap-to-dismiss\': true, \'close-button\': true }"></toaster-container>');
  231. $compile(container)(rootScope);
  232. rootScope.$digest();
  233. var scope = container.scope();
  234. spyOn(scope, 'removeToast').and.callThrough();
  235. console.log = jasmine.createSpy("log");
  236. toaster.pop({ type: 'info', clickHandler: 'clickHandler' });
  237. rootScope.$digest();
  238. scope.click(scope.toasters[0]);
  239. expect(scope.toasters.length).toBe(0);
  240. expect(scope.removeToast).toHaveBeenCalled();
  241. expect(console.log).toHaveBeenCalledWith("TOAST-NOTE: Your click handler is not inside a parent scope of toaster-container.");
  242. });
  243. });
  244. });