| OLD | NEW |
| (Empty) | |
| 1 library ng_model_spec; |
| 2 |
| 3 import '../_specs.dart'; |
| 4 import 'dart:html' as dom; |
| 5 |
| 6 main() => |
| 7 describe('ng-model', () { |
| 8 TestBed _; |
| 9 |
| 10 beforeEach(inject((TestBed tb) => _ = tb)); |
| 11 |
| 12 describe('type="text"', () { |
| 13 it('should update input value from model', inject(() { |
| 14 _.compile('<input type="text" ng-model="model">'); |
| 15 _.rootScope.$digest(); |
| 16 |
| 17 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 18 |
| 19 _.rootScope.$apply('model = "misko"'); |
| 20 expect((_.rootElement as dom.InputElement).value).toEqual('misko'); |
| 21 })); |
| 22 |
| 23 it('should render null as the empty string', inject(() { |
| 24 _.compile('<input type="text" ng-model="model">'); |
| 25 _.rootScope.$digest(); |
| 26 |
| 27 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 28 |
| 29 _.rootScope.$apply('model = null'); |
| 30 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 31 })); |
| 32 |
| 33 it('should update model from the input value', inject(() { |
| 34 _.compile('<input type="text" ng-model="model" probe="p">'); |
| 35 Probe probe = _.rootScope.p; |
| 36 var ngModel = probe.directive(NgModel); |
| 37 InputElement inputElement = probe.element; |
| 38 |
| 39 inputElement.value = 'abc'; |
| 40 _.triggerEvent(inputElement, 'change'); |
| 41 expect(_.rootScope.model).toEqual('abc'); |
| 42 |
| 43 inputElement.value = 'def'; |
| 44 var input = probe.directive(InputTextDirective); |
| 45 input.processValue(); |
| 46 expect(_.rootScope.model).toEqual('def'); |
| 47 |
| 48 })); |
| 49 |
| 50 it('should write to input only if value is different', inject(() { |
| 51 var scope = _.rootScope; |
| 52 var model = new NgModel(scope, new NodeAttrs(new DivElement())); |
| 53 var element = new dom.InputElement(); |
| 54 dom.querySelector('body').append(element); |
| 55 var input = new InputTextDirective(element, model, scope); |
| 56 |
| 57 element.value = 'abc'; |
| 58 element.selectionStart = 1; |
| 59 element.selectionEnd = 2; |
| 60 |
| 61 model.render('abc'); |
| 62 |
| 63 expect(element.value).toEqual('abc'); |
| 64 // No update. selectionStart/End is unchanged. |
| 65 expect(element.selectionStart).toEqual(1); |
| 66 expect(element.selectionEnd).toEqual(2); |
| 67 |
| 68 model.render('xyz'); |
| 69 |
| 70 // Value updated. selectionStart/End changed. |
| 71 expect(element.value).toEqual('xyz'); |
| 72 expect(element.selectionStart).toEqual(3); |
| 73 expect(element.selectionEnd).toEqual(3); |
| 74 })); |
| 75 }); |
| 76 |
| 77 describe('type="password"', () { |
| 78 it('should update input value from model', inject(() { |
| 79 _.compile('<input type="password" ng-model="model">'); |
| 80 _.rootScope.$digest(); |
| 81 |
| 82 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 83 |
| 84 _.rootScope.$apply('model = "misko"'); |
| 85 expect((_.rootElement as dom.InputElement).value).toEqual('misko'); |
| 86 })); |
| 87 |
| 88 it('should render null as the empty string', inject(() { |
| 89 _.compile('<input type="password" ng-model="model">'); |
| 90 _.rootScope.$digest(); |
| 91 |
| 92 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 93 |
| 94 _.rootScope.$apply('model = null'); |
| 95 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 96 })); |
| 97 |
| 98 it('should update model from the input value', inject(() { |
| 99 _.compile('<input type="password" ng-model="model" probe="p">'); |
| 100 Probe probe = _.rootScope.p; |
| 101 var ngModel = probe.directive(NgModel); |
| 102 InputElement inputElement = probe.element; |
| 103 |
| 104 inputElement.value = 'abc'; |
| 105 _.triggerEvent(inputElement, 'change'); |
| 106 expect(_.rootScope.model).toEqual('abc'); |
| 107 |
| 108 inputElement.value = 'def'; |
| 109 var input = probe.directive(InputPasswordDirective); |
| 110 input.processValue(); |
| 111 expect(_.rootScope.model).toEqual('def'); |
| 112 |
| 113 })); |
| 114 |
| 115 it('should write to input only if value is different', inject(() { |
| 116 var scope = _.rootScope; |
| 117 var model = new NgModel(scope, new NodeAttrs(new DivElement())); |
| 118 var element = new dom.InputElement(); |
| 119 dom.querySelector('body').append(element); |
| 120 var input = new InputPasswordDirective(element, model, scope); |
| 121 |
| 122 element.value = 'abc'; |
| 123 element.selectionStart = 1; |
| 124 element.selectionEnd = 2; |
| 125 |
| 126 model.render('abc'); |
| 127 |
| 128 expect(element.value).toEqual('abc'); |
| 129 expect(element.selectionStart).toEqual(1); |
| 130 expect(element.selectionEnd).toEqual(2); |
| 131 |
| 132 model.render('xyz'); |
| 133 |
| 134 expect(element.value).toEqual('xyz'); |
| 135 expect(element.selectionStart).toEqual(3); |
| 136 expect(element.selectionEnd).toEqual(3); |
| 137 })); |
| 138 }); |
| 139 |
| 140 describe('type="checkbox"', () { |
| 141 it('should update input value from model', inject((Scope scope) { |
| 142 var element = _.compile('<input type="checkbox" ng-model="model">'); |
| 143 |
| 144 scope.$apply(() { |
| 145 scope['model'] = true; |
| 146 }); |
| 147 expect(element.checked).toBe(true); |
| 148 |
| 149 scope.$apply(() { |
| 150 scope['model'] = false; |
| 151 }); |
| 152 expect(element.checked).toBe(false); |
| 153 })); |
| 154 |
| 155 |
| 156 it('should allow non boolean values like null, 0, 1', inject((Scope scope) { |
| 157 var element = _.compile('<input type="checkbox" ng-model="model">'); |
| 158 |
| 159 scope.$apply(() { |
| 160 scope['model'] = 0; |
| 161 }); |
| 162 expect(element.checked).toBe(false); |
| 163 |
| 164 scope.$apply(() { |
| 165 scope['model'] = 1; |
| 166 }); |
| 167 expect(element.checked).toBe(true); |
| 168 |
| 169 scope.$apply(() { |
| 170 scope['model'] = null; |
| 171 }); |
| 172 expect(element.checked).toBe(false); |
| 173 })); |
| 174 |
| 175 |
| 176 it('should update model from the input value', inject((Scope scope) { |
| 177 var element = _.compile('<input type="checkbox" ng-model="model">'); |
| 178 |
| 179 element.checked = true; |
| 180 _.triggerEvent(element, 'change'); |
| 181 expect(scope['model']).toBe(true); |
| 182 |
| 183 element.checked = false; |
| 184 _.triggerEvent(element, 'change'); |
| 185 expect(scope['model']).toBe(false); |
| 186 })); |
| 187 |
| 188 }); |
| 189 |
| 190 describe('type="textarea"', () { |
| 191 it('should update textarea value from model', inject(() { |
| 192 _.compile('<textarea ng-model="model">'); |
| 193 _.rootScope.$digest(); |
| 194 |
| 195 expect((_.rootElement as dom.TextAreaElement).value).toEqual(''); |
| 196 |
| 197 _.rootScope.$apply('model = "misko"'); |
| 198 expect((_.rootElement as dom.TextAreaElement).value).toEqual('misko'); |
| 199 })); |
| 200 |
| 201 it('should render null as the empty string', inject(() { |
| 202 _.compile('<textarea ng-model="model">'); |
| 203 _.rootScope.$digest(); |
| 204 |
| 205 expect((_.rootElement as dom.TextAreaElement).value).toEqual(''); |
| 206 |
| 207 _.rootScope.$apply('model = null'); |
| 208 expect((_.rootElement as dom.TextAreaElement).value).toEqual(''); |
| 209 })); |
| 210 |
| 211 it('should update model from the input value', inject(() { |
| 212 _.compile('<textarea ng-model="model" probe="p">'); |
| 213 Probe probe = _.rootScope.p; |
| 214 var ngModel = probe.directive(NgModel); |
| 215 TextAreaElement element = probe.element; |
| 216 |
| 217 element.value = 'abc'; |
| 218 _.triggerEvent(element, 'change'); |
| 219 expect(_.rootScope.model).toEqual('abc'); |
| 220 |
| 221 element.value = 'def'; |
| 222 var textarea = probe.directive(TextAreaDirective); |
| 223 textarea.processValue(); |
| 224 expect(_.rootScope.model).toEqual('def'); |
| 225 |
| 226 })); |
| 227 |
| 228 // NOTE(deboer): This test passes on Dartium, but fails in the content_shell
. |
| 229 // The Dart team is looking into this bug. |
| 230 xit('should write to input only if value is different', inject(() { |
| 231 var scope = _.rootScope; |
| 232 var model = new NgModel(scope, new NodeAttrs(new DivElement())); |
| 233 var element = new dom.TextAreaElement(); |
| 234 dom.querySelector('body').append(element); |
| 235 var input = new TextAreaDirective(element, model, scope); |
| 236 |
| 237 element.value = 'abc'; |
| 238 element.selectionStart = 1; |
| 239 element.selectionEnd = 2; |
| 240 |
| 241 model.render('abc'); |
| 242 |
| 243 expect(element.value).toEqual('abc'); |
| 244 expect(element.selectionStart).toEqual(1); |
| 245 expect(element.selectionEnd).toEqual(2); |
| 246 |
| 247 model.render('xyz'); |
| 248 |
| 249 // Setting the value on a textarea doesn't update the selection the way it |
| 250 // does on input elements. This stays unchanged. |
| 251 expect(element.value).toEqual('xyz'); |
| 252 expect(element.selectionStart).toEqual(0); |
| 253 expect(element.selectionEnd).toEqual(0); |
| 254 })); |
| 255 }); |
| 256 |
| 257 describe('type="number"', () { |
| 258 it('should update input value from model', inject(() { |
| 259 _.compile('<input type="number" ng-model="model">'); |
| 260 _.rootScope.$digest(); |
| 261 |
| 262 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 263 |
| 264 // Invalid number |
| 265 _.rootScope.$apply('model = "foo"'); |
| 266 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 267 |
| 268 // Valid number |
| 269 _.rootScope.$apply('model = 1234'); |
| 270 expect((_.rootElement as dom.InputElement).value).toEqual('1234'); |
| 271 })); |
| 272 |
| 273 it('should not render null or NaN values', inject(() { |
| 274 _.compile('<input type="number" ng-model="model">'); |
| 275 _.rootScope.$digest(); |
| 276 |
| 277 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 278 |
| 279 _.rootScope.$apply('model = null'); |
| 280 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 281 |
| 282 _.rootScope['model'] = 0/0; |
| 283 _.rootScope.$digest(); |
| 284 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 285 })); |
| 286 |
| 287 it('should update model from the input value', inject(() { |
| 288 _.compile('<input type="number" ng-model="model" probe="p">'); |
| 289 Probe probe = _.rootScope.p; |
| 290 var ngModel = probe.directive(NgModel); |
| 291 InputElement inputElement = probe.element; |
| 292 |
| 293 // Valid value |
| 294 inputElement.value = '12345'; |
| 295 _.triggerEvent(inputElement, 'change'); |
| 296 expect(_.rootScope.model).toEqual(12345); |
| 297 |
| 298 // Invalid value |
| 299 inputElement.value = 'foo'; |
| 300 var input = probe.directive(InputNumberDirective); |
| 301 input.processValue(); |
| 302 expect(_.rootScope.model.isNaN).toBe(true); |
| 303 })); |
| 304 }); |
| 305 |
| 306 describe('type="email"', () { |
| 307 it('should update input value from model', inject(() { |
| 308 _.compile('<input type="email" ng-model="model">'); |
| 309 _.rootScope.$digest(); |
| 310 |
| 311 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 312 |
| 313 // Invalid e-mail |
| 314 _.rootScope.$apply('model = "foo"'); |
| 315 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 316 |
| 317 // Valid e-mail |
| 318 _.rootScope.$apply('model = "foo@example.com"'); |
| 319 expect((_.rootElement as dom.InputElement).value).toEqual('foo@example.com
'); |
| 320 })); |
| 321 |
| 322 it('should render null as the empty string', inject(() { |
| 323 _.compile('<input type="email" ng-model="model">'); |
| 324 _.rootScope.$digest(); |
| 325 |
| 326 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 327 |
| 328 _.rootScope.$apply('model = null'); |
| 329 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 330 })); |
| 331 |
| 332 it('should update model from the input value', inject(() { |
| 333 _.compile('<input type="email" ng-model="model" probe="p">'); |
| 334 Probe probe = _.rootScope.p; |
| 335 var ngModel = probe.directive(NgModel); |
| 336 InputElement inputElement = probe.element; |
| 337 |
| 338 // Valid value |
| 339 inputElement.value = 'foo@example.com'; |
| 340 _.triggerEvent(inputElement, 'change'); |
| 341 expect(_.rootScope.model).toEqual('foo@example.com'); |
| 342 |
| 343 // Invalid value |
| 344 inputElement.value = 'foo'; |
| 345 var input = probe.directive(InputEmailDirective); |
| 346 input.processValue(); |
| 347 expect(_.rootScope.model).toEqual(null); |
| 348 })); |
| 349 }); |
| 350 |
| 351 describe('type="url"', () { |
| 352 it('should update input value from model', inject(() { |
| 353 _.compile('<input type="url" ng-model="model">'); |
| 354 _.rootScope.$digest(); |
| 355 |
| 356 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 357 |
| 358 // Invalid e-mail |
| 359 _.rootScope.$apply('model = "foo"'); |
| 360 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 361 |
| 362 // Valid e-mail |
| 363 _.rootScope.$apply('model = "http://www.google.com"'); |
| 364 expect((_.rootElement as dom.InputElement).value).toEqual( |
| 365 'http://www.google.com'); |
| 366 })); |
| 367 |
| 368 it('should render null as the empty string', inject(() { |
| 369 _.compile('<input type="url" ng-model="model">'); |
| 370 _.rootScope.$digest(); |
| 371 |
| 372 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 373 |
| 374 _.rootScope.$apply('model = null'); |
| 375 expect((_.rootElement as dom.InputElement).value).toEqual(''); |
| 376 })); |
| 377 |
| 378 it('should update model from the input value', inject(() { |
| 379 _.compile('<input type="url" ng-model="model" probe="p">'); |
| 380 Probe probe = _.rootScope.p; |
| 381 var ngModel = probe.directive(NgModel); |
| 382 InputElement inputElement = probe.element; |
| 383 |
| 384 // Valid value |
| 385 inputElement.value = 'http://www.google.com'; |
| 386 _.triggerEvent(inputElement, 'change'); |
| 387 expect(_.rootScope.model).toEqual('http://www.google.com'); |
| 388 |
| 389 // Invalid value |
| 390 inputElement.value = 'foo'; |
| 391 var input = probe.directive(InputUrlDirective); |
| 392 input.processValue(); |
| 393 expect(_.rootScope.model).toEqual(null); |
| 394 })); |
| 395 }); |
| 396 |
| 397 describe('type="radio"', () { |
| 398 it('should update input value from model', inject(() { |
| 399 _.compile('<input type="radio" name="color" value="red" ng-model="color" p
robe="r">' + |
| 400 '<input type="radio" name="color" value="green" ng-model="color"
probe="g">' + |
| 401 '<input type="radio" name="color" value="blue" ng-model="color"
probe="b">'); |
| 402 _.rootScope.$digest(); |
| 403 |
| 404 RadioButtonInputElement redBtn = _.rootScope.r.element; |
| 405 RadioButtonInputElement greenBtn = _.rootScope.g.element; |
| 406 RadioButtonInputElement blueBtn = _.rootScope.b.element; |
| 407 |
| 408 expect(redBtn.checked).toBe(false); |
| 409 expect(greenBtn.checked).toBe(false); |
| 410 expect(blueBtn.checked).toBe(false); |
| 411 |
| 412 // Should change correct element to checked. |
| 413 _.rootScope.$apply('color = "green"'); |
| 414 |
| 415 expect(redBtn.checked).toBe(false); |
| 416 expect(greenBtn.checked).toBe(true); |
| 417 expect(blueBtn.checked).toBe(false); |
| 418 |
| 419 // Non-existing element. |
| 420 _.rootScope.$apply('color = "unknown"'); |
| 421 |
| 422 expect(redBtn.checked).toBe(false); |
| 423 expect(greenBtn.checked).toBe(false); |
| 424 expect(blueBtn.checked).toBe(false); |
| 425 |
| 426 // Should update model with value of checked element. |
| 427 _.triggerEvent(redBtn, 'click'); |
| 428 |
| 429 expect(_.rootScope['color']).toEqual('red'); |
| 430 expect(redBtn.checked).toBe(true); |
| 431 expect(greenBtn.checked).toBe(false); |
| 432 expect(blueBtn.checked).toBe(false); |
| 433 |
| 434 _.triggerEvent(greenBtn, 'click'); |
| 435 expect(_.rootScope['color']).toEqual('green'); |
| 436 expect(redBtn.checked).toBe(false); |
| 437 expect(greenBtn.checked).toBe(true); |
| 438 expect(blueBtn.checked).toBe(false); |
| 439 })); |
| 440 }); |
| 441 |
| 442 }); |
| OLD | NEW |