Skip to content

Commit b7ea9c0

Browse files
authored
Merge pull request #1778 from satanTime/issues/1596
fix: looking in vcr.createComponent on root node #1596
2 parents 8922165 + 5aa0d9a commit b7ea9c0

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

libs/ng-mocks/src/lib/mock-helper/crawl/nested-check-children.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,23 @@ export default (node: MockedDebugNode): MockedDebugNode[] => {
2626
children.push(childNode);
2727
}
2828

29+
if ((node as any).parent?.name === 'BODY') {
30+
const childNodes: any[] = (node as any).parent.childNodes;
31+
let start = childNodes.length;
32+
let end = 0;
33+
for (let i = childNodes.length - 1; i >= 0; i -= 1) {
34+
const childNode = childNodes[i];
35+
if (childNode.nativeNode.nodeName === '#comment') {
36+
end = i;
37+
} else if (childNode.nativeNode === node.nativeNode) {
38+
start = i + 1;
39+
break;
40+
}
41+
}
42+
for (let i = start; i < end; i += 1) {
43+
children.push(childNodes[i]);
44+
}
45+
}
46+
2947
return children;
3048
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"build": "npm run clean && npm run build:ncc && npm run build:fix && cp CHANGELOG.md dist/libs/ng-mocks && cp README.md dist/libs/ng-mocks && cp LICENSE dist/libs/ng-mocks && cp libs/ng-mocks/package.json dist/libs/ng-mocks/package.json && cp libs/ng-mocks/migrations.json dist/libs/ng-mocks/migrations.json && cp -R examples dist/libs/ng-mocks",
99
"build:ncc": "cd ./libs/ng-mocks && npx ncc build --no-source-map-register -m -e '@angular/animations' -e '@angular/cli' -e '@angular/common' -e '@angular/common/http' -e '@angular/core' -e '@angular/core/testing' -e '@angular/forms' -e '@angular/platform-browser' -e '@angular/platform-browser/animations' -e '@angular/platform-browser-dynamic' -e '@angular/router' ./src/index.ts -o ../../dist/libs/ng-mocks/",
1010
"build:fix": "sed -e 's/__dirname/(typeof __dirname===\"undefined\"?\"\":__dirname)/g' dist/libs/ng-mocks/index.js > dist/libs/ng-mocks/index.fix.js && mv dist/libs/ng-mocks/index.fix.js dist/libs/ng-mocks/index.js",
11+
"build:sources": "cp -R libs/ng-mocks/src dist/libs/ng-mocks && sed -e 's/\\.\\.\\/webpack:\\/\\/ng-mocks\\///g' dist/libs/ng-mocks/index.js.map > dist/libs/ng-mocks/index.fix.js.map && mv dist/libs/ng-mocks/index.fix.js.map dist/libs/ng-mocks/index.js.map",
1112
"build:all": "npm run lint && npm run build && npm run test",
1213
"build:docs": "cd docs && CI=true npm run build",
1314
"clean": "rm -rf dist test-reports tmp",

tests/issue-1596/test.spec.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {
2+
Component,
3+
ComponentFactoryResolver,
4+
NgModule,
5+
Optional,
6+
ViewContainerRef,
7+
} from '@angular/core';
8+
import { TestBed } from '@angular/core/testing';
9+
import { By } from '@angular/platform-browser';
10+
import { MockBuilder, ngMocks } from 'ng-mocks';
11+
12+
@Component({
13+
selector: 'parent',
14+
template: '<div #parent>parent</div>',
15+
})
16+
class ParentComponent {
17+
public constructor(
18+
viewContainerRef: ViewContainerRef,
19+
@Optional() componentFactoryResolver: ComponentFactoryResolver,
20+
) {
21+
const vcr: any = viewContainerRef;
22+
23+
try {
24+
vcr.createComponent(ChildComponent);
25+
} catch (e) {
26+
const factory =
27+
componentFactoryResolver.resolveComponentFactory(
28+
ChildComponent,
29+
);
30+
vcr.createComponent(factory);
31+
}
32+
}
33+
}
34+
35+
@Component({
36+
selector: 'child',
37+
template: '<span #child>child</span>',
38+
})
39+
class ChildComponent {}
40+
41+
@NgModule({
42+
declarations: [ParentComponent, ChildComponent],
43+
entryComponents: [ChildComponent],
44+
})
45+
class TargetModule {}
46+
47+
// It's a tricky thing, because it behaves like that in Ivy only.
48+
// But even in Ivy, it doesn't render the child component properly.
49+
describe('issue-1596', () => {
50+
beforeEach(() => MockBuilder(ParentComponent, TargetModule));
51+
52+
it('finds child component', () => {
53+
const fixture1 = TestBed.createComponent(ParentComponent);
54+
fixture1.detectChanges();
55+
const expectedEl1 = fixture1.debugElement.query(
56+
By.directive(ChildComponent),
57+
);
58+
const expectedInstance1 = expectedEl1
59+
? expectedEl1.componentInstance
60+
: null;
61+
62+
expect(ngMocks.findInstance(fixture1, ChildComponent, null)).toBe(
63+
expectedInstance1,
64+
);
65+
66+
const fixture2 = TestBed.createComponent(ParentComponent);
67+
fixture2.detectChanges();
68+
const expectedEl2 = fixture2.debugElement.query(
69+
By.directive(ChildComponent),
70+
);
71+
const expectedInstance2 = expectedEl2
72+
? expectedEl2.componentInstance
73+
: null;
74+
75+
expect(ngMocks.findInstance(fixture2, ChildComponent, null)).toBe(
76+
expectedInstance2,
77+
);
78+
});
79+
});

0 commit comments

Comments
 (0)