Skip to content

Commit 9c0b1a7

Browse files
committed
refactor(ng_model_spec): code cleanup
1 parent 0d4b7ad commit 9c0b1a7

File tree

1 file changed

+37
-120
lines changed

1 file changed

+37
-120
lines changed

test/directive/ng_model_spec.dart

+37-120
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void main() {
5050
dirInjector = new DirectiveInjector(null, _.injector, null, null, null, null, null, null);
5151
});
5252

53-
describe('type="text" like', () {
53+
describe('type="text"', () {
5454
it('should update input value from model', () {
5555
_.compile('<input type="text" ng-model="model">');
5656
_.rootScope.apply();
@@ -87,41 +87,24 @@ void main() {
8787
expect(_.rootScope.context['model']).toEqual('def');
8888
});
8989

90-
it('should write to input only if the value is different',
91-
(Injector i, Animate animate) {
92-
93-
NodeAttrs nodeAttrs = new NodeAttrs(new DivElement());
94-
95-
var scope = _.rootScope;
96-
var element = new dom.InputElement();
97-
var ngElement = new NgElement(element, scope, animate);
98-
var ngModelOptions = new NgModelOptions();
99-
100-
nodeAttrs['ng-model'] = 'model';
101-
var model = new NgModel(scope, ngElement, dirInjector,
102-
nodeAttrs, new Animate(), null);
90+
it('should write to input only if the value is different', (Injector i, Scope scope) {
91+
var element = _.compile('<input type="text" ng-model="model">');
10392
dom.querySelector('body').append(element);
104-
var input = new InputTextLike(element, model, scope, ngModelOptions);
10593

106-
element
107-
..value = 'abc'
108-
..selectionStart = 1
109-
..selectionEnd = 2;
94+
element..value = 'abc'
95+
..selectionStart = 1
96+
..selectionEnd = 2;
11097

111-
scope.apply(() {
112-
scope.context['model'] = 'abc';
113-
});
98+
scope.apply('model = "abc"');
11499

115100
expect(element.value).toEqual('abc');
116101
// No update. selectionStart/End is unchanged.
117102
expect(element.selectionStart).toEqual(1);
118103
expect(element.selectionEnd).toEqual(2);
119104

120-
scope.apply(() {
121-
scope.context['model'] = 'xyz';
122-
});
105+
scope.apply('model = "xyz"');
123106

124-
// Value updated. selectionStart/End changed.
107+
// Value updated. selectionStart/End changed.
125108
expect(element.value).toEqual('xyz');
126109
expect(element.selectionStart).toEqual(3);
127110
expect(element.selectionEnd).toEqual(3);
@@ -367,38 +350,21 @@ void main() {
367350

368351
});
369352

370-
it('should write to input only if value is different',
371-
(Injector i, Animate animate) {
372-
373-
NodeAttrs nodeAttrs = new NodeAttrs(new DivElement());
374-
375-
var scope = _.rootScope;
376-
var element = new dom.InputElement();
377-
var ngElement = new NgElement(element, scope, animate);
378-
var ngModelOptions = new NgModelOptions();
379-
380-
nodeAttrs['ng-model'] = 'model';
381-
var model = new NgModel(scope, ngElement, dirInjector,
382-
nodeAttrs, new Animate(), null);
353+
it('should write to input only if value is different', (Injector i, Scope scope) {
354+
var element = _.compile('<input type=password ng-model=model>');
383355
dom.querySelector('body').append(element);
384-
var input = new InputTextLike(element, model, scope, ngModelOptions);
385356

386-
element
387-
..value = 'abc'
388-
..selectionStart = 1
389-
..selectionEnd = 2;
357+
element..value = 'abc'
358+
..selectionStart = 1
359+
..selectionEnd = 2;
390360

391-
scope.apply(() {
392-
scope.context['model'] = 'abc';
393-
});
361+
scope.apply('model = "abc"');
394362

395363
expect(element.value).toEqual('abc');
396364
expect(element.selectionStart).toEqual(1);
397365
expect(element.selectionEnd).toEqual(2);
398366

399-
scope.apply(() {
400-
scope.context['model'] = 'xyz';
401-
});
367+
scope.apply('model = "xyz"');
402368

403369
expect(element.value).toEqual('xyz');
404370
expect(element.selectionStart).toEqual(3);
@@ -459,41 +425,22 @@ void main() {
459425
expect(_.rootScope.context['model']).toEqual('def');
460426
});
461427

462-
it('should write to input only if value is different',
463-
(Injector i, Animate animate) {
464-
465-
NodeAttrs nodeAttrs = new NodeAttrs(new DivElement());
466-
467-
var scope = _.rootScope;
468-
var element = new dom.InputElement();
469-
var ngElement = new NgElement(element, scope, animate);
470-
var ngModelOptions = new NgModelOptions();
471-
472-
nodeAttrs['ng-model'] = 'model';
473-
var model = new NgModel(scope, ngElement, dirInjector,
474-
nodeAttrs, new Animate(), null);
428+
it('should write to input only if value is different', (Injector i, Scope scope) {
429+
var element = _.compile('<input type=search ng-model=model>');
475430
dom.querySelector('body').append(element);
476-
var input = new InputTextLike(element, model, scope, ngModelOptions);
477431

478-
element
479-
..value = 'abc'
480-
..selectionStart = 1
481-
..selectionEnd = 2;
432+
element..value = 'abc'
433+
..selectionStart = 1
434+
..selectionEnd = 2;
482435

483-
scope.apply(() {
484-
scope.context['model'] = 'abc';
485-
});
436+
scope.apply('model = "abc"');
486437

487438
expect(element.value).toEqual('abc');
488-
// No update. selectionStart/End is unchanged.
489439
expect(element.selectionStart).toEqual(1);
490440
expect(element.selectionEnd).toEqual(2);
491441

492-
scope.apply(() {
493-
scope.context['model'] = 'xyz';
494-
});
442+
scope.apply('model = "xyz"');
495443

496-
// Value updated. selectionStart/End changed.
497444
expect(element.value).toEqual('xyz');
498445
expect(element.selectionStart).toEqual(3);
499446
expect(element.selectionEnd).toEqual(3);
@@ -564,38 +511,21 @@ void main() {
564511
expect(_.rootScope.context['model']).toEqual('def');
565512
});
566513

567-
it('should write to input only if value is different',
568-
(Injector i, Animate animate) {
569-
570-
NodeAttrs nodeAttrs = new NodeAttrs(new DivElement());
571-
572-
var scope = _.rootScope;
573-
var element = new dom.InputElement();
574-
var ngElement = new NgElement(element, scope, animate);
575-
var ngModelOptions = new NgModelOptions();
576-
577-
nodeAttrs['ng-model'] = 'model';
578-
var model = new NgModel(scope, ngElement, dirInjector,
579-
nodeAttrs, new Animate(), null);
514+
it('should write to input only if value is different', (Injector i, Scope scope) {
515+
var element = _.compile('<input ng-model=model>');
580516
dom.querySelector('body').append(element);
581-
var input = new InputTextLike(element, model, scope, ngModelOptions);
582517

583-
element
584-
..value = 'abc'
585-
..selectionStart = 1
586-
..selectionEnd = 2;
518+
element..value = 'abc'
519+
..selectionStart = 1
520+
..selectionEnd = 2;
587521

588-
scope.apply(() {
589-
scope.context['model'] = 'abc';
590-
});
522+
scope.apply('model = "abc"');
591523

592524
expect(element.value).toEqual('abc');
593525
expect(element.selectionStart).toEqual(1);
594526
expect(element.selectionEnd).toEqual(2);
595527

596-
scope.apply(() {
597-
scope.context['model'] = 'xyz';
598-
});
528+
scope.apply('model = "xyz"');
599529

600530
expect(element.value).toEqual('xyz');
601531
expect(element.selectionStart).toEqual(3);
@@ -775,34 +705,21 @@ void main() {
775705

776706
// NOTE(deboer): This test passes on Dartium, but fails in the content_shell.
777707
// The Dart team is looking into this bug.
778-
xit('should write to input only if value is different',
779-
(Injector i, Animate animate) {
780-
781-
NodeAttrs nodeAttrs = new NodeAttrs(new DivElement());
782-
783-
var scope = _.rootScope;
784-
var element = new dom.TextAreaElement();
785-
var ngElement = new NgElement(element, scope, animate);
786-
var ngModelOptions = new NgModelOptions();
787-
788-
nodeAttrs['ng-model'] = 'model';
789-
var model = new NgModel(scope, ngElement, dirInjector,
790-
nodeAttrs, new Animate(), null);
708+
xit('should write to input only if value is different', (Injector i, Scope scope) {
709+
var element = _.compile('<textarea ng-model=model></textarea>');
791710
dom.querySelector('body').append(element);
792-
var input = new InputTextLike(element, model, scope, ngModelOptions);
793711

794-
element
795-
..value = 'abc'
796-
..selectionStart = 1
797-
..selectionEnd = 2;
712+
element..value = 'abc'
713+
..selectionStart = 1
714+
..selectionEnd = 2;
798715

799-
model.render('abc');
716+
scope.apply('model = "abc"');
800717

801718
expect(element.value).toEqual('abc');
802719
expect(element.selectionStart).toEqual(1);
803720
expect(element.selectionEnd).toEqual(2);
804721

805-
model.render('xyz');
722+
scope.apply('model = "xyz"');
806723

807724
// Setting the value on a textarea doesn't update the selection the way it
808725
// does on input elements. This stays unchanged.

0 commit comments

Comments
 (0)