1 | import { NgZone } from '@angular/core';
|
2 | var lorem_ipsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi maximus nisl lobortis interdum condimentum. Cras volutpat, massa quis vehicula eleifend, turpis mauris sodales erat, ut varius ligula ipsum et turpis. Aliquam erat volutpat. Maecenas sodales pellentesque auctor. Suspendisse faucibus a erat sit amet pretium. Vestibulum nec tempus tellus. Mauris fringilla faucibus dui sed vestibulum. Curabitur porttitor consectetur nisl. Nulla porta, neque sed congue tempus, erat nunc rutrum diam, eu elementum sapien leo quis eros. Donec non convallis felis. Nam eu pharetra sapien.';
|
3 | export var TEXT_CORPUS = [
|
4 | ['hola', 'hola'],
|
5 | ['', ''],
|
6 | [' ', ' '],
|
7 | ['adiós', 'adiós'],
|
8 | ['hola y adiós', 'hola y adiós'],
|
9 | [lorem_ipsum, lorem_ipsum]
|
10 | ];
|
11 | export var NUMBER_CORPUS = [
|
12 | [-1, -1],
|
13 | [0, 0],
|
14 | [-123456789, -123456789],
|
15 | [1.1234, 1.1234],
|
16 | [123456789, 123456789],
|
17 | ['1.1234', 1.1234],
|
18 | ['123456789', 123456789],
|
19 | ['-123456789', -123456789]
|
20 | ];
|
21 | export var BOOLEAN_CORPUS = [
|
22 | [true, true],
|
23 | [false, false],
|
24 | ['', true],
|
25 | ['false', false],
|
26 | ['true', true],
|
27 | ];
|
28 | export var ANY_CORPUS = [
|
29 | [true, true],
|
30 | [false, false],
|
31 | [0, 0],
|
32 | ['', ''],
|
33 | [' ', ' '],
|
34 | ['hola', 'hola']
|
35 | ];
|
36 | export function commonInputTest(input, config) {
|
37 |
|
38 |
|
39 |
|
40 | var zone = new NgZone({ enableLongStackTrace: true });
|
41 | zone.run(function () {
|
42 | if (config.testItem === true && !input._item) {
|
43 | (void 0) ;
|
44 | }
|
45 | if (config.testForm === true && !input._form) {
|
46 | (void 0) ;
|
47 | }
|
48 |
|
49 | testInput(input, config, false);
|
50 | input.ngAfterContentInit();
|
51 | input.ngAfterViewInit && input.ngAfterViewInit();
|
52 |
|
53 | testInput(input, config, true);
|
54 |
|
55 | if (config.testItem === true && !input._item) {
|
56 | input._item = undefined;
|
57 | testInput(input, config, true);
|
58 | }
|
59 |
|
60 | if (config.testForm === true && !input._form) {
|
61 | input._form = undefined;
|
62 | testInput(input, config, true);
|
63 | }
|
64 | testInput(input, config, true);
|
65 | input.ngOnDestroy();
|
66 | (void 0) ;
|
67 | });
|
68 | }
|
69 | function testInput(input, config, isInit) {
|
70 | testState(input, config, isInit);
|
71 | testWriteValue(input, config, isInit);
|
72 | testNgModelChange(input, config, isInit);
|
73 | }
|
74 | function testState(input, config, isInit) {
|
75 | assertEqual(input._init, isInit, 'input must be init');
|
76 | assertEqual(input._isFocus, false, 'should not be focus');
|
77 | assertEqual(input.isFocus(), false, 'should not be focus');
|
78 | assertEqual(input.value, config.defaultValue, 'default value is wrong');
|
79 | if (isInit) {
|
80 | var blurCount_1 = 0;
|
81 | var focusCount_1 = 0;
|
82 | var onTouchedCalled_1 = 0;
|
83 | var subBlur = input.ionBlur.subscribe(function (ev) {
|
84 | assertEqual(ev, input, 'ionBlur argument is wrong');
|
85 | blurCount_1++;
|
86 | if (config.onFocusChange && config.onFocusChange(false) !== true) {
|
87 | (void 0) ;
|
88 | }
|
89 | });
|
90 | var subFocus = input.ionFocus.subscribe(function (ev) {
|
91 | assertEqual(ev, input, 'ionFocus argument is wrong');
|
92 | focusCount_1++;
|
93 | if (config.onFocusChange && config.onFocusChange(true) !== true) {
|
94 | (void 0) ;
|
95 | }
|
96 | });
|
97 | input.registerOnTouched(function () {
|
98 | assertEqual(onTouchedCalled_1, 0, 'registerOnTouched: internal error');
|
99 | onTouchedCalled_1++;
|
100 | });
|
101 | input._fireBlur();
|
102 | assertEqual(blurCount_1, 0, 'blur should not have been emitted');
|
103 | assertEqual(onTouchedCalled_1, 0, 'touched should not have been called');
|
104 | input._fireFocus();
|
105 | assertEqual(input._isFocus, true, 'should be focus');
|
106 | assertEqual(input.isFocus(), true, 'should be focus');
|
107 | input._fireFocus();
|
108 | input._fireBlur();
|
109 | assertEqual(input._isFocus, false, 'should be not focus');
|
110 | assertEqual(input.isFocus(), false, 'should be not focus');
|
111 | assertEqual(onTouchedCalled_1, 1, 'touched should have been called');
|
112 | input._fireBlur();
|
113 | assertEqual(focusCount_1, 1, 'ionFocus was not called correctly');
|
114 | assertEqual(blurCount_1, 1, 'ionBlur was not called correctly');
|
115 | subBlur.unsubscribe();
|
116 | subFocus.unsubscribe();
|
117 | }
|
118 | }
|
119 | function testWriteValue(input, config, isInit) {
|
120 | var test;
|
121 | var i;
|
122 | var ionChangeCalled = 0;
|
123 | var OnChangeCalled = 0;
|
124 | var OnTouchedCalled = 0;
|
125 | var ngModelValue;
|
126 |
|
127 | var sub = input.ionChange.subscribe(function (ev) {
|
128 | assertEqual(ionChangeCalled, 0, 'ionChange: internal error');
|
129 | assertEqual(ev, input, 'ionChange: ev is not the input');
|
130 | assertEqual(ev.value, test[1], 'ionChange: value does not match');
|
131 | assertEqual(ngModelValue, test[1], 'ionChange: ngmodel was not updated');
|
132 | ionChangeCalled++;
|
133 | });
|
134 |
|
135 | input.registerOnChange(function (ev) {
|
136 | assertEqual(OnChangeCalled, 0, 'registerOnChange: internal error');
|
137 | assertEqual(input.value, ev, 'registerOnChange: ev output does not match');
|
138 | assertEqual(input.value, test[1], 'registerOnChange: value does not match');
|
139 | ngModelValue = ev;
|
140 | OnChangeCalled++;
|
141 | });
|
142 |
|
143 | input.registerOnTouched(function () {
|
144 | assertEqual(OnTouchedCalled, 0, 'registerOnTouched: internal error');
|
145 | OnTouchedCalled++;
|
146 | });
|
147 |
|
148 | for (i = 0; i < config.corpus.length; i++) {
|
149 | test = config.corpus[i];
|
150 | input.value = test[0];
|
151 | assertEqual(input.value, test[1], 'loop: input/output does not match');
|
152 | if (isInit) {
|
153 | assertEqual(ionChangeCalled, 1, 'loop: ionChange error');
|
154 | if (config.onValueChange && config.onValueChange(test[1]) !== true) {
|
155 | (void 0) ;
|
156 | }
|
157 | }
|
158 | else {
|
159 | assertEqual(ionChangeCalled, 0, 'loop: ionChange error');
|
160 | }
|
161 | assertEqual(OnChangeCalled, 1, 'loop: OnChangeCalled was not called');
|
162 | assertEqual(OnTouchedCalled, 0, 'loop: OnTouchedCalled was called');
|
163 | OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;
|
164 |
|
165 | input.value = test[0];
|
166 | assertEqual(ionChangeCalled, 0, 'loop: ionChange should not be called');
|
167 | assertEqual(OnChangeCalled, 0, 'loop: OnChangeCalled should not be called');
|
168 |
|
169 | OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;
|
170 | }
|
171 |
|
172 | input.value = undefined;
|
173 | assertEqual(input.value, test[1], 'undefined should not change the value');
|
174 | assertEqual(ionChangeCalled, 0, 'undefined: ionChange should not be called');
|
175 | assertEqual(OnChangeCalled, 0, 'undefined: OnChangeCalled should not be called');
|
176 | assertEqual(OnTouchedCalled, 0, 'undefined: OnTouchedCalled should not be called');
|
177 |
|
178 | test = [null, config.defaultValue];
|
179 | input.value = null;
|
180 | assertEqual(input.value, config.defaultValue, 'null: wrong default value');
|
181 | assertEqual(OnChangeCalled, 1, 'null: OnChangeCalled was not called');
|
182 | assertEqual(OnTouchedCalled, 0, 'null: OnTouchedCalled was called');
|
183 | input.registerOnChange(null);
|
184 | input.registerOnTouched(null);
|
185 | sub.unsubscribe();
|
186 | }
|
187 | function testNgModelChange(input, config, isInit) {
|
188 | var test;
|
189 | var i;
|
190 | var ionChangeCalled = 0;
|
191 | var OnChangeCalled = 0;
|
192 | var OnTouchedCalled = 0;
|
193 |
|
194 | var sub = input.ionChange.subscribe(function (ev) {
|
195 | assertEqual(ionChangeCalled, 0, 'internal error');
|
196 | assertEqual(ev, input, 'ev output does not match');
|
197 | assertEqual(test[1], ev.value, 'value does not match');
|
198 | ionChangeCalled++;
|
199 | });
|
200 |
|
201 | input.registerOnChange(function () {
|
202 | OnChangeCalled++;
|
203 | });
|
204 |
|
205 | input.registerOnTouched(function () {
|
206 | OnTouchedCalled++;
|
207 | });
|
208 |
|
209 | for (i = 0; i < config.corpus.length; i++) {
|
210 | test = config.corpus[i];
|
211 | input.writeValue(test[0]);
|
212 | assertEqual(input.value, test[1], 'input/output does not match');
|
213 | if (isInit) {
|
214 | assertEqual(ionChangeCalled, 1, 'ionChange error');
|
215 | if (config.onValueChange && config.onValueChange(test[1]) !== true) {
|
216 | (void 0) ;
|
217 | }
|
218 | }
|
219 | else {
|
220 | assertEqual(ionChangeCalled, 0, 'ionChange error');
|
221 | }
|
222 | assertEqual(OnChangeCalled, 0, 'OnChangeCalled should not be called');
|
223 | assertEqual(OnTouchedCalled, 0, 'OnTouchedCalled should not be called');
|
224 | OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;
|
225 |
|
226 | input.writeValue(test[0]);
|
227 | input.value = test[0];
|
228 | assertEqual(ionChangeCalled, 0, 'ionChange should not be called');
|
229 | assertEqual(OnChangeCalled, 0, 'OnChangeCalled should not be called');
|
230 |
|
231 | OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;
|
232 | }
|
233 | input.registerOnChange(null);
|
234 | input.registerOnTouched(null);
|
235 | sub.unsubscribe();
|
236 | input.value = config.defaultValue;
|
237 | }
|
238 | function assertEqual(a, b, message) {
|
239 | if (!equal(a, b)) {
|
240 | (void 0) ;
|
241 | }
|
242 | }
|
243 | function equal(a, b) {
|
244 | if (a === b) {
|
245 | return true;
|
246 | }
|
247 |
|
248 | return JSON.stringify(a) === JSON.stringify(b);
|
249 | }
|
250 |
|
\ | No newline at end of file |