Skip to content

Commit

Permalink
fix: correct stop of search in ivy tree
Browse files Browse the repository at this point in the history
closes #298
  • Loading branch information
satanTime committed Feb 13, 2021
1 parent 397ecf8 commit 952986e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DebugNode } from '@angular/core';

export default (node: DebugNode): DebugNode =>
node.nativeNode.nodeName === '#text' && node.parent ? node.parent : node;
node.nativeNode?.nodeName === '#text' && node.parent ? node.parent : node;
36 changes: 36 additions & 0 deletions libs/ng-mocks/src/lib/mock-helper/func.get-from-node-ivy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ describe('func.get-from-node-ivy', () => {
expect(result).toEqual([proto]);
});

it('disables on arrays', () => {
const result: any[] = [];
const proto = new Proto();
const node: any = {
nativeNode: {},
parent: {
nativeNode: {
__ngContext__: {
lView: [
[] /* now proto should not be collected */,
proto,
],
},
},
},
};

funcGetFromNodeIvy(result, node, Proto);

expect(result).toEqual([]);
});

it('handles empty context', () => {
const result: any[] = [];
const node: any = {
Expand Down Expand Up @@ -69,6 +91,20 @@ describe('func.get-from-node-ivy', () => {
expect(result).toEqual([proto]);
});

it('handles missed nativeNode', () => {
const result: any[] = [];
const node: any = {
parent: {
nativeNode: {
__ngContext__: [],
},
},
};

funcGetFromNodeIvy(result, node, Proto);
expect(result).toEqual([]);
});

it('skips node with _debugContext', () => {
const result: any[] = [];
const proto = new Proto();
Expand Down
3 changes: 1 addition & 2 deletions libs/ng-mocks/src/lib/mock-helper/func.get-from-node-ivy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import funcGetFromNodeScan from './func.get-from-node-scan';

const detectContext = (node: DebugNode): any => {
let current = node;
let context = current.nativeNode.__ngContext__;
let context = current.nativeNode?.__ngContext__;
while (!context && current.parent) {
current = current.parent;
context = current.nativeNode.__ngContext__;
Expand All @@ -25,7 +25,6 @@ export default <T>(result: T[], node: (DebugNode & Node) | null | undefined, pro
}

const el = funcGetFromNodeElement(node);

funcGetFromNodeScan(
{
el,
Expand Down
8 changes: 6 additions & 2 deletions libs/ng-mocks/src/lib/mock-helper/func.get-from-node-scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { DebugNode } from '@angular/core';
import { Type } from '../common/core.types';

const detectGatherFlag = (gather: boolean, el: DebugNode | null, node: any): boolean => {
// LContainer should stop the scan.
if (Array.isArray(node)) {
return false;
}

if (!el || !node.nodeName) {
return gather;
}
Expand All @@ -12,8 +17,7 @@ const detectGatherFlag = (gather: boolean, el: DebugNode | null, node: any): boo
return node.parentNode === el.nativeNode;
}

// checking if an injectedNode belongs to the current element.
return node === el.nativeNode;
return false;
};

const isNotObject = (node: any): boolean => !node || typeof node !== 'object';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const getMeta = (token: any): Directive | undefined => {
};

export default (el: DebugNode | null | undefined, token: any): Directive | undefined => {
// istanbul ignore if
if (!el) {
return undefined;
}
Expand Down
18 changes: 13 additions & 5 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ const parseArgs = (args: any[]): [MockedDebugElement | null | undefined, string,
args.length === 3 ? args[2] : defaultNotFoundValue,
];

const attrMatches = (attribute: string, selector: string): string | undefined => {
const [prop, alias = ''] = attribute.split(':', 2).map(v => v.trim());

if ((!alias && prop === selector) || (!!alias && alias === selector)) {
return prop;
}

return undefined;
};

const detectAttribute = (el: MockedDebugElement | null | undefined, attr: 'inputs' | 'outputs', sel: string) => {
for (const token of el?.providerTokens || []) {
const meta = funcParseProviderTokensDirectives(el, token);
Expand All @@ -19,12 +29,10 @@ const detectAttribute = (el: MockedDebugElement | null | undefined, attr: 'input
}

for (const attrDef of meta[attr] || /* istanbul ignore next */ []) {
const [prop, alias = ''] = attrDef.split(':', 2).map(v => v.trim());
if ((!alias && prop !== sel) || (alias && alias !== sel)) {
continue;
const prop = attrMatches(attrDef, sel);
if (prop) {
return mockHelperGet(el, token)[prop];
}

return mockHelperGet(el, token)[prop];
}
}

Expand Down

0 comments on commit 952986e

Please sign in to comment.