mirror of https://github.com/abpframework/abp
parent
4df815ce9c
commit
af51255d3d
@ -0,0 +1,14 @@
|
||||
import { Injectable, Injector, TemplateRef, Type } from '@angular/core';
|
||||
import { ProjectionStrategy } from '../strategies/projection.strategy';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class ContentProjectionService {
|
||||
constructor(private injector: Injector) {}
|
||||
|
||||
projectContent<T extends Type<any> | TemplateRef<any>>(
|
||||
projectionStrategy: ProjectionStrategy<T>,
|
||||
injector = this.injector,
|
||||
) {
|
||||
return projectionStrategy.injectContent(injector);
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
import { Component, ComponentRef, NgModule } from '@angular/core';
|
||||
import { createServiceFactory, SpectatorService } from '@ngneat/spectator';
|
||||
import { ContentProjectionService } from '../services';
|
||||
import { PROJECTION_STRATEGY } from '../strategies';
|
||||
|
||||
describe('ContentProjectionService', () => {
|
||||
@Component({ template: '<div class="foo">bar</div>' })
|
||||
class TestComponent {}
|
||||
|
||||
// createServiceFactory does not accept entryComponents directly
|
||||
@NgModule({
|
||||
declarations: [TestComponent],
|
||||
entryComponents: [TestComponent],
|
||||
})
|
||||
class TestModule {}
|
||||
|
||||
let componentRef: ComponentRef<TestComponent>;
|
||||
let spectator: SpectatorService<ContentProjectionService>;
|
||||
const createService = createServiceFactory({
|
||||
service: ContentProjectionService,
|
||||
imports: [TestModule],
|
||||
});
|
||||
|
||||
beforeEach(() => (spectator = createService()));
|
||||
|
||||
afterEach(() => componentRef.destroy());
|
||||
|
||||
describe('#projectContent', () => {
|
||||
it('should call injectContent of given projectionStrategy and return what it returns', () => {
|
||||
const strategy = PROJECTION_STRATEGY.AppendComponentToBody(TestComponent);
|
||||
componentRef = spectator.service.projectContent(strategy);
|
||||
const foo = document.querySelector('body > ng-component > div.foo');
|
||||
|
||||
expect(componentRef).toBeInstanceOf(ComponentRef);
|
||||
expect(foo.textContent).toBe('bar');
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in new issue