blob: c67d03a6267d16a554e1723e7a2c8dce90168be8 [file] [log] [blame]
Matteo Scandolo74b6ef72016-04-15 16:43:48 -07001/**
2 * © OpenCORD
3 *
4 * Created by teone on 4/15/16.
5 */
6
7(function () {
8 'use strict';
9
10 describe('The xos.helper module', function(){
11 describe('The xos-alert component', () => {
12
13 let element, scope, isolatedScope;
14
15 let message = 'Test Error Message';
16
17 beforeEach(module('xos.helpers'));
18
19 it('should throw an error if no config is specified', inject(($compile, $rootScope) => {
20 function errorFunctionWrapper(){
21 $compile(angular.element('<xos-alert></xos-alert>'))($rootScope);
22 $rootScope.$digest();
23 }
24 expect(errorFunctionWrapper).toThrow(new Error('[xosAlert] Please provide a configuration via the "config" attribute'));
25 }));
26
27 describe('when correctly configured', () => {
28 beforeEach(inject(($compile, $rootScope) => {
29
30 scope = $rootScope.$new();
31
32 scope.config = {
33 type: 'danger',
34 closeBtn: true
35 };
36
37 element = angular.element(`<xos-alert config="config">${message}</xos-alert>`);
38 $compile(element)(scope);
39 scope.$digest();
40 isolatedScope = element.isolateScope().vm;
41 }));
42
43 it('should transclude the message', () => {
44 let textContainer = element[0].getElementsByTagName('p')[0];
45 let text = angular.element(textContainer).text();
46 expect(text).toEqual(message)
47 });
48
49 it('should have a close button', () => {
50 let btn = element[0].getElementsByTagName('button');
51 expect(btn.length).toEqual(1);
52 });
53
54 describe('when the close button is clicked', () => {
55 it('should hide the alert', () => {
56 let btn = element[0].getElementsByTagName('button')[0];
57 btn.click();
58 let alert = angular.element(element[0].querySelectorAll('.alert')[0]);
59 expect(alert.hasClass('ng-hide')).toBeTruthy();
60 });
61 });
62
63 describe('when autoHide is set', () => {
64
65 let to;
66
67 beforeEach(inject(($compile, $rootScope, $timeout) => {
68 scope = $rootScope.$new();
69
70 scope.config = {
71 type: 'danger',
72 closeBtn: true,
73 autoHide: 500
74 };
75
76 to = $timeout;
77
78 element = angular.element(`<xos-alert config="config">${message}</xos-alert>`);
79 $compile(element)(scope);
80 scope.$digest();
81 isolatedScope = element.isolateScope().vm;
82 }));
83
84 it('should hide the alert', () => {
85 to.flush();
86 expect(isolatedScope.show).toBeFalsy();
87 let alert = angular.element(element[0].querySelectorAll('.alert')[0]);
88 expect(alert.hasClass('ng-hide')).toBeTruthy();
89 });
90 });
91
92 describe('when show is set to false', () => {
93
94 beforeEach(inject(($compile, $rootScope) => {
95 scope = $rootScope.$new();
96
97 scope.config = {
98 type: 'danger',
99 closeBtn: true
100 };
101
102 scope.show = false;
103
104 element = angular.element(`<xos-alert config="config" show="show">${message}</xos-alert>`);
105 $compile(element)(scope);
106 scope.$digest();
107 isolatedScope = element.isolateScope().vm;
108 }));
109
110 it('should hide the alert', () => {
111 let alert = angular.element(element[0].querySelectorAll('.alert')[0]);
112 expect(alert.hasClass('ng-hide')).toBeTruthy();
113 });
114
115 describe('when show is changed to true', () => {
116 beforeEach(() => {
117 scope.show = true;
118 scope.$digest();
119 });
120
121 it('should show the alert', () => {
122 let alert = angular.element(element[0].querySelectorAll('.alert')[0]);
123 expect(alert.hasClass('ng-hide')).toBeFalsy();
124 });
125 });
126 });
127
128 });
129 });
130 });
131})();