-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: respect of virtual dom with ngMocks.crawl
closes #289
- Loading branch information
Showing
25 changed files
with
775 additions
and
214 deletions.
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
libs/ng-mocks/src/lib/mock-helper/crawl/crawl-by-declaration.spec.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default (a: any, b: any): boolean => { | ||
if (!a || !b) { | ||
return false; | ||
} | ||
|
||
return a === b; | ||
}; |
10 changes: 10 additions & 0 deletions
10
libs/ng-mocks/src/lib/mock-helper/crawl/el-def-get-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import detectTextNode from './detect-text-node'; | ||
|
||
export default (node: any) => { | ||
return detectTextNode(node) | ||
? undefined | ||
: (undefined as any) || | ||
node.injector._tNode || // ivy | ||
node.injector.elDef || // classic | ||
undefined; | ||
}; |
51 changes: 51 additions & 0 deletions
51
libs/ng-mocks/src/lib/mock-helper/crawl/el-def-get-parent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { DebugElement, ViewContainerRef } from '@angular/core'; | ||
|
||
import elDefGetNode from './el-def-get-node'; | ||
|
||
const getVcr = (node: any, child: any): undefined | ViewContainerRef => { | ||
if (node === child) { | ||
return undefined; | ||
} | ||
if (child.nativeNode.nodeName !== '#comment') { | ||
return undefined; | ||
} | ||
|
||
return child.injector.get(ViewContainerRef, null) || undefined; | ||
}; | ||
|
||
const scanViewRef = (node: DebugElement) => { | ||
let result: any; | ||
let index: any; | ||
|
||
for (const child of node.parent?.childNodes || []) { | ||
const vcr = getVcr(node, child); | ||
if (!vcr) { | ||
continue; | ||
} | ||
|
||
for (let vrIndex = 0; vrIndex < vcr.length; vrIndex += 1) { | ||
const vr = vcr.get(vrIndex); | ||
for (let rnIndex = 0; rnIndex < (vr as any).rootNodes.length; rnIndex += 1) { | ||
const rootNode = (vr as any).rootNodes[rnIndex]; | ||
if (rootNode === node.nativeNode && (index === undefined || rnIndex < index)) { | ||
result = elDefGetNode(child); | ||
index = rnIndex; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
export default (node: any) => { | ||
return ( | ||
(undefined as any) || | ||
node.injector._tNode?.parent || // ivy | ||
node.injector.elDef?.parent || // classic | ||
scanViewRef(node) || | ||
node.parent?.injector._tNode || // ivy | ||
node.parent?.injector.elDef || // classic | ||
undefined | ||
); | ||
}; |
36 changes: 7 additions & 29 deletions
36
libs/ng-mocks/src/lib/mock-helper/crawl/mock-helper.crawl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,11 @@ | ||
import funcParseFindArgs from '../func.parse-find-args'; | ||
import funcParseFindArgsName from '../func.parse-find-args-name'; | ||
import { DebugNode } from '@angular/core'; | ||
|
||
import detectCrawler from './detect-crawler'; | ||
import detectTextNode from './detect-text-node'; | ||
import nestedCheck from './nested-check'; | ||
|
||
const defaultNotFoundValue = {}; // simulating Symbol | ||
|
||
export default (...args: any[]): any => { | ||
const { el, sel, notFoundValue } = funcParseFindArgs(args, defaultNotFoundValue); | ||
|
||
const detector = detectCrawler(sel); | ||
|
||
let result; | ||
nestedCheck(el, node => { | ||
if (!detectTextNode(node) && detector(node)) { | ||
result = node; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
if (result) { | ||
return result; | ||
} | ||
if (notFoundValue !== defaultNotFoundValue) { | ||
return notFoundValue; | ||
} | ||
|
||
throw new Error(`Cannot find a DebugElement via ngMocks.reveal(${funcParseFindArgsName(sel)})`); | ||
export default ( | ||
el: DebugNode | undefined, | ||
callback: (node: DebugNode) => void | boolean, | ||
includeTextNode = false, | ||
): void => { | ||
nestedCheck(el, undefined, callback, includeTextNode); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
libs/ng-mocks/src/lib/mock-helper/crawl/mock-helper.reveal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import funcParseFindArgs from '../func.parse-find-args'; | ||
import funcParseFindArgsName from '../func.parse-find-args-name'; | ||
|
||
import detectCrawler from './detect-crawler'; | ||
import detectTextNode from './detect-text-node'; | ||
import mockHelperCrawl from './mock-helper.crawl'; | ||
|
||
const defaultNotFoundValue = {}; // simulating Symbol | ||
|
||
export default (...args: any[]): any => { | ||
const { el, sel, notFoundValue } = funcParseFindArgs(args, defaultNotFoundValue); | ||
|
||
const detector = detectCrawler(sel); | ||
|
||
let result; | ||
mockHelperCrawl(el, node => { | ||
if (node !== el && !detectTextNode(node) && detector(node)) { | ||
result = node; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
if (result) { | ||
return result; | ||
} | ||
if (notFoundValue !== defaultNotFoundValue) { | ||
return notFoundValue; | ||
} | ||
|
||
throw new Error(`Cannot find a DebugElement via ngMocks.reveal(${funcParseFindArgsName(sel)})`); | ||
}; |
30 changes: 30 additions & 0 deletions
30
libs/ng-mocks/src/lib/mock-helper/crawl/nested-check-children.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { MockedDebugNode } from '../../mock-render/types'; | ||
|
||
import detectTextNode from './detect-text-node'; | ||
import elDefCompare from './el-def-compare'; | ||
import elDefGetNode from './el-def-get-node'; | ||
import elDefGetParent from './el-def-get-parent'; | ||
|
||
export default (node: MockedDebugNode): MockedDebugNode[] => { | ||
const elDef = elDefGetNode(node); | ||
if (!elDef || detectTextNode(node)) { | ||
return []; | ||
} | ||
|
||
const isDirect = (node as any).childNodes !== undefined; | ||
const children: MockedDebugNode[] = []; | ||
for (const childNode of (node as any).childNodes || node.parent?.childNodes || []) { | ||
const childNodeParent = elDefGetParent(childNode); | ||
|
||
if (!isDirect && !elDefCompare(elDef, childNodeParent)) { | ||
continue; | ||
} | ||
if (childNodeParent && !elDefCompare(elDef, childNodeParent)) { | ||
continue; | ||
} | ||
|
||
children.push(childNode); | ||
} | ||
|
||
return children; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 18 additions & 2 deletions
20
libs/ng-mocks/src/lib/mock-helper/mock-helper.find-instance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.