forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngIdSpec.js
156 lines (126 loc) · 5.66 KB
/
ngIdSpec.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
describe('ngId', function() {
var element;
afterEach(function() {
dealoc(element);
});
it('should add new and remove old ids dynamically', inject(function($rootScope, $compile) {
element = $compile('<div id="existing" ng-id="dynId"></div>')($rootScope);
expect(element.attr('id') === 'existing').toBeTruthy();
$rootScope.dynId = 'A';
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBeFalsy();
expect(element.attr('id') === 'A').toBeTruthy();
$rootScope.dynId = 'B';
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBeFalsy();
expect(element.attr('id') === 'A').toBeFalsy();
expect(element.attr('id') === 'B').toBeTruthy();
delete $rootScope.dynId;
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBeTruthy();
expect(element.attr('id') === 'A').toBeFalsy();
expect(element.attr('id') === 'B').toBeFalsy();
}));
it('should support not support ids via an array', inject(function($rootScope, $compile) {
element = $compile('<div id="existing" ng-id="[\'A\', \'B\']"></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBeTruthy();
expect(element.attr('id') === 'A').toBeFalsy();
expect(element.attr('id') === 'B').toBeFalsy();
}));
it('should support adding multiple ids conditionally via a map of id names to boolean' +
'expressions', inject(function($rootScope, $compile) {
var element = $compile(
'<div id="existing" ' +
'ng-id="{A: conditionA, B: conditionB(), AnotB: conditionA&&!conditionB()}">' +
'</div>')($rootScope);
$rootScope.conditionA = true;
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBeFalsy();
expect(element.attr('id') === 'A').toBeTruthy();
expect(element.attr('id') === 'B').toBeFalsy();
expect(element.attr('id') === 'AnotB').toBeFalsy();
$rootScope.conditionB = function() { return true; };
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBeFalsy();
expect(element.attr('id') === 'A').toBeTruthy();
expect(element.attr('id') === 'B').toBeFalsy();
expect(element.attr('id') === 'AnotB').toBeFalsy();
$rootScope.conditionA = false;
$rootScope.conditionB = function() { return true; };
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBeFalsy();
expect(element.attr('id') === 'A').toBeFalsy();
expect(element.attr('id') === 'B').toBeTruthy();
expect(element.attr('id') === 'AnotB').toBeFalsy();
}));
it('should remove ids when the referenced object is the same but its property is changed',
inject(function($rootScope, $compile) {
var element = $compile('<div ng-id="ids"></div>')($rootScope);
$rootScope.ids = { A: true, B: true };
$rootScope.$digest();
expect(element.attr('id') === 'A').toBeTruthy();
expect(element.attr('id') === 'B').toBeFalsy();
$rootScope.ids.A = false;
$rootScope.$digest();
expect(element.attr('id') === 'A').toBeFalsy();
expect(element.attr('id') === 'B').toBeTruthy();
}
));
it('should return only the first word in a space delimited string', inject(function($rootScope, $compile) {
element = $compile('<div id="existing" ng-id="\'A B\'"></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBeFalsy();
expect(element.attr('id') === 'A').toBeTruthy();
expect(element.attr('id') === 'B').toBeFalsy();
}));
it('should replace id added post compilation with pre-existing ng-id value', inject(function($rootScope, $compile) {
element = $compile('<div id="existing" ng-id="dynId"></div>')($rootScope);
$rootScope.dynId = 'A';
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBe(false);
// add extra id, change model and eval
element.attr('id', 'newId');
$rootScope.dynId = 'B';
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBe(false);
expect(element.attr('id') === 'B').toBe(true);
expect(element.attr('id') === 'newid').toBe(false);
}));
it('should replace id added post compilation without pre-existing ids"', inject(function($rootScope, $compile) {
element = $compile('<div ng-id="dynId"></div>')($rootScope);
$rootScope.dynId = 'A';
$rootScope.$digest();
expect(element.attr('id') === 'A').toBe(true);
// add extra id, change model and eval
element.attr('id', 'newId');
$rootScope.dynId= 'B';
$rootScope.$digest();
expect(element.attr('id') === 'B').toBe(true);
expect(element.attr('id') === 'newid').toBe(false);
}));
it('should remove ids even if it was specified via id attribute', inject(function($rootScope, $compile) {
element = $compile('<div id="existing" ng-id="dynId"></div>')($rootScope);
$rootScope.dynId = 'A';
$rootScope.$digest();
$rootScope.dynId = 'B';
$rootScope.$digest();
expect(element.attr('id') === 'B').toBe(true);
}));
it('should convert undefined and null values to an empty string', inject(function($rootScope, $compile) {
element = $compile('<div ng-id="dynId"></div>')($rootScope);
$rootScope.dynId = [undefined, null];
$rootScope.$digest();
expect(element[0].id).toBeFalsy();
}));
it('should reinstate the original id if the specified id evaluates to false', inject(function($rootScope, $compile) {
element = $compile('<div id="existing" ng-id="dynId"></div>')($rootScope);
$rootScope.dynId = 'A';
$rootScope.$digest();
expect(element.attr('id') === 'A').toBeTruthy();
$rootScope.dynId = false;
$rootScope.$digest();
expect(element.attr('id') === 'existing').toBeTruthy();
}));
});