-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
mock-helper.findInstances.ts
38 lines (33 loc) · 1.12 KB
/
mock-helper.findInstances.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
38
import { Type } from '../common/core.types';
import { getSourceOfMock } from '../common/func.get-source-of-mock';
import { MockedDebugElement, MockedDebugNode } from '../mock-render/types';
import getLastFixture from './func.get-last-fixture';
function nestedCheck<T>(
result: T[],
node: MockedDebugNode & { childNodes?: MockedDebugNode[] },
callback: (node: MockedDebugNode) => undefined | T
) {
const element = callback(node);
if (element) {
result.push(element);
}
const childNodes = node?.childNodes || [];
childNodes.forEach(childNode => {
nestedCheck(result, childNode, callback);
});
}
export default <T>(...args: any[]): T[] => {
const el: undefined | MockedDebugElement =
typeof args[0] !== 'object' ? undefined : args[0].debugElement ? args[0].debugElement : args[0];
const sel: Type<any> = el ? args[1] : args[0];
const debugElement = el || getLastFixture()?.debugElement;
const result: T[] = [];
nestedCheck<T>(result, debugElement, node => {
try {
return node.injector.get(getSourceOfMock(sel));
} catch (error) {
return undefined;
}
});
return result;
};