Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

fix(ng-model): Add input type="search" #468

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/directive/ng_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class InputCheckboxDirective {
@NgDirective(selector: 'input[type=url][ng-model]')
@NgDirective(selector: 'input[type=email][ng-model]')
@NgDirective(selector: 'input[type=number][ng-model]')
@NgDirective(selector: 'input[type=search][ng-model]')
class InputTextLikeDirective {
final dom.Element inputElement;
final NgModel ngModel;
Expand Down
64 changes: 64 additions & 0 deletions test/directive/ng_model_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,70 @@ describe('ng-model', () {
expect(element.selectionEnd).toEqual(3);
}));
});

describe('type="search"', () {
it('should update input value from model', inject(() {
_.compile('<input type="search" ng-model="model">');
_.rootScope.$digest();

expect((_.rootElement as dom.InputElement).value).toEqual('');

_.rootScope.$apply('model = "misko"');
expect((_.rootElement as dom.InputElement).value).toEqual('misko');
}));

it('should render null as the empty string', inject(() {
_.compile('<input type="search" ng-model="model">');
_.rootScope.$digest();

expect((_.rootElement as dom.InputElement).value).toEqual('');

_.rootScope.$apply('model = null');
expect((_.rootElement as dom.InputElement).value).toEqual('');
}));

it('should update model from the input value', inject(() {
_.compile('<input type="search" ng-model="model" probe="p">');
Probe probe = _.rootScope.p;
var ngModel = probe.directive(NgModel);
InputElement inputElement = probe.element;

inputElement.value = 'abc';
_.triggerEvent(inputElement, 'change');
expect(_.rootScope.model).toEqual('abc');

inputElement.value = 'def';
var input = probe.directive(InputTextLikeDirective);
input.processValue();
expect(_.rootScope.model).toEqual('def');
}));

it('should write to input only if value is different', inject(() {
var scope = _.rootScope;
var element = new dom.InputElement();
var model = new NgModel(scope, new NodeAttrs(new DivElement()), element, new NgNullForm());
dom.querySelector('body').append(element);
var input = new InputTextLikeDirective(element, model, scope);

element.value = 'abc';
element.selectionStart = 1;
element.selectionEnd = 2;

model.render('abc');

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

model.render('xyz');

// Value updated. selectionStart/End changed.
expect(element.value).toEqual('xyz');
expect(element.selectionStart).toEqual(3);
expect(element.selectionEnd).toEqual(3);
}));
});

describe('type="checkbox"', () {
it('should update input value from model', inject((Scope scope) {
Expand Down