-
-
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.
fix(#333): register mock components with entryComponents
- Loading branch information
Showing
4 changed files
with
172 additions
and
11 deletions.
There are no files selected for viewing
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
45 changes: 45 additions & 0 deletions
45
libs/ng-mocks/src/lib/mock-builder/promise/handle-entry-components.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,45 @@ | ||
import { ComponentFactoryResolver, NgModule } from '@angular/core'; | ||
|
||
let isIvy = false; | ||
try { | ||
// tslint:disable-next-line no-require-imports no-var-requires | ||
const module = require('@angular/core'); | ||
isIvy = module.ɵivyEnabled; | ||
} catch (e) { | ||
// nothing to do | ||
} | ||
|
||
import coreDefineProperty from '../../common/core.define-property'; | ||
import { extendClass } from '../../common/core.helpers'; | ||
import { NG_MOCKS } from '../../common/core.tokens'; | ||
import { isNgDef } from '../../common/func.is-ng-def'; | ||
|
||
import { NgMeta } from './types'; | ||
|
||
class EntryComponentsModule { | ||
protected origin: ComponentFactoryResolver['resolveComponentFactory']; | ||
|
||
public constructor(map: Map<any, any>, protected componentFactoryResolver: ComponentFactoryResolver) { | ||
this.origin = componentFactoryResolver.resolveComponentFactory; | ||
componentFactoryResolver.resolveComponentFactory = component => | ||
this.origin.call(componentFactoryResolver, map.get(component) ?? component) as any; | ||
} | ||
} | ||
coreDefineProperty(EntryComponentsModule, 'parameters', [[NG_MOCKS], [ComponentFactoryResolver]]); | ||
|
||
export default (ngModule: NgMeta): void => { | ||
const entryComponents: any[] = []; | ||
for (const declaration of ngModule.declarations) { | ||
if (isNgDef(declaration, 'c')) { | ||
entryComponents.push(declaration); | ||
} | ||
} | ||
// the way to cause entryComponents to do its work | ||
const entryComponent = extendClass(EntryComponentsModule); | ||
NgModule({ | ||
// Ivy knows how to make any component an entry point, | ||
// but we still would like to patch resolveComponentFactory in order to provide mocks. | ||
entryComponents: isIvy ? /* istanbul ignore next */ [] : entryComponents, | ||
})(entryComponent); | ||
ngModule.imports.push(entryComponent); | ||
}; |
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,124 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import * as core from '@angular/core'; | ||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
@core.Component({ | ||
selector: 'dynamic-overlay', | ||
template: | ||
'<ng-container *ngComponentOutlet="component"></ng-container>', | ||
}) | ||
export class DynamicOverlayComponent { | ||
public component?: core.Type<any>; | ||
|
||
public attachComponent(component: core.Type<any>) { | ||
this.component = component; | ||
} | ||
} | ||
|
||
@core.NgModule({ | ||
declarations: [DynamicOverlayComponent], | ||
exports: [DynamicOverlayComponent], | ||
imports: [CommonModule], | ||
}) | ||
export class OverlayModule {} | ||
|
||
@core.Component({ | ||
selector: 'dep-component', | ||
template: 'Dependency', | ||
}) | ||
class DepComponent {} | ||
|
||
@core.Component({ | ||
selector: 'mock-component', | ||
template: '<h1 *ngIf="flag"><dep-component></dep-component></h1>', | ||
}) | ||
class MockComponent { | ||
public flag = true; | ||
} | ||
|
||
describe('issue-333', () => { | ||
describe('1:keep', () => { | ||
// this should work with and without ivy | ||
beforeEach(() => | ||
MockBuilder(DynamicOverlayComponent, OverlayModule) | ||
.keep(MockComponent) | ||
.keep(DepComponent), | ||
); | ||
|
||
it('should render', () => { | ||
const fixture = MockRender(DynamicOverlayComponent); | ||
|
||
expect(ngMocks.formatHtml(fixture)).toEqual( | ||
`<dynamic-overlay></dynamic-overlay>`, | ||
); | ||
}); | ||
|
||
it('should project content', () => { | ||
const fixture = MockRender(DynamicOverlayComponent); | ||
fixture.point.componentInstance.attachComponent(MockComponent); | ||
fixture.detectChanges(); | ||
|
||
expect(ngMocks.formatText(fixture)).toEqual(`Dependency`); | ||
}); | ||
}); | ||
|
||
describe('2:mock', () => { | ||
// this should work with and without ivy | ||
beforeEach(() => | ||
MockBuilder(DynamicOverlayComponent, OverlayModule) | ||
.mock(MockComponent) | ||
.keep(CommonModule, { export: true }), | ||
); | ||
|
||
it('renders a mock component', () => { | ||
const fixture = MockRender(DynamicOverlayComponent); | ||
|
||
expect(ngMocks.formatHtml(fixture)).toEqual( | ||
`<dynamic-overlay></dynamic-overlay>`, | ||
); | ||
}); | ||
|
||
it('projects content', () => { | ||
const fixture = MockRender(DynamicOverlayComponent); | ||
fixture.point.componentInstance.attachComponent(MockComponent); | ||
fixture.detectChanges(); | ||
|
||
expect(ngMocks.formatHtml(fixture)).toEqual( | ||
`<dynamic-overlay><mock-component></mock-component></dynamic-overlay>`, | ||
); | ||
}); | ||
|
||
it('fails on unknown', () => { | ||
if ((core as any).ɵivyEnabled) { | ||
pending('ivy fails differently'); | ||
} else { | ||
const fixture = MockRender(DynamicOverlayComponent); | ||
fixture.point.componentInstance.attachComponent(DepComponent); | ||
expect(() => fixture.detectChanges()).toThrowError( | ||
/DepComponent/, | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
describe('3:mock', () => { | ||
// this should work with and without ivy | ||
beforeEach(() => | ||
MockBuilder(DynamicOverlayComponent, OverlayModule), | ||
); | ||
|
||
it('fails on unknown', () => { | ||
if ((core as any).ɵivyEnabled) { | ||
pending('ivy fails differently'); | ||
} else { | ||
const fixture = MockRender(DynamicOverlayComponent); | ||
fixture.point.componentInstance.attachComponent( | ||
MockComponent, | ||
); | ||
expect(() => fixture.detectChanges()).toThrowError( | ||
/MockComponent/, | ||
); | ||
} | ||
}); | ||
}); | ||
}); |