-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
mock-helper.change.ts
37 lines (29 loc) · 994 Bytes
/
mock-helper.change.ts
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
import { DebugElement } from '@angular/core';
import { isMockControlValueAccessor } from '../../common/func.is-mock-control-value-accessor';
import funcGetVca from './func.get-vca';
// default html behavior
const triggerInput = (el: DebugElement, value: any): void => {
el.triggerEventHandler('focus', {});
el.nativeElement.value = value;
el.triggerEventHandler('input', {
target: {
value,
},
});
el.triggerEventHandler('blur', {});
};
export default (el: DebugElement, value: any): void => {
const valueAccessor = funcGetVca(el);
if (isMockControlValueAccessor(valueAccessor.instance)) {
valueAccessor.instance.__simulateChange(value);
return;
}
triggerInput(el, value);
for (const key of ['onChange', '_onChange', 'changeFn', '_onChangeCallback', 'onModelChange']) {
if (typeof valueAccessor[key] === 'function') {
valueAccessor[key](value);
return;
}
}
throw new Error('Unsupported type of ControlValueAccessor');
};