2015-02-03 10:27:09 -05:00
|
|
|
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach,
|
2015-02-05 17:09:26 -05:00
|
|
|
el, queryView, dispatchEvent} from 'angular2/test_lib';
|
2015-02-03 10:27:09 -05:00
|
|
|
|
2015-02-05 17:09:26 -05:00
|
|
|
import {Lexer, Parser, ChangeDetector, dynamicChangeDetection} from 'angular2/change_detection';
|
|
|
|
import {Compiler, CompilerCache} from 'angular2/src/core/compiler/compiler';
|
|
|
|
import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader';
|
2015-01-30 03:43:21 -05:00
|
|
|
import {NativeShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy';
|
2015-02-05 17:09:26 -05:00
|
|
|
import {Injector} from 'angular2/di';
|
|
|
|
import {DOM} from 'angular2/src/facade/dom';
|
2015-02-03 10:27:09 -05:00
|
|
|
|
2015-02-05 17:09:26 -05:00
|
|
|
import {Component, TemplateConfig} from 'angular2/core';
|
|
|
|
import {ControlDecorator, ControlGroupDecorator, Control, ControlGroup} from 'angular2/forms';
|
2015-02-03 10:27:09 -05:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
function detectChanges(view) {
|
|
|
|
view.changeDetector.detectChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
function compile(componentType, template, context, callback) {
|
|
|
|
var compiler = new Compiler(dynamicChangeDetection, null, new DirectiveMetadataReader(),
|
2015-01-30 03:43:21 -05:00
|
|
|
new Parser(new Lexer()), new CompilerCache(), new NativeShadowDomStrategy());
|
2015-02-03 10:27:09 -05:00
|
|
|
|
|
|
|
compiler.compile(componentType, el(template)).then((pv) => {
|
|
|
|
var view = pv.instantiate(null);
|
|
|
|
view.hydrate(new Injector([]), null, context);
|
|
|
|
detectChanges(view);
|
|
|
|
callback(view);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("integration tests", () => {
|
|
|
|
it("should initialize DOM elements with the given form object", (done) => {
|
|
|
|
var ctx = new MyComp(new ControlGroup({
|
|
|
|
"login": new Control("loginValue")
|
|
|
|
}));
|
|
|
|
|
|
|
|
var t = `<div [control-group]="form">
|
|
|
|
<input [control-name]="'login'">
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
compile(MyComp, t, ctx, (view) => {
|
|
|
|
var input = queryView(view, "input")
|
|
|
|
expect(input.value).toEqual("loginValue");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the control group values on DOM change", (done) => {
|
|
|
|
var form = new ControlGroup({
|
|
|
|
"login": new Control("oldValue")
|
|
|
|
});
|
|
|
|
var ctx = new MyComp(form);
|
|
|
|
|
|
|
|
var t = `<div [control-group]="form">
|
|
|
|
<input [control-name]="'login'">
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
compile(MyComp, t, ctx, (view) => {
|
|
|
|
var input = queryView(view, "input")
|
|
|
|
|
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "change");
|
|
|
|
|
|
|
|
expect(form.value).toEqual({"login": "updatedValue"});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update DOM elements when rebinding the control group", (done) => {
|
|
|
|
var form = new ControlGroup({
|
|
|
|
"login": new Control("oldValue")
|
|
|
|
});
|
|
|
|
var ctx = new MyComp(form);
|
|
|
|
|
|
|
|
var t = `<div [control-group]="form">
|
|
|
|
<input [control-name]="'login'">
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
compile(MyComp, t, ctx, (view) => {
|
|
|
|
ctx.form = new ControlGroup({
|
|
|
|
"login": new Control("newValue")
|
|
|
|
});
|
|
|
|
detectChanges(view);
|
|
|
|
|
|
|
|
var input = queryView(view, "input")
|
|
|
|
expect(input.value).toEqual("newValue");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update DOM element when rebinding the control name", (done) => {
|
|
|
|
var ctx = new MyComp(new ControlGroup({
|
|
|
|
"one": new Control("one"),
|
|
|
|
"two": new Control("two")
|
|
|
|
}), "one");
|
|
|
|
|
|
|
|
var t = `<div [control-group]="form">
|
|
|
|
<input [control-name]="name">
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
compile(MyComp, t, ctx, (view) => {
|
|
|
|
var input = queryView(view, "input")
|
|
|
|
expect(input.value).toEqual("one");
|
|
|
|
|
|
|
|
ctx.name = "two";
|
|
|
|
detectChanges(view);
|
|
|
|
|
|
|
|
expect(input.value).toEqual("two");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: new TemplateConfig({
|
|
|
|
directives: [ControlGroupDecorator, ControlDecorator]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
class MyComp {
|
|
|
|
form:ControlGroup;
|
|
|
|
name:string;
|
|
|
|
|
|
|
|
constructor(form, name = null) {
|
|
|
|
this.form = form;
|
|
|
|
this.name = name;
|
|
|
|
}
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|