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,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
dispatchEvent,
|
2015-05-30 14:56:00 -04:00
|
|
|
fakeAsync,
|
|
|
|
flushMicrotasks,
|
2015-05-31 15:24:34 -04:00
|
|
|
tick,
|
2015-05-22 15:32:49 -04:00
|
|
|
el,
|
|
|
|
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-05-22 15:32:49 -04:00
|
|
|
import {TestBed} from 'angular2/src/test_lib/test_bed';
|
2015-05-30 14:56:00 -04:00
|
|
|
import {NgIf} from 'angular2/directives';
|
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
import {
|
|
|
|
Control,
|
|
|
|
ControlGroup,
|
|
|
|
RequiredValidatorDirective,
|
2015-05-30 14:56:00 -04:00
|
|
|
TemplateDrivenFormDirective,
|
|
|
|
formDirectives,
|
2015-05-22 15:32:49 -04:00
|
|
|
Validators,
|
2015-05-30 14:56:00 -04:00
|
|
|
ControlDirective,
|
|
|
|
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",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var ctx = MyComp.create({form: new ControlGroup({"login": new Control("loginValue")})});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("loginValue");
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should update the control group values on DOM change",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var form = new ControlGroup({"login": new Control("oldValue")});
|
|
|
|
var ctx = MyComp.create({form: form});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
|
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
expect(form.value).toEqual({"login": "updatedValue"});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should work with single controls", inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var control = new Control("loginValue");
|
|
|
|
var ctx = MyComp.create({form: control});
|
|
|
|
|
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
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("loginValue");
|
|
|
|
|
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
expect(control.value).toEqual("updatedValue");
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should update DOM elements when rebinding the control group",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var form = new ControlGroup({"login": new Control("oldValue")});
|
|
|
|
var ctx = MyComp.create({form: form});
|
|
|
|
|
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
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
ctx.form = new ControlGroup({"login": new Control("newValue")});
|
|
|
|
view.detectChanges();
|
|
|
|
|
2015-06-01 13:41:50 -04:00
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("newValue");
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should update DOM elements when updating the value of a control",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var login = new Control("oldValue");
|
|
|
|
var form = new ControlGroup({"login": login});
|
|
|
|
var ctx = MyComp.create({form: form});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
login.updateValue("newValue");
|
|
|
|
|
|
|
|
view.detectChanges();
|
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("newValue");
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-02 11:41:33 -04:00
|
|
|
it("should mark controls as touched after interacting with the DOM control",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var login = new Control("oldValue");
|
|
|
|
var form = new ControlGroup({"login": login});
|
|
|
|
var ctx = MyComp.create({form: form});
|
|
|
|
|
|
|
|
var t = `<div [ng-form-model]="form">
|
|
|
|
<input type="text" ng-control="login">
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
var loginEl = view.querySelector("input");
|
|
|
|
|
|
|
|
expect(login.touched).toBe(false);
|
|
|
|
|
|
|
|
dispatchEvent(loginEl, "blur");
|
|
|
|
|
|
|
|
expect(login.touched).toBe(true);
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
describe("different control types", () => {
|
|
|
|
it("should support <input type=text>", inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var ctx = MyComp.create({form: new ControlGroup({"text": new Control("old")})});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("old");
|
|
|
|
|
|
|
|
input.value = "new";
|
|
|
|
dispatchEvent(input, "input");
|
|
|
|
|
|
|
|
expect(ctx.form.value).toEqual({"text": "new"});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should support <input> without type",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var ctx = MyComp.create({form: new ControlGroup({"text": new Control("old")})});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("old");
|
|
|
|
|
|
|
|
input.value = "new";
|
|
|
|
dispatchEvent(input, "input");
|
|
|
|
|
|
|
|
expect(ctx.form.value).toEqual({"text": "new"});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should support <textarea>", inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var ctx = MyComp.create({form: new ControlGroup({"text": new Control('old')})});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var textarea = view.querySelector("textarea");
|
|
|
|
expect(textarea.value).toEqual("old");
|
|
|
|
|
|
|
|
textarea.value = "new";
|
|
|
|
dispatchEvent(textarea, "input");
|
|
|
|
|
|
|
|
expect(ctx.form.value).toEqual({"text": 'new'});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should support <type=checkbox>", inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var ctx = MyComp.create({form: new ControlGroup({"checkbox": new Control(true)})});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.checked).toBe(true);
|
|
|
|
|
|
|
|
input.checked = false;
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
expect(ctx.form.value).toEqual({"checkbox": false});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should support <select>", inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var ctx = MyComp.create({form: new ControlGroup({"city": new Control("SF")})});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var select = view.querySelector("select");
|
|
|
|
var sfOption = view.querySelector("option");
|
|
|
|
expect(select.value).toEqual('SF');
|
2015-05-30 14:56:00 -04:00
|
|
|
expect(sfOption.selected).toBe(true);
|
2015-05-22 15:32:49 -04:00
|
|
|
|
|
|
|
select.value = 'NYC';
|
|
|
|
dispatchEvent(select, "change");
|
|
|
|
|
|
|
|
expect(ctx.form.value).toEqual({"city": 'NYC'});
|
2015-05-30 14:56:00 -04:00
|
|
|
expect(sfOption.selected).toBe(false);
|
2015-05-22 15:32:49 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should support custom value accessors",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var ctx = MyComp.create({form: new ControlGroup({"name": new Control("aa")})});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("!aa!");
|
|
|
|
|
|
|
|
input.value = "!bb!";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
expect(ctx.form.value).toEqual({"name": "bb"});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("validations", () => {
|
|
|
|
it("should use validators defined in html",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var form = new ControlGroup({"login": new Control("aa")});
|
|
|
|
var ctx = MyComp.create({form: form});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
expect(form.valid).toEqual(true);
|
|
|
|
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
|
|
|
|
input.value = "";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
expect(form.valid).toEqual(false);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should use validators defined in the model",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var form = new ControlGroup({"login": new Control("aa", Validators.required)});
|
|
|
|
var ctx = MyComp.create({form: form});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
expect(form.valid).toEqual(true);
|
|
|
|
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
|
|
|
|
input.value = "";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
expect(form.valid).toEqual(false);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("nested forms", () => {
|
|
|
|
it("should init DOM with the given form object",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var form =
|
|
|
|
new ControlGroup({"nested": new ControlGroup({"login": new Control("value")})});
|
|
|
|
var ctx = MyComp.create({form: form});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("value");
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should update the control group values on DOM change",
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var form =
|
|
|
|
new ControlGroup({"nested": new ControlGroup({"login": new Control("value")})});
|
|
|
|
var ctx = MyComp.create({form: form});
|
|
|
|
|
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>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
2015-05-30 14:56:00 -04:00
|
|
|
var input = view.querySelector("input");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-05-30 14:56:00 -04:00
|
|
|
input.value = "updatedValue";
|
2015-05-22 15:32:49 -04:00
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
expect(form.value).toEqual({"nested": {"login": "updatedValue"}});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
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(
|
|
|
|
[TestBed], fakeAsync(tb => {
|
|
|
|
var form = new ControlGroup({"name": new Control("")});
|
|
|
|
var ctx = MyComp.create({name: "oldValue", form: form});
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("oldValue");
|
|
|
|
|
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
expect(ctx.name).toEqual("updatedValue");
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
|
|
|
|
it("should support ng-model for single fields",
|
|
|
|
inject([TestBed], fakeAsync(tb => {
|
|
|
|
var form = new Control("");
|
|
|
|
var ctx = MyComp.create({name: "oldValue", form: form});
|
|
|
|
|
2015-06-02 17:53:49 -04:00
|
|
|
var t =
|
|
|
|
`<div><input type="text" [ng-form-control]="form" [(ng-model)]="name"></div>`;
|
2015-05-31 15:24:34 -04:00
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("oldValue");
|
|
|
|
|
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
expect(ctx.name).toEqual("updatedValue");
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
|
2015-05-30 14:56:00 -04:00
|
|
|
describe("template-driven forms", () => {
|
|
|
|
it("should add new controls and control groups",
|
|
|
|
inject([TestBed], fakeAsync(tb => {
|
|
|
|
var ctx = MyComp.create({name: null});
|
|
|
|
|
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
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var form =
|
|
|
|
view.rawView.elementInjectors[0].get(TemplateDrivenFormDirective);
|
|
|
|
expect(form.controls['user']).not.toBeDefined();
|
|
|
|
|
2015-05-31 15:24:34 -04:00
|
|
|
tick();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
|
|
|
expect(form.controls['user']).toBeDefined();
|
|
|
|
expect(form.controls['user'].controls['login']).toBeDefined();
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
|
2015-06-01 16:11:34 -04:00
|
|
|
it("should not create a template-driven form when ng-no-form is used",
|
|
|
|
inject([TestBed], fakeAsync(tb => {
|
|
|
|
var ctx = MyComp.create({name: null});
|
|
|
|
|
|
|
|
var t = `<form ng-no-form>
|
|
|
|
</form>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
expect(view.rawView.elementInjectors.length).toEqual(0);
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
|
2015-05-30 14:56:00 -04:00
|
|
|
it("should remove controls", inject([TestBed], fakeAsync(tb => {
|
|
|
|
var ctx = MyComp.create({name: 'show'});
|
|
|
|
|
2015-06-01 16:11:34 -04:00
|
|
|
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
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var form = view.rawView.elementInjectors[0].get(
|
|
|
|
TemplateDrivenFormDirective);
|
|
|
|
|
2015-05-31 15:24:34 -04:00
|
|
|
tick();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
|
|
|
expect(form.controls['login']).toBeDefined();
|
|
|
|
|
|
|
|
ctx.name = 'hide';
|
|
|
|
view.detectChanges();
|
2015-05-31 15:24:34 -04:00
|
|
|
tick();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
|
|
|
expect(form.controls['login']).not.toBeDefined();
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
|
|
|
|
it("should remove control groups",
|
|
|
|
inject([TestBed], fakeAsync(tb => {
|
|
|
|
var ctx = MyComp.create({name: 'show'});
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
var form =
|
|
|
|
view.rawView.elementInjectors[0].get(TemplateDrivenFormDirective);
|
|
|
|
flushMicrotasks();
|
|
|
|
|
|
|
|
expect(form.controls['user']).toBeDefined();
|
|
|
|
|
|
|
|
ctx.name = 'hide';
|
|
|
|
view.detectChanges();
|
|
|
|
flushMicrotasks();
|
|
|
|
|
|
|
|
expect(form.controls['user']).not.toBeDefined();
|
|
|
|
});
|
|
|
|
flushMicrotasks();
|
|
|
|
})));
|
2015-05-31 15:24:34 -04:00
|
|
|
|
|
|
|
it("should support ng-model for complex forms",
|
|
|
|
inject([TestBed], fakeAsync(tb => {
|
|
|
|
var ctx = MyComp.create({name: "oldValue"});
|
|
|
|
|
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
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
tick();
|
2015-06-03 14:56:01 -04:00
|
|
|
view.detectChanges();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("oldValue");
|
|
|
|
|
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
expect(ctx.name).toEqual("updatedValue");
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
|
|
it("should support ng-model for single fields",
|
|
|
|
inject([TestBed], fakeAsync(tb => {
|
|
|
|
var ctx = MyComp.create({name: "oldValue"});
|
|
|
|
|
|
|
|
var t = `<div><input type="text" [(ng-model)]="name"></div>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(input.value).toEqual("oldValue");
|
|
|
|
|
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
expect(ctx.name).toEqual("updatedValue");
|
|
|
|
});
|
|
|
|
flushMicrotasks();
|
|
|
|
})));
|
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",
|
|
|
|
inject([TestBed], fakeAsync(tb => {
|
|
|
|
var form = new Control("", Validators.required);
|
|
|
|
var ctx = MyComp.create({form: form});
|
|
|
|
|
|
|
|
var t = `<div><input type="text" [ng-form-control]="form"></div>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-untouched", "ng-pristine", "ng-invalid"]);
|
|
|
|
|
|
|
|
dispatchEvent(input, "blur");
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-pristine", "ng-invalid", "ng-touched"]);
|
|
|
|
|
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-touched", "ng-dirty", "ng-valid"]);
|
|
|
|
tick();
|
|
|
|
});
|
|
|
|
flushMicrotasks();
|
|
|
|
})));
|
|
|
|
|
|
|
|
it("should work with complex model-driven forms",
|
|
|
|
inject([TestBed], fakeAsync(tb => {
|
|
|
|
var form = new ControlGroup({"name": new Control("", Validators.required)});
|
|
|
|
var ctx = MyComp.create({form: form});
|
|
|
|
|
|
|
|
var t =
|
|
|
|
`<form [ng-form-model]="form"><input type="text" ng-control="name"></form>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-untouched", "ng-pristine", "ng-invalid"]);
|
|
|
|
|
|
|
|
dispatchEvent(input, "blur");
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-pristine", "ng-invalid", "ng-touched"]);
|
|
|
|
|
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-touched", "ng-dirty", "ng-valid"]);
|
|
|
|
tick();
|
|
|
|
});
|
|
|
|
flushMicrotasks();
|
|
|
|
})));
|
|
|
|
|
|
|
|
it("should work with ng-model",
|
|
|
|
inject([TestBed], fakeAsync(tb => {
|
|
|
|
var ctx = MyComp.create({name: ""});
|
|
|
|
|
|
|
|
var t = `<div><input [(ng-model)]="name" required></div>`;
|
|
|
|
|
|
|
|
tb.createView(MyComp, {context: ctx, html: t})
|
|
|
|
.then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
var input = view.querySelector("input");
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-untouched", "ng-pristine", "ng-invalid"]);
|
|
|
|
|
|
|
|
dispatchEvent(input, "blur");
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-pristine", "ng-invalid", "ng-touched"]);
|
|
|
|
|
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
view.detectChanges();
|
|
|
|
|
|
|
|
expect(DOM.classList(input))
|
|
|
|
.toEqual(["ng-binding", "ng-touched", "ng-dirty", "ng-valid"]);
|
|
|
|
tick();
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[wrapped-value]',
|
|
|
|
hostListeners: {'change': 'handleOnChange($event.target.value)'},
|
|
|
|
hostProperties: {'value': 'value'}
|
|
|
|
})
|
2015-05-30 14:56:00 -04:00
|
|
|
class WrappedValue implements ControlValueAccessor {
|
2015-05-22 15:32:49 -04:00
|
|
|
value;
|
|
|
|
onChange: Function;
|
|
|
|
|
|
|
|
constructor(cd: ControlDirective) { cd.valueAccessor = this; }
|
|
|
|
|
|
|
|
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-05-30 14:56:00 -04:00
|
|
|
@View({directives: [formDirectives, WrappedValue, RequiredValidatorDirective, NgIf]})
|
2015-05-22 15:32:49 -04:00
|
|
|
class MyComp {
|
|
|
|
form: any;
|
|
|
|
name: string;
|
|
|
|
|
2015-05-30 14:56:00 -04:00
|
|
|
static create({form, name}: {form?: any, name?: any}) {
|
2015-05-22 15:32:49 -04:00
|
|
|
var mc = new MyComp();
|
|
|
|
mc.form = form;
|
|
|
|
mc.name = name;
|
|
|
|
return mc;
|
|
|
|
}
|
2015-06-04 03:58:22 -04:00
|
|
|
}
|