Matteo Scandolo | 686547a | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 19 | /** |
| 20 | * © OpenCORD |
| 21 | * |
| 22 | * Created by teone on 4/15/16. |
| 23 | */ |
| 24 | |
| 25 | (function () { |
| 26 | 'use strict'; |
| 27 | |
| 28 | describe('The xos.helper module', function(){ |
| 29 | describe('The xos-alert component', () => { |
| 30 | |
| 31 | let element, scope, isolatedScope; |
| 32 | |
| 33 | let message = 'Test Error Message'; |
| 34 | |
| 35 | beforeEach(module('xos.helpers')); |
| 36 | |
| 37 | it('should throw an error if no config is specified', inject(($compile, $rootScope) => { |
| 38 | function errorFunctionWrapper(){ |
| 39 | $compile(angular.element('<xos-alert></xos-alert>'))($rootScope); |
| 40 | $rootScope.$digest(); |
| 41 | } |
| 42 | expect(errorFunctionWrapper).toThrow(new Error('[xosAlert] Please provide a configuration via the "config" attribute')); |
| 43 | })); |
| 44 | |
| 45 | describe('when correctly configured', () => { |
| 46 | beforeEach(inject(($compile, $rootScope) => { |
| 47 | |
| 48 | scope = $rootScope.$new(); |
| 49 | |
| 50 | scope.config = { |
| 51 | type: 'danger', |
| 52 | closeBtn: true |
| 53 | }; |
| 54 | |
| 55 | element = angular.element(`<xos-alert config="config">${message}</xos-alert>`); |
| 56 | $compile(element)(scope); |
| 57 | scope.$digest(); |
| 58 | isolatedScope = element.isolateScope().vm; |
| 59 | })); |
| 60 | |
| 61 | it('should transclude the message', () => { |
| 62 | let textContainer = element[0].getElementsByTagName('p')[0]; |
| 63 | let text = angular.element(textContainer).text(); |
| 64 | expect(text).toEqual(message) |
| 65 | }); |
| 66 | |
| 67 | it('should have a close button', () => { |
| 68 | let btn = element[0].getElementsByTagName('button'); |
| 69 | expect(btn.length).toEqual(1); |
| 70 | }); |
| 71 | |
| 72 | describe('when the close button is clicked', () => { |
| 73 | it('should hide the alert', () => { |
| 74 | let btn = element[0].getElementsByTagName('button')[0]; |
| 75 | btn.click(); |
| 76 | let alert = angular.element(element[0].querySelectorAll('.alert')[0]); |
| 77 | expect(alert.hasClass('ng-hide')).toBeTruthy(); |
| 78 | }); |
| 79 | }); |
| 80 | |
| 81 | describe('when autoHide is set', () => { |
| 82 | |
| 83 | let to; |
| 84 | |
| 85 | beforeEach(inject(($compile, $rootScope, $timeout) => { |
| 86 | scope = $rootScope.$new(); |
| 87 | |
| 88 | scope.config = { |
| 89 | type: 'danger', |
| 90 | closeBtn: true, |
| 91 | autoHide: 500 |
| 92 | }; |
| 93 | |
| 94 | to = $timeout; |
| 95 | |
| 96 | element = angular.element(`<xos-alert config="config">${message}</xos-alert>`); |
| 97 | $compile(element)(scope); |
| 98 | scope.$digest(); |
| 99 | isolatedScope = element.isolateScope().vm; |
| 100 | })); |
| 101 | |
| 102 | it('should hide the alert', () => { |
| 103 | to.flush(); |
| 104 | expect(isolatedScope.show).toBeFalsy(); |
| 105 | let alert = angular.element(element[0].querySelectorAll('.alert')[0]); |
| 106 | expect(alert.hasClass('ng-hide')).toBeTruthy(); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | describe('when show is set to false', () => { |
| 111 | |
| 112 | beforeEach(inject(($compile, $rootScope) => { |
| 113 | scope = $rootScope.$new(); |
| 114 | |
| 115 | scope.config = { |
| 116 | type: 'danger', |
| 117 | closeBtn: true |
| 118 | }; |
| 119 | |
| 120 | scope.show = false; |
| 121 | |
| 122 | element = angular.element(`<xos-alert config="config" show="show">${message}</xos-alert>`); |
| 123 | $compile(element)(scope); |
| 124 | scope.$digest(); |
| 125 | isolatedScope = element.isolateScope().vm; |
| 126 | })); |
| 127 | |
| 128 | it('should hide the alert', () => { |
| 129 | let alert = angular.element(element[0].querySelectorAll('.alert')[0]); |
| 130 | expect(alert.hasClass('ng-hide')).toBeTruthy(); |
| 131 | }); |
| 132 | |
| 133 | describe('when show is changed to true', () => { |
| 134 | beforeEach(() => { |
| 135 | scope.show = true; |
| 136 | scope.$digest(); |
| 137 | }); |
| 138 | |
| 139 | it('should show the alert', () => { |
| 140 | let alert = angular.element(element[0].querySelectorAll('.alert')[0]); |
| 141 | expect(alert.hasClass('ng-hide')).toBeFalsy(); |
| 142 | }); |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | }); |
| 147 | }); |
| 148 | }); |
| 149 | })(); |