import {Component, Directive, View} from 'angular2/angular2';
import {
afterEach,
AsyncTestCompleter,
beforeEach,
ddescribe,
describe,
dispatchEvent,
fakeAsync,
flushMicrotasks,
tick,
el,
expect,
it,
inject,
iit,
xit
} from 'angular2/test_lib';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {TestBed} from 'angular2/src/test_lib/test_bed';
import {NgIf} from 'angular2/directives';
import {
Control,
ControlGroup,
RequiredValidatorDirective,
TemplateDrivenFormDirective,
formDirectives,
Validators,
ControlDirective,
ControlValueAccessor
} 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")})});
var t = `
`;
tb.createView(MyComp, {context: ctx, html: t})
.then((view) => {
view.detectChanges();
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});
var t = `
`;
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});
var t = ``;
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});
var t = `
`;
tb.createView(MyComp, {context: ctx, html: t})
.then((view) => {
view.detectChanges();
ctx.form = new ControlGroup({"login": new Control("newValue")});
view.detectChanges();
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});
var t = `
`;
tb.createView(MyComp, {context: ctx, html: t})
.then((view) => {
view.detectChanges();
login.updateValue("newValue");
view.detectChanges();
var input = view.querySelector("input");
expect(input.value).toEqual("newValue");
async.done();
});
}));
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 = `
`;
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();
});
}));
describe("different control types", () => {
it("should support ", inject([TestBed, AsyncTestCompleter], (tb, async) => {
var ctx = MyComp.create({form: new ControlGroup({"text": new Control("old")})});
var t = `
`;
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 without type",
inject([TestBed, AsyncTestCompleter], (tb, async) => {
var ctx = MyComp.create({form: new ControlGroup({"text": new Control("old")})});
var t = `