comunicationLeap/node_modules/toastr-master/tests/unit/toastr-tests.js
node_modules/toastr-master/tests/unit/toastr-tests.jsRaw
1/// <reference path="../../../toastr.js" />
2/// <reference path="../qunit/qunit.js" />
3(function () {
4 var iconClasses = {
5 error: 'toast-error',
6 info: 'toast-info',
7 success: 'toast-success',
8 warning: 'toast-warning'
9 };
10 var positionClasses = {
11 topRight: 'toast-top-right',
12 bottomRight: 'toast-bottom-right',
13 bottomLeft: 'toast-bottom-left',
14 topLeft: 'toast-top-left',
15 topCenter: 'toast-top-center',
16 bottomCenter: 'toast-bottom-center'
17 };
18 var sampleMsg = 'I don\'t think they really exist';
19 var sampleTitle = 'ROUS';
20 var selectors = {
21 container: 'div#toast-container',
22 toastInfo: 'div#toast-container > div.toast-info',
23 toastWarning: 'div#toast-container > div.toast-success',
24 toastError: 'div#toast-container > div.toast-error',
25 toastSuccess: 'div#toast-container > div.toast-success'
26 };
27
28 toastr.options = {
29 timeOut: 2000,
30 extendedTimeOut: 0,
31 fadeOut: 0,
32 fadeIn: 0,
33 showDuration: 0,
34 hideDuration: 0,
35 debug: false
36 };
37
38 var delay = toastr.options.timeOut + 500;
39
40 // 'Clears' must go first
41 module('clear');
42 asyncTest('clear - show 3 toasts, clear the 2nd', 1, function () {
43 //Arrange
44 var $toast = [];
45 $toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
46 $toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
47 $toast[2] = toastr.info(sampleMsg, sampleTitle + '-3');
48 var $container = toastr.getContainer();
49 //Act
50 toastr.clear($toast[1]);
51 //Assert
52 setTimeout(function () {
53 //debugger;
54 //console.log($container.children().length);
55 ok($container && $container.children().length === 2);
56 //Teardown
57 resetContainer();
58 start();
59 }, 1000);
60 });
61 asyncTest('clear - show 3 toasts, clear all 3, 0 left', 1, function () {
62 //Arrange
63 var $toast = [];
64 $toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
65 $toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
66 $toast[2] = toastr.info(sampleMsg, sampleTitle + '-3');
67 var $container = toastr.getContainer();
68 //Act
69 toastr.clear();
70 //Assert
71 setTimeout(function () {
72 ok($container && $container.children().length === 0);
73 //Teardown
74 resetContainer();
75 start();
76 }, delay);
77 });
78 test('clear - after clear with force option toast with focus disappears', 1, function () {
79 //Arrange
80 var $toast;
81 var msg = sampleMsg + '<br/><br/><button type="button">Clear</button>';
82 //Act
83 $toast = toastr.info(msg, sampleTitle + '-1');
84 $toast.find('button').focus();
85 toastr.clear($toast, { force: true });
86 var $container = toastr.getContainer();
87 //Assert
88 ok($container && $container.children().length === 0, 'Focused toast after a clear with force is not visible');
89 //Teardown
90 resetContainer();
91 });
92 asyncTest('clear and show - show 2 toasts, clear both, then show 1 more', 2, function () {
93 //Arrange
94 var $toast = [];
95 $toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
96 $toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
97 var $container = toastr.getContainer();
98 toastr.clear();
99 //Act
100 setTimeout(function () {
101 $toast[2] = toastr.info(sampleMsg, sampleTitle + '-3-Visible');
102 //Assert
103 equal($toast[2].find('div.toast-title').html(), sampleTitle + '-3-Visible', 'Finds toast after a clear');
104 ok($toast[2].is(':visible'), 'Toast after a clear is visible');
105 //Teardown
106 resetContainer();
107 start();
108 }, delay);
109 });
110 asyncTest('clear and show - clear removes toast container', 2, function () {
111 //Arrange
112 var $toast = [];
113 $toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
114 $toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
115 var $container = toastr.getContainer();
116 toastr.clear();
117 //Act
118 setTimeout(function () {
119 //Assert
120 equal($(selectors.container).length, 0, 'Toast container does not exist');
121 ok(!$toast[1].is(':visible'), 'Toast after a clear is visible');
122 //Teardown
123 resetContainer();
124 start();
125 }, delay);
126 });
127 asyncTest('clear and show - after clear new toast creates container', 1, function () {
128 //Arrange
129 var $toast = [];
130 $toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
131 $toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
132 var $container = toastr.getContainer();
133 toastr.clear();
134 //Act
135 setTimeout(function () {
136 $toast[2] = toastr.info(sampleMsg, sampleTitle + '-3-Visible');
137 //Assert
138 equal($(selectors.container).find('div.toast-title').html(), sampleTitle + '-3-Visible', 'Finds toast after a clear'); //Teardown
139 resetContainer();
140 start();
141 }, delay);
142 });
143 test('clear and show - after clear all toasts new toast still appears', 1, function () {
144 //Arrange
145 var $toast = [];
146 //Act
147 $toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
148 $toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
149 toastr.clear();
150 $toast[2] = toastr.info(sampleMsg, sampleTitle + '-3-Visible');
151 //Assert
152 ok($toast[2].is(':visible'), 'Toast after a clear is visible');
153 //Teardown
154 resetContainer();
155 });
156 module('info');
157 test('info - pass title and message', 3, function () {
158 //Arrange
159 //Act
160 var $toast = toastr.info(sampleMsg, sampleTitle);
161 //Assert
162 equal($toast.find('div.toast-title').html(), sampleTitle, 'Sets title');
163 equal($toast.find('div.toast-message').html(), sampleMsg, 'Sets message');
164 ok($toast.hasClass(iconClasses.info), 'Sets info icon');
165 //Teardown
166 $toast.remove();
167 clearContainerChildren();
168 });
169 test('info - pass message, but no title', 3, function () {
170 //Arrange
171 //Act
172 var $toast = toastr.info(sampleMsg);
173 //Assert
174 equal($toast.find('div.toast-title').length, 0, 'Sets null title');
175 equal($toast.find('div.toast-message').html(), sampleMsg, 'Sets message');
176 ok($toast.hasClass(iconClasses.info), 'Sets info icon');
177 //Teardown
178 $toast.remove();
179 clearContainerChildren();
180 });
181 test('info - pass no message nor title', 3, function () {
182 //Arrange
183 //Act
184 var $toast = toastr.info(); //Assert
185 equal($toast.find('div.toast-title').length, 0, 'Sets null title');
186 equal($toast.find('div.toast-message').html(), null, 'Sets message');
187 ok($toast.hasClass(iconClasses.info), 'Sets info icon');
188 //Teardown
189 $toast.remove();
190 clearContainerChildren();
191 });
192 module('warning');
193 test('warning - pass message and title', 3, function () {
194 //Arrange
195 //Act
196 var $toast = toastr.warning(sampleMsg, sampleTitle);
197 //Assert
198 equal($toast.find('div.toast-title').html(), sampleTitle, 'Sets title');
199 equal($toast.find('div.toast-message').html(), sampleMsg, 'Sets message');
200 ok($toast.hasClass(iconClasses.warning), 'Sets warning icon');
201 //Teardown
202 $toast.remove();
203 clearContainerChildren();
204 });
205 test('warning - pass message, but no title', 3, function () {
206 //Arrange
207 //Act
208 var $toast = toastr.warning(sampleMsg);
209 //Assert
210 equal($toast.find('div.toast-title').length, 0, 'Sets empty title');
211 equal($toast.find('div.toast-message').html(), sampleMsg, 'Sets message');
212 ok($toast.hasClass(iconClasses.warning), 'Sets warning icon');
213 //Teardown
214 $toast.remove();
215 clearContainerChildren();
216 });
217 test('warning - no message nor title', 3, function () {
218 //Arrange
219 //Act
220 var $toast = toastr.warning('');
221 //Assert
222 equal($toast.find('div.toast-title').length, 0, 'Sets null title');
223 equal($toast.find('div.toast-message').length, 0, 'Sets empty message');
224 ok($toast.hasClass(iconClasses.warning), 'Sets warning icon');
225 //Teardown
226 $toast.remove();
227 clearContainerChildren();
228 });
229 module('error');
230 test('error - pass message and title', 3, function () {
231 //Arrange
232 //Act
233 var $toast = toastr.error(sampleMsg, sampleTitle);
234 //Assert
235 equal($toast.find('div.toast-title').html(), sampleTitle, 'Sets title');
236 equal($toast.find('div.toast-message').html(), sampleMsg, 'Sets message');
237 ok($toast.hasClass(iconClasses.error), 'Sets error icon');
238 //Teardown
239 $toast.remove();
240 clearContainerChildren();
241 });
242 test('error - pass message, but no title', 3, function () {
243 //Arrange
244 //Act
245 var $toast = toastr.error(sampleMsg); //Assert
246 equal($toast.find('div.toast-title').length, 0, 'Sets empty title');
247 equal($toast.find('div.toast-message').html(), sampleMsg, 'Sets message');
248 ok($toast.hasClass(iconClasses.error), 'Sets error icon');
249 //Teardown
250 $toast.remove();
251 clearContainerChildren();
252 });
253 test('error - no message nor title', 3, function () {
254 //Arrange
255 //Act
256 var $toast = toastr.error('');
257 //Assert
258 equal($toast.find('div.toast-title').length, 0, 'Sets empty title');
259 equal($toast.find('div.toast-message').length, 0, 'Sets empty message');
260 ok($toast.hasClass(iconClasses.error), 'Sets error icon');
261 //Teardown
262 $toast.remove();
263 clearContainerChildren();
264 });
265 module('success');
266 test('success - pass message and title', 3, function () {
267 //Arrange
268 //Act
269 var $toast = toastr.success(sampleMsg, sampleTitle);
270 //Assert
271 equal($toast.find('div.toast-title').html(), sampleTitle, 'Sets title');
272 equal($toast.find('div.toast-message').html(), sampleMsg, 'Sets message');
273 ok($toast.hasClass(iconClasses.success), 'Sets success icon');
274 //Teardown
275 $toast.remove();
276 clearContainerChildren();
277 });
278 test('success - pass message, but no title', 3, function () {
279 //Arrange
280 //Act
281 var $toast = toastr.success(sampleMsg);
282 //Assert
283 equal($toast.find('div.toast-title').length, 0, 'Sets empty title');
284 equal($toast.find('div.toast-message').html(), sampleMsg, 'Sets message');
285 ok($toast.hasClass(iconClasses.success), 'Sets success icon');
286 //Teardown
287 $toast.remove();
288 clearContainerChildren();
289 });
290 test('success - no message nor title', 3, function () {
291 //Arrange
292 //Act
293 var $toast = toastr.success('');
294 //Assert
295 equal($toast.find('div.toast-title').length, 0, 'Sets null title');
296 equal($toast.find('div.toast-message').length, 0, 'Sets empty message');
297 ok($toast.hasClass(iconClasses.success), 'Sets success icon'); //Teardown
298 $toast.remove();
299 clearContainerChildren();
300 });
301
302
303 module('escape html', {
304 teardown: function () {
305 toastr.options.escapeHtml = false;
306 }
307 });
308 test('info - escape html', 2, function () {
309 //Arrange
310 toastr.options.escapeHtml = true;
311 //Act
312 var $toast = toastr.info('html <strong>message</strong>', 'html <u>title</u>');
313 //Assert
314 equal($toast.find('div.toast-title').html(), 'html <u>title</u>', 'Title is escaped');
315 equal($toast.find('div.toast-message').html(), 'html <strong>message</strong>', 'Message is escaped');
316 //Teardown
317 $toast.remove();
318 clearContainerChildren();
319 });
320 test('warning - escape html', 2, function () {
321 //Arrange
322 toastr.options.escapeHtml = true;
323 //Act
324 var $toast = toastr.warning('html <strong>message</strong>', 'html <u>title</u>');
325 //Assert
326 equal($toast.find('div.toast-title').html(), 'html <u>title</u>', 'Title is escaped');
327 equal($toast.find('div.toast-message').html(), 'html <strong>message</strong>', 'Message is escaped');
328 //Teardown
329 $toast.remove();
330 clearContainerChildren();
331 });
332 test('error - escape html', 2, function () {
333 //Arrange
334 toastr.options.escapeHtml = true;
335 //Act
336 var $toast = toastr.error('html <strong>message</strong>', 'html <u>title</u>');
337 //Assert
338 equal($toast.find('div.toast-title').html(), 'html <u>title</u>', 'Title is escaped');
339 equal($toast.find('div.toast-message').html(), 'html <strong>message</strong>', 'Message is escaped');
340 //Teardown
341 $toast.remove();
342 clearContainerChildren();
343 });
344 test('success - escape html', 2, function () {
345 //Arrange
346 toastr.options.escapeHtml = true;
347 //Act
348 var $toast = toastr.success('html <strong>message</strong>', 'html <u>title</u>');
349 //Assert
350 equal($toast.find('div.toast-title').html(), 'html <u>title</u>', 'Title is escaped');
351 equal($toast.find('div.toast-message').html(), 'html <strong>message</strong>', 'Message is escaped');
352 //Teardown
353 $toast.remove();
354 clearContainerChildren();
355 });
356
357 module('closeButton', {
358 teardown: function () {
359 toastr.options.closeButton = false;
360 }
361 });
362 test('close button disabled', 1, function () {
363 //Arrange
364 toastr.options.closeButton = false;
365 //Act
366 var $toast = toastr.success('');
367 //Assert
368 equal($toast.find('button.toast-close-button').length, 0, 'close button should not exist with closeButton=false');
369 //Teardown
370 $toast.remove();
371 clearContainerChildren();
372 });
373 test('close button enabled', 1, function () {
374 //Arrange
375 toastr.options.closeButton = true;
376 //Act
377 var $toast = toastr.success('');
378 //Assert
379 equal($toast.find('button.toast-close-button').length, 1, 'close button should exist with closeButton=true');
380 //Teardown
381 $toast.remove();
382 clearContainerChildren();
383 });
384 test('close button has type=button', 1, function () {
385 //Arrange
386 toastr.options.closeButton = true;
387 //Act
388 var $toast = toastr.success('');
389 //Assert
390 equal($toast.find('button[type="button"].toast-close-button').length, 1, 'close button should have type=button');
391 //Teardown
392 $toast.remove();
393 clearContainerChildren();
394 });
395 asyncTest('close button duration', 1, function () {
396 //Arrange
397 toastr.options.closeButton = true;
398 toastr.options.closeDuration = 0;
399 toastr.options.hideDuration = 2000;
400 var $container = toastr.getContainer();
401 //Act
402 var $toast = toastr.success('');
403 $toast.find('button.toast-close-button').click();
404 setTimeout(function () {
405 //Assert
406 ok($container && $container.children().length === 0, 'close button should support own hide animation');
407 //Teardown
408 toastr.options.hideDuration = 0;
409 resetContainer();
410 start();
411 }, 500);
412 });
413
414 module('progressBar', {
415 teardown: function () {
416 toastr.options.progressBar = false;
417 }
418 });
419 test('progress bar disabled', 1, function () {
420 //Arrange
421 toastr.options.progressBar = false;
422 //Act
423 var $toast = toastr.success('');
424 //Assert
425 equal($toast.find('div.toast-progress').length, 0, 'progress bar should not exist with progressBar=false');
426 //Teardown
427 $toast.remove();
428 clearContainerChildren();
429 });
430 test('progress bar enabled', 1, function () {
431 //Arrange
432 toastr.options.progressBar = true;
433 //Act
434 var $toast = toastr.success('');
435 //Assert
436 equal($toast.find('div.toast-progress').length, 1, 'progress bar should exist with progressBar=true');
437 //Teardown
438 $toast.remove();
439 clearContainerChildren();
440 });
441
442 module('event');
443 asyncTest('event - onShown is executed', 1, function () {
444 // Arrange
445 var run = false;
446 var onShown = function () { run = true; };
447 toastr.options.onShown = onShown;
448 // Act
449 var $toast = toastr.success(sampleMsg, sampleTitle);
450 setTimeout(function () {
451 // Assert
452 ok(run);
453 //Teardown
454 $toast.remove();
455 clearContainerChildren();
456 start();
457 }, delay);
458 });
459
460 asyncTest('event - onHidden is executed', 1, function () {
461 //Arrange
462 var run = false;
463 var onHidden = function () { run = true; };
464 toastr.options.onHidden = onHidden;
465 toastr.options.timeOut = 1;
466 //Act
467 var $toast = toastr.success(sampleMsg, sampleTitle);
468 setTimeout(function () {
469 // Assert
470 ok(run); //Teardown
471 $toast.remove();
472 clearContainerChildren();
473 start();
474 }, delay);
475 });
476
477 asyncTest('event - onShown and onHidden are both executed', 2, function () {
478 //Arrange
479 var onShowRun = false;
480 var onHideRun = false;
481 var onShow = function () { onShowRun = true; };
482 var onHide = function () { onHideRun = true; };
483 toastr.options.onShown = onShow;
484 toastr.options.onHidden = onHide;
485 toastr.options.timeOut = 1;
486 //Act
487 var $toast = toastr.success(sampleMsg, sampleTitle);
488 setTimeout(function () {
489 // Assert
490 ok(onShowRun);
491 ok(onHideRun);
492 //Teardown
493 $toast.remove();
494 clearContainerChildren();
495 start();
496 }, delay);
497 });
498
499 test('event - message appears when no show or hide method functions provided', 1, function () {
500 //Arrange
501 //Act
502 var $toast = toastr.success(sampleMsg, sampleTitle);
503 //Assert
504 ok($toast.hasClass(iconClasses.success), 'Sets success icon');
505 //Teardown
506 $toast.remove();
507 clearContainerChildren();
508 });
509
510 test('event - prevent duplicate sequential toasts.', 1, function(){
511 toastr.options.preventDuplicates = true;
512
513 var $toast = [];
514 $toast[0] = toastr.info(sampleMsg, sampleTitle);
515 $toast[1] = toastr.info(sampleMsg, sampleTitle);
516 $toast[2] = toastr.info(sampleMsg + " 1", sampleTitle);
517 $toast[3] = toastr.info(sampleMsg, sampleTitle);
518 var $container = toastr.getContainer();
519
520 ok($container && $container.children().length === 3);
521
522 clearContainerChildren();
523 });
524
525 test('event - prevent duplicate sequential toasts, but allow previous after clear.', 1, function(){
526 toastr.options.preventDuplicates = true;
527
528 var $toast = [];
529 $toast[0] = toastr.info(sampleMsg, sampleTitle);
530 $toast[1] = toastr.info(sampleMsg, sampleTitle);
531 clearContainerChildren();
532 $toast[3] = toastr.info(sampleMsg, sampleTitle);
533 var $container = toastr.getContainer();
534
535 ok($container && $container.children().length === 1);
536
537 clearContainerChildren();
538 });
539
540 test('event - allow duplicate sequential toasts.', 1, function(){
541 toastr.options.preventDuplicates = false;
542
543 var $toast = [];
544 $toast[0] = toastr.info(sampleMsg, sampleTitle);
545 $toast[1] = toastr.info(sampleMsg, sampleTitle);
546 $toast[1] = toastr.info(sampleMsg, sampleTitle);
547 var $container = toastr.getContainer();
548
549 ok($container && $container.children().length === 3);
550
551 clearContainerChildren();
552 });
553
554 test('event - allow preventDuplicates option to be overridden.', 1, function() {
555 var $toast = [];
556
557 $toast[0] = toastr.info(sampleMsg, sampleTitle, {
558 preventDuplicates: true
559 });
560 $toast[1] = toastr.info(sampleMsg, sampleTitle, {
561 preventDuplicates: true
562 });
563 $toast[2] = toastr.info(sampleMsg, sampleTitle);
564 var $container = toastr.getContainer();
565
566 ok($container && $container.children().length === 2);
567 clearContainerChildren();
568 });
569
570 module('order of appearance');
571 test('Newest toast on top', 1, function () {
572 //Arrange
573 resetContainer();
574 toastr.options.newestOnTop = true;
575 //Act
576 var $first = toastr.success("First toast");
577 var $second = toastr.success("Second toast");
578 //Assert
579 var containerHtml = toastr.getContainer().html();
580 ok(containerHtml.indexOf("First toast") > containerHtml.indexOf("Second toast"), 'Newest toast is on top');
581 //Teardown
582 $first.remove();
583 $second.remove();
584 resetContainer();
585 });
586
587 test('Oldest toast on top', 1, function () {
588 //Arrange
589 resetContainer();
590 toastr.options.newestOnTop = false;
591 //Act
592 var $first = toastr.success("First toast");
593 var $second = toastr.success("Second toast");
594 //Assert
595 var containerHtml = toastr.getContainer().html();
596 ok(containerHtml.indexOf("First toast") < containerHtml.indexOf("Second toast"), 'Oldest toast is on top');
597 //Teardown
598 $first.remove();
599 $second.remove();
600 resetContainer();
601 });
602
603 // These must go last
604 module('positioning');
605 test('Container - position top-right', 1, function () {
606 //Arrange
607 resetContainer();
608 toastr.options.positionClass = positionClasses.topRight;
609 //Act
610 var $toast = toastr.success(sampleMsg);
611 var $container = toastr.getContainer();
612 //Assert
613 ok($container.hasClass(positionClasses.topRight), 'Has position top right');
614 //Teardown
615 $toast.remove();
616 resetContainer();
617 });
618 test('Container - position bottom-right', 1, function () {
619 //Arrange
620 resetContainer();
621 toastr.options.positionClass = positionClasses.bottomRight;
622 //Act
623 var $toast = toastr.success(sampleMsg);
624 var $container = toastr.getContainer();
625 //Assert
626 ok($container.hasClass(positionClasses.bottomRight), 'Has position bottom right');
627 //Teardown
628 $toast.remove();
629 resetContainer();
630 });
631 test('Container - position bottom-left', 1, function () {
632 //Arrange
633 resetContainer();
634 //$(selectors.container).remove()
635 toastr.options.positionClass = positionClasses.bottomLeft;
636 //Act
637 var $toast = toastr.success(sampleMsg);
638 var $container = toastr.getContainer();
639 //Assert
640 ok($container.hasClass(positionClasses.bottomLeft), 'Has position bottom left');
641 //Teardown
642 $toast.remove();
643 resetContainer();
644 });
645 test('Container - position top-left', 1, function () {
646 //Arrange
647 resetContainer();
648 toastr.options.positionClass = positionClasses.topLeft;
649 //Act
650 var $toast = toastr.success(sampleMsg);
651 var $container = toastr.getContainer();
652 //Assert
653 ok($container.hasClass(positionClasses.topLeft), 'Has position top left');
654 //Teardown
655 $toast.remove();
656 resetContainer();
657 });
658 test('Container - position top-center', 1, function () {
659 //Arrange
660 resetContainer();
661 toastr.options.positionClass = positionClasses.topCenter;
662 //Act
663 var $toast = toastr.success(sampleMsg);
664 var $container = toastr.getContainer();
665 //Assert
666 ok($container.hasClass(positionClasses.topCenter), 'Has position top center');
667 //Teardown
668 $toast.remove();
669 resetContainer();
670 });
671 test('Container - position bottom-center', 1, function () {
672 //Arrange
673 resetContainer();
674 toastr.options.positionClass = positionClasses.bottomCenter;
675 //Act
676 var $toast = toastr.success(sampleMsg);
677 var $container = toastr.getContainer();
678 //Assert
679 ok($container.hasClass(positionClasses.bottomCenter), 'Has position bottom center');
680 //Teardown
681 $toast.remove();
682 resetContainer();
683 });
684
685 function resetContainer() {
686 var $container = toastr.getContainer();
687 if ($container) {
688 $container.remove();
689 }
690 $(selectors.container).remove();
691 clearContainerChildren();
692 }
693
694 function clearContainerChildren() {
695 toastr.clear();
696 }
697
698})();