2015-05-30 14:56:00 -04:00
|
|
|
import {Component, Directive, View} from 'angular2/angular2';
|
2015-05-22 15:32:49 -04:00
|
|
|
import {
|
|
|
|
afterEach,
|
|
|
|
AsyncTestCompleter,
|
2015-06-25 00:51:37 -04:00
|
|
|
TestComponentBuilder,
|
|
|
|
By,
|
2015-05-22 15:32:49 -04:00
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
dispatchEvent,
|
2015-05-30 14:56:00 -04:00
|
|
|
fakeAsync,
|
2015-05-31 15:24:34 -04:00
|
|
|
tick,
|
2015-05-22 15:32:49 -04:00
|
|
|
expect,
|
|
|
|
it,
|
2015-05-31 15:24:34 -04:00
|
|
|
inject,
|
|
|
|
iit,
|
2015-05-22 15:32:49 -04:00
|
|
|
xit
|
|
|
|
} from 'angular2/test_lib';
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-03 14:56:01 -04:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2015-06-13 15:15:42 -04:00
|
|
|
import {NgIf, NgFor} from 'angular2/directives';
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
import {
|
|
|
|
Control,
|
|
|
|
ControlGroup,
|
2015-06-10 16:51:44 -04:00
|
|
|
NgForm,
|
2015-05-30 14:56:00 -04:00
|
|
|
formDirectives,
|
2015-05-22 15:32:49 -04:00
|
|
|
Validators,
|
2015-06-10 16:51:44 -04:00
|
|
|
NgControl,
|
2015-05-30 14:56:00 -04:00
|
|
|
ControlValueAccessor
|
2015-05-22 15:32:49 -04:00
|
|
|
} from 'angular2/forms';
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe("integration tests", () => {
|
|
|
|
it("should initialize DOM elements with the given form object",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="text" ng-control="login">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({"login": new Control("loginValue")});
|
|
|
|
rootTC.detectChanges();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("loginValue");
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it("should update the control group values on DOM change",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-22 15:32:49 -04:00
|
|
|
var form = new ControlGroup({"login": new Control("oldValue")});
|
|
|
|
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="text" ng-control="login">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
var input = rootTC.query(By.css("input"));
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.nativeElement.value = "updatedValue";
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(form.value).toEqual({"login": "updatedValue"});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2015-06-05 17:29:50 -04:00
|
|
|
it("should emit ng-submit event on submit",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
|
|
|
var t =
|
|
|
|
`<div><form [ng-form-model]="form" (ng-submit)="name='updated'"></form><span>{{name}}</span></div>`;
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var rootTC;
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { rootTC = root; });
|
|
|
|
tick();
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({});
|
|
|
|
rootTC.componentInstance.name = 'old';
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tick();
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var form = rootTC.query(By.css("form"));
|
|
|
|
dispatchEvent(form.nativeElement, "submit");
|
|
|
|
|
|
|
|
tick();
|
|
|
|
expect(rootTC.componentInstance.name).toEqual('updated');
|
|
|
|
})));
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it("should work with single controls",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-22 15:32:49 -04:00
|
|
|
var control = new Control("loginValue");
|
|
|
|
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div><input type="text" [ng-form-control]="form"></div>`;
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = control;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
|
|
|
|
var input = rootTC.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("loginValue");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.nativeElement.value = "updatedValue";
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(control.value).toEqual("updatedValue");
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it("should update DOM elements when rebinding the control group",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="text" ng-control="login">
|
2015-05-30 14:56:00 -04:00
|
|
|
</div>`;
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({"login": new Control("oldValue")});
|
|
|
|
rootTC.detectChanges();
|
|
|
|
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({"login": new Control("newValue")});
|
|
|
|
rootTC.detectChanges();
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("newValue");
|
|
|
|
async.done();
|
|
|
|
});
|
2015-06-01 13:41:50 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it("should update DOM elements when updating the value of a control",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-01 13:41:50 -04:00
|
|
|
var login = new Control("oldValue");
|
|
|
|
var form = new ControlGroup({"login": login});
|
|
|
|
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="text" ng-control="login">
|
2015-06-01 13:41:50 -04:00
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
2015-06-01 13:41:50 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
login.updateValue("newValue");
|
2015-06-01 13:41:50 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
rootTC.detectChanges();
|
2015-06-01 13:41:50 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("newValue");
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2015-06-02 11:41:33 -04:00
|
|
|
it("should mark controls as touched after interacting with the DOM control",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-02 11:41:33 -04:00
|
|
|
var login = new Control("oldValue");
|
|
|
|
var form = new ControlGroup({"login": login});
|
|
|
|
|
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="text" ng-control="login">
|
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
2015-06-02 11:41:33 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var loginEl = rootTC.query(By.css("input"));
|
|
|
|
expect(login.touched).toBe(false);
|
2015-06-02 11:41:33 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
dispatchEvent(loginEl.nativeElement, "blur");
|
2015-06-02 11:41:33 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(login.touched).toBe(true);
|
2015-06-02 11:41:33 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2015-06-02 11:41:33 -04:00
|
|
|
}));
|
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
describe("different control types", () => {
|
2015-06-11 21:50:41 -04:00
|
|
|
it("should support <input type=text>",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="text" ng-control="text">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({"text": new Control("old")});
|
|
|
|
rootTC.detectChanges();
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("old");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.nativeElement.value = "new";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
|
|
|
|
|
|
|
expect(rootTC.componentInstance.form.value).toEqual({"text": "new"});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it("should support <input> without type",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input ng-control="text">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({"text": new Control("old")});
|
|
|
|
rootTC.detectChanges();
|
|
|
|
var input = rootTC.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("old");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.nativeElement.value = "new";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(rootTC.componentInstance.form.value).toEqual({"text": "new"});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it("should support <textarea>",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<textarea ng-control="text"></textarea>
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({"text": new Control('old')});
|
|
|
|
rootTC.detectChanges();
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var textarea = rootTC.query(By.css("textarea"));
|
|
|
|
expect(textarea.nativeElement.value).toEqual("old");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
textarea.nativeElement.value = "new";
|
|
|
|
dispatchEvent(textarea.nativeElement, "input");
|
|
|
|
|
|
|
|
expect(rootTC.componentInstance.form.value).toEqual({"text": 'new'});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it("should support <type=checkbox>",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="checkbox" ng-control="checkbox">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({"checkbox": new Control(true)});
|
|
|
|
rootTC.detectChanges();
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.checked).toBe(true);
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.nativeElement.checked = false;
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(rootTC.componentInstance.form.value).toEqual({"checkbox": false});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
it("should support <select>",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<select ng-control="city">
|
2015-05-22 15:32:49 -04:00
|
|
|
<option value="SF"></option>
|
|
|
|
<option value="NYC"></option>
|
|
|
|
</select>
|
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({"city": new Control("SF")});
|
|
|
|
rootTC.detectChanges();
|
|
|
|
|
|
|
|
var select = rootTC.query(By.css("select"));
|
|
|
|
var sfOption = rootTC.query(By.css("option"));
|
|
|
|
expect(select.nativeElement.value).toEqual('SF');
|
|
|
|
expect(sfOption.nativeElement.selected).toBe(true);
|
|
|
|
|
|
|
|
select.nativeElement.value = 'NYC';
|
|
|
|
dispatchEvent(select.nativeElement, "change");
|
|
|
|
|
|
|
|
expect(rootTC.componentInstance.form.value).toEqual({"city": 'NYC'});
|
|
|
|
expect(sfOption.nativeElement.selected).toBe(false);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2015-06-13 15:15:42 -04:00
|
|
|
it("should support <select> with a dynamic list of options",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div [ng-form-model]="form">
|
2015-06-13 15:15:42 -04:00
|
|
|
<select ng-control="city">
|
|
|
|
<option *ng-for="#c of data" [value]="c"></option>
|
|
|
|
</select>
|
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({"city": new Control("NYC")});
|
|
|
|
rootTC.componentInstance.data = ['SF', 'NYC'];
|
|
|
|
rootTC.detectChanges();
|
2015-06-13 15:15:42 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var select = rootTC.query(By.css('select'));
|
|
|
|
expect(select.nativeElement.value).toEqual('NYC');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-06-13 15:15:42 -04:00
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
it("should support custom value accessors",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="text" ng-control="name" wrapped-value>
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = new ControlGroup({"name": new Control("aa")});
|
|
|
|
rootTC.detectChanges();
|
|
|
|
var input = rootTC.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("!aa!");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.nativeElement.value = "!bb!";
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(rootTC.componentInstance.form.value).toEqual({"name": "bb"});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("validations", () => {
|
|
|
|
it("should use validators defined in html",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-22 15:32:49 -04:00
|
|
|
var form = new ControlGroup({"login": new Control("aa")});
|
|
|
|
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="text" ng-control="login" required>
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(form.valid).toEqual(true);
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input"));
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.nativeElement.value = "";
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(form.valid).toEqual(false);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it("should use validators defined in the model",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-22 15:32:49 -04:00
|
|
|
var form = new ControlGroup({"login": new Control("aa", Validators.required)});
|
|
|
|
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="text" ng-control="login">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(form.valid).toEqual(true);
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input"));
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.nativeElement.value = "";
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(form.valid).toEqual(false);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("nested forms", () => {
|
|
|
|
it("should init DOM with the given form object",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-22 15:32:49 -04:00
|
|
|
var form =
|
|
|
|
new ControlGroup({"nested": new ControlGroup({"login": new Control("value")})});
|
|
|
|
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<div ng-control-group="nested">
|
|
|
|
<input type="text" ng-control="login">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
|
|
|
|
var input = rootTC.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("value");
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it("should update the control group values on DOM change",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-22 15:32:49 -04:00
|
|
|
var form =
|
|
|
|
new ControlGroup({"nested": new ControlGroup({"login": new Control("value")})});
|
|
|
|
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<div ng-control-group="nested">
|
|
|
|
<input type="text" ng-control="login">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
var input = rootTC.query(By.css("input"));
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.nativeElement.value = "updatedValue";
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(form.value).toEqual({"nested": {"login": "updatedValue"}});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
});
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-05-31 15:24:34 -04:00
|
|
|
it("should support ng-model for complex forms",
|
|
|
|
inject(
|
2015-06-25 00:51:37 -04:00
|
|
|
[TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
2015-05-31 15:24:34 -04:00
|
|
|
var form = new ControlGroup({"name": new Control("")});
|
|
|
|
|
|
|
|
var t =
|
2015-06-02 17:53:49 -04:00
|
|
|
`<div [ng-form-model]="form"><input type="text" ng-control="name" [(ng-model)]="name"></div>`;
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var rootTC;
|
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { rootTC = root; });
|
|
|
|
tick();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
rootTC.componentInstance.name = 'oldValue';
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input")).nativeElement;
|
|
|
|
expect(input.value).toEqual("oldValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tick();
|
|
|
|
expect(rootTC.componentInstance.name).toEqual("updatedValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
})));
|
|
|
|
|
|
|
|
it("should support ng-model for single fields",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
|
|
|
var form = new Control("");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var t = `<div><input type="text" [ng-form-control]="form" [(ng-model)]="name"></div>`;
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var rootTC;
|
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { rootTC = root; });
|
|
|
|
tick();
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.componentInstance.name = "oldValue";
|
|
|
|
rootTC.detectChanges();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input")).nativeElement;
|
|
|
|
expect(input.value).toEqual("oldValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
tick();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(rootTC.componentInstance.name).toEqual("updatedValue");
|
|
|
|
})));
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-05-30 14:56:00 -04:00
|
|
|
describe("template-driven forms", () => {
|
|
|
|
it("should add new controls and control groups",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
2015-06-01 16:11:34 -04:00
|
|
|
var t = `<form>
|
2015-06-02 17:53:49 -04:00
|
|
|
<div ng-control-group="user">
|
|
|
|
<input type="text" ng-control="login">
|
2015-05-30 14:56:00 -04:00
|
|
|
</div>
|
2015-06-01 16:11:34 -04:00
|
|
|
</form>`;
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var rootTC;
|
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
|
|
|
(root) => { rootTC = root; });
|
|
|
|
tick();
|
|
|
|
rootTC.componentInstance.name = null;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
|
|
|
|
var form = rootTC.componentViewChildren[0].inject(NgForm);
|
|
|
|
expect(form.controls['user']).not.toBeDefined();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tick();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(form.controls['user']).toBeDefined();
|
|
|
|
expect(form.controls['user'].controls['login']).toBeDefined();
|
2015-05-30 14:56:00 -04:00
|
|
|
})));
|
|
|
|
|
2015-06-05 17:29:50 -04:00
|
|
|
it("should emit ng-submit event on submit",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
2015-06-05 17:29:50 -04:00
|
|
|
var t = `<div><form (ng-submit)="name='updated'"></form></div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var rootTC;
|
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
|
|
|
(root) => { rootTC = root; });
|
|
|
|
tick();
|
|
|
|
rootTC.componentInstance.name = 'old';
|
|
|
|
var form = rootTC.query(By.css("form"));
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
dispatchEvent(form.nativeElement, "submit");
|
|
|
|
tick();
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(rootTC.componentInstance.name).toEqual("updated");
|
2015-06-05 17:29:50 -04:00
|
|
|
})));
|
|
|
|
|
2015-06-01 16:11:34 -04:00
|
|
|
it("should not create a template-driven form when ng-no-form is used",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<form ng-no-form>
|
2015-06-01 16:11:34 -04:00
|
|
|
</form>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.name = null;
|
|
|
|
rootTC.detectChanges();
|
2015-06-01 16:11:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(rootTC.componentViewChildren.length).toEqual(0);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
it("should remove controls",
|
|
|
|
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<form>
|
2015-05-30 14:56:00 -04:00
|
|
|
<div *ng-if="name == 'show'">
|
2015-06-02 17:53:49 -04:00
|
|
|
<input type="text" ng-control="login">
|
2015-05-30 14:56:00 -04:00
|
|
|
</div>
|
2015-06-01 16:11:34 -04:00
|
|
|
</form>`;
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var rootTC;
|
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
|
|
|
(root) => { rootTC = root; });
|
|
|
|
tick();
|
|
|
|
rootTC.componentInstance.name = 'show';
|
|
|
|
rootTC.detectChanges();
|
|
|
|
tick();
|
|
|
|
var form = rootTC.componentViewChildren[0].inject(NgForm);
|
2015-05-30 14:56:00 -04:00
|
|
|
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(form.controls['login']).toBeDefined();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
rootTC.componentInstance.name = 'hide';
|
|
|
|
rootTC.detectChanges();
|
|
|
|
tick();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(form.controls['login']).not.toBeDefined();
|
|
|
|
})));
|
2015-05-30 14:56:00 -04:00
|
|
|
|
|
|
|
it("should remove control groups",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
2015-06-01 16:11:34 -04:00
|
|
|
var t = `<form>
|
2015-06-02 17:53:49 -04:00
|
|
|
<div *ng-if="name=='show'" ng-control-group="user">
|
|
|
|
<input type="text" ng-control="login">
|
2015-05-30 14:56:00 -04:00
|
|
|
</div>
|
2015-06-01 16:11:34 -04:00
|
|
|
</form>`;
|
2015-05-30 14:56:00 -04:00
|
|
|
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var rootTC;
|
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
|
|
|
(root) => { rootTC = root; });
|
|
|
|
tick();
|
|
|
|
rootTC.componentInstance.name = 'show';
|
|
|
|
rootTC.detectChanges();
|
|
|
|
tick();
|
|
|
|
var form = rootTC.componentViewChildren[0].inject(NgForm);
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(form.controls['user']).toBeDefined();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
rootTC.componentInstance.name = 'hide';
|
|
|
|
rootTC.detectChanges();
|
|
|
|
tick();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(form.controls['user']).not.toBeDefined();
|
2015-05-30 14:56:00 -04:00
|
|
|
})));
|
2015-05-31 15:24:34 -04:00
|
|
|
|
|
|
|
it("should support ng-model for complex forms",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
2015-06-02 17:53:49 -04:00
|
|
|
var t = `<form>
|
|
|
|
<input type="text" ng-control="name" [(ng-model)]="name">
|
|
|
|
</form>`;
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var rootTC;
|
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
|
|
|
(root) => { rootTC = root; });
|
|
|
|
tick();
|
|
|
|
rootTC.componentInstance.name = "oldValue";
|
|
|
|
rootTC.detectChanges();
|
|
|
|
tick();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input")).nativeElement;
|
|
|
|
expect(input.value).toEqual("oldValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
tick();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(rootTC.componentInstance.name).toEqual("updatedValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
|
|
it("should support ng-model for single fields",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
2015-05-31 15:24:34 -04:00
|
|
|
var t = `<div><input type="text" [(ng-model)]="name"></div>`;
|
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var rootTC;
|
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
|
|
|
(root) => { rootTC = root; });
|
|
|
|
tick();
|
|
|
|
rootTC.componentInstance.name = "oldValue";
|
|
|
|
rootTC.detectChanges();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input")).nativeElement;
|
|
|
|
expect(input.value).toEqual("oldValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
tick();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(rootTC.componentInstance.name).toEqual("updatedValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
})));
|
2015-05-30 14:56:00 -04:00
|
|
|
});
|
2015-06-03 14:56:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
describe("setting status classes", () => {
|
|
|
|
it("should work with single fields",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var form = new Control("", Validators.required);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var t = `<div><input type="text" [ng-form-control]="form"></div>`;
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input")).nativeElement;
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(['ng-binding', 'ng-untouched', 'ng-pristine', 'ng-invalid']);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
dispatchEvent(input, "blur");
|
|
|
|
rootTC.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-pristine", "ng-invalid", "ng-touched"]);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
rootTC.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-touched", "ng-dirty", "ng-valid"]);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-06-03 14:56:01 -04:00
|
|
|
|
|
|
|
it("should work with complex model-driven forms",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var form = new ControlGroup({"name": new Control("", Validators.required)});
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var t = `<form [ng-form-model]="form"><input type="text" ng-control="name"></form>`;
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input")).nativeElement;
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-untouched", "ng-pristine", "ng-invalid"]);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
dispatchEvent(input, "blur");
|
|
|
|
rootTC.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-pristine", "ng-invalid", "ng-touched"]);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
rootTC.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-touched", "ng-dirty", "ng-valid"]);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-06-03 14:56:01 -04:00
|
|
|
|
|
|
|
it("should work with ng-model",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div><input [(ng-model)]="name" required></div>`;
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
|
|
|
|
rootTC.componentInstance.name = "";
|
|
|
|
rootTC.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
var input = rootTC.query(By.css("input")).nativeElement;
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-untouched", "ng-pristine", "ng-invalid"]);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
dispatchEvent(input, "blur");
|
|
|
|
rootTC.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-pristine", "ng-invalid", "ng-touched"]);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
rootTC.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-06-25 00:51:37 -04:00
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-touched", "ng-dirty", "ng-valid"]);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-06-03 14:56:01 -04:00
|
|
|
});
|
2015-07-15 21:16:50 -04:00
|
|
|
|
|
|
|
describe("ng-model corner cases", () => {
|
|
|
|
it("should not update the view when the value initially came from the view",
|
|
|
|
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
|
|
|
var form = new Control("");
|
|
|
|
|
|
|
|
var t =
|
|
|
|
`<div><input type="text" [ng-form-control]="form" [(ng-model)]="name"></div>`;
|
|
|
|
var rootTC;
|
|
|
|
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
|
|
|
(root) => { rootTC = root; });
|
|
|
|
tick();
|
|
|
|
rootTC.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
|
|
|
|
var input = rootTC.query(By.css("input")).nativeElement;
|
|
|
|
input.value = "aa";
|
|
|
|
input.selectionStart = 1;
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
tick();
|
|
|
|
rootTC.detectChanges();
|
|
|
|
|
|
|
|
// selection start has not changed because we did not reset the value
|
|
|
|
expect(input.selectionStart).toEqual(1);
|
|
|
|
})));
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[wrapped-value]',
|
2015-06-09 06:33:40 -04:00
|
|
|
host: {'(change)': 'handleOnChange($event.target.value)', '[value]': 'value'}
|
2015-05-22 15:32:49 -04:00
|
|
|
})
|
2015-05-30 14:56:00 -04:00
|
|
|
class WrappedValue implements ControlValueAccessor {
|
2015-05-22 15:32:49 -04:00
|
|
|
value;
|
|
|
|
onChange: Function;
|
|
|
|
|
2015-06-10 16:51:44 -04:00
|
|
|
constructor(cd: NgControl) { cd.valueAccessor = this; }
|
2015-05-22 15:32:49 -04:00
|
|
|
|
|
|
|
writeValue(value) { this.value = `!${value}!`; }
|
|
|
|
|
2015-05-30 14:56:00 -04:00
|
|
|
registerOnChange(fn) { this.onChange = fn; }
|
2015-06-02 11:41:33 -04:00
|
|
|
registerOnTouched(fn) {}
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
handleOnChange(value) { this.onChange(value.substring(1, value.length - 1)); }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: "my-comp"})
|
2015-06-13 15:15:42 -04:00
|
|
|
@View({directives: [formDirectives, WrappedValue, NgIf, NgFor]})
|
2015-05-22 15:32:49 -04:00
|
|
|
class MyComp {
|
|
|
|
form: any;
|
|
|
|
name: string;
|
2015-06-13 15:15:42 -04:00
|
|
|
data: any;
|
2015-06-04 03:58:22 -04:00
|
|
|
}
|