forked from angular-ui/bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalert.spec.js
108 lines (83 loc) · 3 KB
/
alert.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
describe('alert', function () {
var scope, $compile;
var element;
beforeEach(module('ui.bootstrap.alert'));
beforeEach(module('template/alert/alert.html'));
beforeEach(inject(function ($rootScope, _$compile_) {
scope = $rootScope;
$compile = _$compile_;
element = angular.element(
'<div>' +
'<alert ng-repeat="alert in alerts" type="{{alert.type}}"' +
'close="removeAlert($index)">{{alert.msg}}' +
'</alert>' +
'</div>');
scope.alerts = [
{ msg:'foo', type:'success'},
{ msg:'bar', type:'error'},
{ msg:'baz'}
];
}));
function createAlerts() {
$compile(element)(scope);
scope.$digest();
return element.find('.alert');
}
function findCloseButton(index) {
return element.find('.close').eq(index);
}
function findContent(index) {
return element.find('div[ng-transclude] span').eq(index);
}
it('should generate alerts using ng-repeat', function () {
var alerts = createAlerts();
expect(alerts.length).toEqual(3);
});
it('should use correct classes for different alert types', function () {
var alerts = createAlerts();
expect(alerts.eq(0)).toHaveClass('alert-success');
expect(alerts.eq(1)).toHaveClass('alert-error');
expect(alerts.eq(2)).toHaveClass('alert-warning');
});
it('should respect alert type binding', function () {
var alerts = createAlerts();
expect(alerts.eq(0)).toHaveClass('alert-success');
scope.alerts[0].type = 'error';
scope.$digest();
expect(alerts.eq(0)).toHaveClass('alert-error');
});
it('should show the alert content', function() {
var alerts = createAlerts();
for (var i = 0, n = alerts.length; i < n; i++) {
expect(findContent(i).text()).toBe(scope.alerts[i].msg);
}
});
it('should show close buttons and have the dismissable class', function () {
var alerts = createAlerts();
for (var i = 0, n = alerts.length; i < n; i++) {
expect(findCloseButton(i).css('display')).not.toBe('none');
expect(alerts.eq(i)).toHaveClass('alert-dismissable');
}
});
it('should fire callback when closed', function () {
var alerts = createAlerts();
scope.$apply(function () {
scope.removeAlert = jasmine.createSpy();
});
expect(findCloseButton(0).css('display')).not.toBe('none');
findCloseButton(1).click();
expect(scope.removeAlert).toHaveBeenCalledWith(1);
});
it('should not show close button and have the dismissable class if no close callback specified', function () {
element = $compile('<alert>No close</alert>')(scope);
scope.$digest();
expect(findCloseButton(0)).toBeHidden();
expect(element).not.toHaveClass('alert-dismissable');
});
it('should be possible to add additional classes for alert', function () {
var element = $compile('<alert class="alert-block" type="info">Default alert!</alert>')(scope);
scope.$digest();
expect(element).toHaveClass('alert-block');
expect(element).toHaveClass('alert-info');
});
});