import {Component, Directive, View} from 'angular2/angular2';
import {
afterEach,
AsyncTestCompleter,
TestComponentBuilder,
By,
beforeEach,
ddescribe,
describe,
dispatchEvent,
fakeAsync,
tick,
expect,
it,
inject,
iit,
xit
} from 'angular2/test_lib';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {NgIf, NgFor} from 'angular2/directives';
import {
Control,
ControlGroup,
NgForm,
formDirectives,
Validators,
NgControl,
ControlValueAccessor
} from 'angular2/forms';
export function main() {
describe("integration tests", () => {
it("should initialize DOM elements with the given form object",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var t = `
`;
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
rootTC.componentInstance.form = new ControlGroup({"login": new Control("loginValue")});
rootTC.detectChanges();
var input = rootTC.query(By.css("input"));
expect(input.nativeElement.value).toEqual("loginValue");
async.done();
});
}));
it("should update the control group values on DOM change",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var form = new ControlGroup({"login": new Control("oldValue")});
var t = `
`;
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
rootTC.componentInstance.form = form;
rootTC.detectChanges();
var input = rootTC.query(By.css("input"));
input.nativeElement.value = "updatedValue";
dispatchEvent(input.nativeElement, "change");
expect(form.value).toEqual({"login": "updatedValue"});
async.done();
});
}));
it("should emit ng-submit event on submit",
inject(
[TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
var t =
`
{{name}}
`;
var rootTC;
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { rootTC = root; });
tick();
rootTC.componentInstance.form = new ControlGroup({});
rootTC.componentInstance.name = 'old';
tick();
var form = rootTC.query(By.css("form"));
dispatchEvent(form.nativeElement, "submit");
tick();
expect(rootTC.componentInstance.name).toEqual('updated');
})));
it("should work with single controls",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var control = new Control("loginValue");
var t = ``;
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");
input.nativeElement.value = "updatedValue";
dispatchEvent(input.nativeElement, "change");
expect(control.value).toEqual("updatedValue");
async.done();
});
}));
it("should update DOM elements when rebinding the control group",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var t = `
`;
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();
var input = rootTC.query(By.css("input"));
expect(input.nativeElement.value).toEqual("newValue");
async.done();
});
}));
it("should update DOM elements when updating the value of a control",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var login = new Control("oldValue");
var form = new ControlGroup({"login": login});
var t = `
`;
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
rootTC.componentInstance.form = form;
rootTC.detectChanges();
login.updateValue("newValue");
rootTC.detectChanges();
var input = rootTC.query(By.css("input"));
expect(input.nativeElement.value).toEqual("newValue");
async.done();
});
}));
it("should mark controls as touched after interacting with the DOM control",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var login = new Control("oldValue");
var form = new ControlGroup({"login": login});
var t = `
`;
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
rootTC.componentInstance.form = form;
rootTC.detectChanges();
var loginEl = rootTC.query(By.css("input"));
expect(login.touched).toBe(false);
dispatchEvent(loginEl.nativeElement, "blur");
expect(login.touched).toBe(true);
async.done();
});
}));
describe("different control types", () => {
it("should support ",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var t = `
`;
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");
input.nativeElement.value = "new";
dispatchEvent(input.nativeElement, "input");
expect(rootTC.componentInstance.form.value).toEqual({"text": "new"});
async.done();
});
}));
it("should support without type",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var t = `
`;
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");
input.nativeElement.value = "new";
dispatchEvent(input.nativeElement, "input");
expect(rootTC.componentInstance.form.value).toEqual({"text": "new"});
async.done();
});
}));
it("should support