Repository navigation
Expand file tree
/
Copy pathintegration_spec.js
More file actions
180 lines (145 loc) · 5.38 KB
/
Copy pathintegration_spec.js
File metadata and controls
180 lines (145 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach,
el, queryView, dispatchEvent} from 'angular2/test_lib';
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';
import {NativeShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy';
import {Injector} from 'angular2/di';
import {DOM} from 'angular2/src/facade/dom';
import {Component, TemplateConfig} from 'angular2/core';
import {ControlDirective, ControlNameDirective, ControlGroupDirective, NewControlGroupDirective,
Control, ControlGroup} from 'angular2/forms';
import {TemplateLoader} from 'angular2/src/core/compiler/template_loader';
import {XHRMock} from 'angular2/src/mock/xhr_mock';
export function main() {
function detectChanges(view) {
view.changeDetector.detectChanges();
}
function compile(componentType, template, context, callback) {
var compiler = new Compiler(dynamicChangeDetection,
new TemplateLoader(new XHRMock()),
new DirectiveMetadataReader(),
new Parser(new Lexer()),
new CompilerCache(),
new NativeShadowDomStrategy());
compiler.compile(componentType, el(template)).then((pv) => {
var view = pv.instantiate(null);
view.hydrate(new Injector([]), null, context);
detectChanges(view);
callback(view);
});
}
function formComponent(view) {
// TODO: vsavkin remove when view variables work
return view.elementInjectors[0].getComponent();
}
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();
});
});
describe("declarative forms", () => {
it("should initialize dom elements", (done) => {
var t = `<div [new-control-group]="{'login': 'loginValue', 'password':'passValue'}">
<input id="login" [control]="'login'">
<input id="password" [control]="'password'">
</div>`;
compile(MyComp, t, new MyComp(), (view) => {
var loginInput = queryView(view, "#login")
expect(loginInput.value).toEqual("loginValue");
var passInput = queryView(view, "#password")
expect(passInput.value).toEqual("passValue");
done();
});
});
it("should update the control group values on DOM change", (done) => {
var t = `<div [new-control-group]="{'login': 'loginValue'}">
<input [control]="'login'">
</div>`;
compile(MyComp, t, new MyComp(), (view) => {
var input = queryView(view, "input")
input.value = "updatedValue";
dispatchEvent(input, "change");
expect(formComponent(view).value).toEqual({'login': 'updatedValue'});
done();
});
});
});
});
}
@Component({
selector: "my-comp",
template: new TemplateConfig({
inline: "",
directives: [ControlGroupDirective, ControlNameDirective,
ControlDirective, NewControlGroupDirective]
})
})
class MyComp {
form:ControlGroup;
name:string;
constructor(form = null, name = null) {
this.form = form;
this.name = name;
}
}