This repository was archived by the owner on Sep 16, 2022. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathng_form_model_test.dart
207 lines (180 loc) · 5.78 KB
/
ng_form_model_test.dart
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import'package:test/test.dart';
import'package:angular/angular.dart';
import'package:angular_forms/angular_forms.dart';
import'package:angular_test/angular_test.dart';
import'ng_form_model_test.template.dart'as ng;
MatcherthrowsWith(String s) =>
throwsA(predicate((e) => e.toString().contains(s)));
voidmain() {
ng.initReflector();
group('NgFormModel', () {
lateNgTestFixture<NgFormModelTest> fixture;
tearDown(() =>disposeAnyRunningTest());
setUp(() async {
var testBed =NgTestBed(ng.createNgFormModelTestFactory());
fixture =await testBed.create();
});
test('should reexport control properties', () {
fixture.update((cmp) {
final form = cmp.form!;
final formModel = cmp.formModel;
expect(form.control, formModel);
expect(form.value, formModel.value);
expect(form.valid, formModel.valid);
expect(form.errors, formModel.errors);
expect(form.pristine, formModel.pristine);
expect(form.dirty, formModel.dirty);
expect(form.touched, formModel.touched);
expect(form.untouched, formModel.untouched);
});
});
group('addControl', () {
test('should throw when no value accessor', () async {
await fixture.update((cmp) {
var dir =NgControlName(cmp.form!, null, null);
dir.name ='login';
expect(() => cmp.form!.addControl(dir),
throwsWith('No value accessor for (login)'));
});
});
test('should set up validators', () async {
await fixture.update((cmp) {
// sync validators are set
expect(cmp.formModel.hasError('required', ['login']), true);
(cmp.formModel.findPath(['login']) asControl)
.updateValue('invalid value');
});
});
test('should write value to the DOM', () async {
await fixture.update((cmp) {
(cmp.formModel.findPath(['login']) asControl)
.updateValue('initValue');
expect(
(cmp.loginControlDir!.valueAccessor asDummyControlValueAccessor)
.writtenValue,
'initValue');
});
});
test(
'should add the directive to the list of directives '
'included in the form', () async {
await fixture.update((cmp) {
expect(cmp.form!.directives, [cmp.loginControlDir]);
});
});
});
group('addControlGroup', () {
test('should set up validator', () async {
await fixture.update((cmp) {
(cmp.formModel.findPath(['passwords', 'password']) asControl)
.updateValue('somePassword');
(cmp.formModel.findPath(['passwords', 'passwordConfirm']) asControl)
.updateValue('someOtherPassword');
});
await fixture.update((cmp) {
// sync validators are set
expect(cmp.formModel.hasError('differentPasswords', ['passwords']),
true);
(cmp.formModel.findPath(['passwords', 'passwordConfirm']) asControl)
.updateValue('somePassword');
expect(cmp.formModel.hasError('differentPasswords', ['passwords']),
false);
});
});
});
group('removeControl', () {
test(
'should remove the directive to the list of directives included in '
'the form', () async {
await fixture.update((cmp) {
cmp.needsLogin =false;
});
await fixture.update((cmp) {
expect(cmp.form!.directives, []);
});
});
});
group('ngAfterChanges', () {
test('should update dom values of all the directives', () async {
await fixture.update((cmp) {
(cmp.formModel.findPath(['login']) asControl)
.updateValue('new value');
});
await fixture.update((cmp) {
expect(
(cmp.loginControlDir!.valueAccessor asDummyControlValueAccessor)
.writtenValue,
'new value');
});
});
});
});
}
@Component(
selector:'ng-form-model-test',
directives: [
formDirectives,
DummyControlValueAccessor,
MatchingPasswordsValidator,
NgIf,
],
template:'''
<div [ngFormModel]="formModel" #form="ngForm">
<div *ngIf="needsLogin">
<input [ngControl]="'login'" #login="ngForm" required dummy />
</div>
<div [ngControlGroup]="'passwords'" #passwords="ngForm" matchingPasswords>
</div>
</div>
''',
)
classNgFormModelTest {
@ViewChild('form')
NgFormModel? form;
@ViewChild('login')
NgControlName? loginControlDir;
@ViewChild('passwords')
NgControlGroup? passwords;
bool needsLogin =true;
var formModel =ControlGroup({
'login':Control(),
'passwords':
ControlGroup({'password':Control(), 'passwordConfirm':Control()})
});
}
@Directive(selector:'[dummy]', providers: [
ExistingProvider.forToken(
ngValueAccessor,
DummyControlValueAccessor,
)
])
classDummyControlValueAccessorimplementsControlValueAccessor<dynamic> {
dynamic writtenValue;
@override
voidwriteValue(dynamic obj) {
writtenValue = obj;
}
@override
voidregisterOnChange(fn) {}
@override
voidregisterOnTouched(fn) {}
@override
voidonDisabledChanged(bool isDisabled) {}
}
@Directive(selector:'[matchingPasswords]', providers: [
ValueProvider.forToken(
NG_VALIDATORS, MatchingPasswordsValidator.matchingPasswordsValidator),
])
classMatchingPasswordsValidator {
staticMap<String, dynamic>?matchingPasswordsValidator(
AbstractControl control) {
if (control is!ControlGroup) throwStateError('Must be ControlGroup');
var group = control;
if (group.controls['password']!.value !=
group.controls['passwordConfirm']!.value) {
return {'differentPasswords':true};
} else {
returnnull;
}
}
}