@@ -83,52 +83,64 @@ describe('ngPluralize', function() {
83
83
} ) ) ;
84
84
85
85
86
- it ( 'should show single/plural strings with mal-formed inputs' , inject ( function ( $rootScope ) {
87
- $rootScope . email = '' ;
88
- $rootScope . $digest ( ) ;
89
- expect ( element . text ( ) ) . toBe ( '' ) ;
90
- expect ( elementAlt . text ( ) ) . toBe ( '' ) ;
91
-
92
- $rootScope . email = null ;
93
- $rootScope . $digest ( ) ;
94
- expect ( element . text ( ) ) . toBe ( '' ) ;
95
- expect ( elementAlt . text ( ) ) . toBe ( '' ) ;
86
+ it ( 'should show single/plural strings with mal-formed inputs' , inject (
87
+ function ( $log , $rootScope ) {
88
+ $rootScope . email = '' ;
89
+ $rootScope . $digest ( ) ;
90
+ expect ( element . text ( ) ) . toBe ( '' ) ;
91
+ expect ( elementAlt . text ( ) ) . toBe ( '' ) ;
92
+ expect ( $log . debug . logs . shift ( ) ) . toEqual ( [
93
+ "ngPluralize: no rule defined for 'NaN' in {" +
94
+ "'-1': 'You have negative email. Whohoo!'," +
95
+ "'0': 'You have no new email'," +
96
+ "'one': 'You have one new email'," +
97
+ "'other': 'You have {} new emails'}"
98
+ ] ) ;
99
+ expect ( $log . debug . logs . shift ( ) ) . toEqual ( [
100
+ "ngPluralize: no rule defined for 'NaN' in undefined"
101
+ ] ) ;
102
+
103
+ $rootScope . email = null ;
104
+ $rootScope . $digest ( ) ;
105
+ expect ( element . text ( ) ) . toBe ( '' ) ;
106
+ expect ( elementAlt . text ( ) ) . toBe ( '' ) ;
96
107
97
- $rootScope . email = undefined ;
98
- $rootScope . $digest ( ) ;
99
- expect ( element . text ( ) ) . toBe ( '' ) ;
100
- expect ( elementAlt . text ( ) ) . toBe ( '' ) ;
108
+ $rootScope . email = undefined ;
109
+ $rootScope . $digest ( ) ;
110
+ expect ( element . text ( ) ) . toBe ( '' ) ;
111
+ expect ( elementAlt . text ( ) ) . toBe ( '' ) ;
101
112
102
- $rootScope . email = 'a3' ;
103
- $rootScope . $digest ( ) ;
104
- expect ( element . text ( ) ) . toBe ( '' ) ;
105
- expect ( elementAlt . text ( ) ) . toBe ( '' ) ;
113
+ $rootScope . email = 'a3' ;
114
+ $rootScope . $digest ( ) ;
115
+ expect ( element . text ( ) ) . toBe ( '' ) ;
116
+ expect ( elementAlt . text ( ) ) . toBe ( '' ) ;
106
117
107
- $rootScope . email = '011' ;
108
- $rootScope . $digest ( ) ;
109
- expect ( element . text ( ) ) . toBe ( 'You have 11 new emails' ) ;
110
- expect ( elementAlt . text ( ) ) . toBe ( 'You have 11 new emails' ) ;
118
+ $rootScope . email = '011' ;
119
+ $rootScope . $digest ( ) ;
120
+ expect ( element . text ( ) ) . toBe ( 'You have 11 new emails' ) ;
121
+ expect ( elementAlt . text ( ) ) . toBe ( 'You have 11 new emails' ) ;
111
122
112
- $rootScope . email = '-011' ;
113
- $rootScope . $digest ( ) ;
114
- expect ( element . text ( ) ) . toBe ( 'You have -11 new emails' ) ;
115
- expect ( elementAlt . text ( ) ) . toBe ( 'You have -11 new emails' ) ;
123
+ $rootScope . email = '-011' ;
124
+ $rootScope . $digest ( ) ;
125
+ expect ( element . text ( ) ) . toBe ( 'You have -11 new emails' ) ;
126
+ expect ( elementAlt . text ( ) ) . toBe ( 'You have -11 new emails' ) ;
116
127
117
- $rootScope . email = '1fff' ;
118
- $rootScope . $digest ( ) ;
119
- expect ( element . text ( ) ) . toBe ( 'You have one new email' ) ;
120
- expect ( elementAlt . text ( ) ) . toBe ( 'You have one new email' ) ;
128
+ $rootScope . email = '1fff' ;
129
+ $rootScope . $digest ( ) ;
130
+ expect ( element . text ( ) ) . toBe ( 'You have one new email' ) ;
131
+ expect ( elementAlt . text ( ) ) . toBe ( 'You have one new email' ) ;
121
132
122
- $rootScope . email = '0aa22' ;
123
- $rootScope . $digest ( ) ;
124
- expect ( element . text ( ) ) . toBe ( 'You have no new email' ) ;
125
- expect ( elementAlt . text ( ) ) . toBe ( 'You have no new email' ) ;
133
+ $rootScope . email = '0aa22' ;
134
+ $rootScope . $digest ( ) ;
135
+ expect ( element . text ( ) ) . toBe ( 'You have no new email' ) ;
136
+ expect ( elementAlt . text ( ) ) . toBe ( 'You have no new email' ) ;
126
137
127
- $rootScope . email = '000001' ;
128
- $rootScope . $digest ( ) ;
129
- expect ( element . text ( ) ) . toBe ( 'You have one new email' ) ;
130
- expect ( elementAlt . text ( ) ) . toBe ( 'You have one new email' ) ;
131
- } ) ) ;
138
+ $rootScope . email = '000001' ;
139
+ $rootScope . $digest ( ) ;
140
+ expect ( element . text ( ) ) . toBe ( 'You have one new email' ) ;
141
+ expect ( elementAlt . text ( ) ) . toBe ( 'You have one new email' ) ;
142
+ }
143
+ ) ) ;
132
144
} ) ;
133
145
134
146
@@ -144,6 +156,33 @@ describe('ngPluralize', function() {
144
156
$rootScope . $digest ( ) ;
145
157
expect ( element . text ( ) ) . toBe ( '' ) ;
146
158
} ) ) ;
159
+
160
+ it ( 'should be able to specify a message for null/undefined values' , inject (
161
+ function ( $compile , $rootScope ) {
162
+ element = $compile (
163
+ '<ng:pluralize count="email"' +
164
+ "when=\"{'NaN': 'Unspecified email count'," +
165
+ "'0': ''," +
166
+ "'one': 'Some text'," +
167
+ "'other': 'Some text'}\">" +
168
+ '</ng:pluralize>' ) ( $rootScope ) ;
169
+
170
+ $rootScope . email = '0' ;
171
+ $rootScope . $digest ( ) ;
172
+ expect ( element . text ( ) ) . toBe ( '' ) ;
173
+
174
+ $rootScope . email = undefined ;
175
+ $rootScope . $digest ( ) ;
176
+ expect ( element . text ( ) ) . toBe ( 'Unspecified email count' ) ;
177
+
178
+ $rootScope . email = '1' ;
179
+ $rootScope . $digest ( ) ;
180
+ expect ( element . text ( ) ) . toBe ( 'Some text' ) ;
181
+
182
+ $rootScope . email = null ;
183
+ $rootScope . $digest ( ) ;
184
+ expect ( element . text ( ) ) . toBe ( 'Unspecified email count' ) ;
185
+ } ) ) ;
147
186
} ) ;
148
187
149
188
describe ( 'undefined rule cases' , function ( ) {
@@ -195,6 +234,10 @@ describe('ngPluralize', function() {
195
234
$rootScope . email = '1' ;
196
235
$rootScope . $digest ( ) ;
197
236
expect ( element . text ( ) ) . toBe ( 'Some text' ) ;
237
+
238
+ $rootScope . email = null ;
239
+ $rootScope . $digest ( ) ;
240
+ expect ( element . text ( ) ) . toBe ( '' ) ;
198
241
} ) ) ;
199
242
} ) ;
200
243
@@ -243,6 +286,11 @@ describe('ngPluralize', function() {
243
286
$rootScope . $digest ( ) ;
244
287
expect ( element . text ( ) ) . toBe ( 'Igor, Misko and 2 other people are viewing.' ) ;
245
288
expect ( elementAlt . text ( ) ) . toBe ( 'Igor, Misko and 2 other people are viewing.' ) ;
289
+
290
+ $rootScope . viewCount = null ;
291
+ $rootScope . $digest ( ) ;
292
+ expect ( element . text ( ) ) . toBe ( '' ) ;
293
+ expect ( elementAlt . text ( ) ) . toBe ( '' ) ;
246
294
} ) ) ;
247
295
} ) ;
248
296
@@ -296,7 +344,6 @@ describe('ngPluralize', function() {
296
344
297
345
298
346
describe ( 'bind-once' , function ( ) {
299
-
300
347
it ( 'should support for `count` to be a one-time expression' ,
301
348
inject ( function ( $compile , $rootScope ) {
302
349
element = $compile (
@@ -317,6 +364,9 @@ describe('ngPluralize', function() {
317
364
318
365
$rootScope . email = 3 ;
319
366
$rootScope . $digest ( ) ;
367
+
368
+ $rootScope . email = null ;
369
+ $rootScope . $digest ( ) ;
320
370
expect ( element . text ( ) ) . toBe ( 'You have 3 new emails' ) ;
321
371
expect ( elementAlt . text ( ) ) . toBe ( 'You have 3 new emails' ) ;
322
372
0 commit comments