From 0bb9d2176020c35f62c4544ec735f9f59dbac19d Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Fri, 10 Apr 2020 01:34:59 +0300 Subject: [PATCH] feat: simplify predefined projection strategies --- .../src/lib/strategies/projection.strategy.ts | 61 +++++++++---------- .../src/lib/tests/projection.strategy.spec.ts | 49 ++++++++++++--- 2 files changed, 68 insertions(+), 42 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/strategies/projection.strategy.ts b/npm/ng-packs/packages/core/src/lib/strategies/projection.strategy.ts index 4d8c410480..ad5927e09e 100644 --- a/npm/ng-packs/packages/core/src/lib/strategies/projection.strategy.ts +++ b/npm/ng-packs/packages/core/src/lib/strategies/projection.strategy.ts @@ -8,14 +8,9 @@ import { Type, ViewContainerRef, } from '@angular/core'; -import { InferedInstanceOf } from '../models/utility'; +import { InferedContextOf, InferedInstanceOf } from '../models/utility'; import { ContainerStrategy, CONTAINER_STRATEGY } from './container.strategy'; -import { - ComponentContextStrategy, - ContextStrategy, - CONTEXT_STRATEGY, - TemplateContextStrategy, -} from './context.strategy'; +import { ContextStrategy, CONTEXT_STRATEGY } from './context.strategy'; import { DomStrategy, DOM_STRATEGY } from './dom.strategy'; export abstract class ProjectionStrategy { @@ -78,14 +73,14 @@ export class RootComponentProjectionStrategy> extends Projec export class TemplateProjectionStrategy> extends ProjectionStrategy { constructor( - template: T, + templateRef: T, private containerStrategy: ContainerStrategy, private contextStrategy = CONTEXT_STRATEGY.None(), ) { - super(template); + super(templateRef); } - injectContent(injector: Injector) { + injectContent() { this.containerStrategy.prepare(); const embeddedViewRef = this.containerStrategy.containerRef.createEmbeddedView( @@ -100,76 +95,76 @@ export class TemplateProjectionStrategy> extends Proj } export const PROJECTION_STRATEGY = { - AppendComponentToBody>( - component: T, - contextStrategy?: ComponentContextStrategy, - ) { - return new RootComponentProjectionStrategy(component, contextStrategy); + AppendComponentToBody>(component: T, context?: InferedInstanceOf) { + return new RootComponentProjectionStrategy( + component, + context && CONTEXT_STRATEGY.Component(context), + ); }, AppendComponentToContainer>( component: T, containerRef: ViewContainerRef, - contextStrategy?: ComponentContextStrategy, + context?: InferedInstanceOf, ) { return new ComponentProjectionStrategy( component, CONTAINER_STRATEGY.Append(containerRef), - contextStrategy, + context && CONTEXT_STRATEGY.Component(context), ); }, AppendTemplateToContainer>( - template: T, + templateRef: T, containerRef: ViewContainerRef, - contextStrategy?: TemplateContextStrategy, + context?: InferedContextOf, ) { return new TemplateProjectionStrategy( - template, + templateRef, CONTAINER_STRATEGY.Append(containerRef), - contextStrategy, + context && CONTEXT_STRATEGY.Template(context), ); }, PrependComponentToContainer>( component: T, containerRef: ViewContainerRef, - contextStrategy?: ComponentContextStrategy, + context?: InferedInstanceOf, ) { return new ComponentProjectionStrategy( component, CONTAINER_STRATEGY.Prepend(containerRef), - contextStrategy, + context && CONTEXT_STRATEGY.Component(context), ); }, PrependTemplateToContainer>( - template: T, + templateRef: T, containerRef: ViewContainerRef, - contextStrategy?: TemplateContextStrategy, + context?: InferedContextOf, ) { return new TemplateProjectionStrategy( - template, + templateRef, CONTAINER_STRATEGY.Prepend(containerRef), - contextStrategy, + context && CONTEXT_STRATEGY.Template(context), ); }, ProjectComponentToContainer>( component: T, containerRef: ViewContainerRef, - contextStrategy?: ComponentContextStrategy, + context?: InferedInstanceOf, ) { return new ComponentProjectionStrategy( component, CONTAINER_STRATEGY.Clear(containerRef), - contextStrategy, + context && CONTEXT_STRATEGY.Component(context), ); }, ProjectTemplateToContainer>( - template: T, + templateRef: T, containerRef: ViewContainerRef, - contextStrategy?: TemplateContextStrategy, + context?: InferedContextOf, ) { return new TemplateProjectionStrategy( - template, + templateRef, CONTAINER_STRATEGY.Clear(containerRef), - contextStrategy, + context && CONTEXT_STRATEGY.Template(context), ); }, }; diff --git a/npm/ng-packs/packages/core/src/lib/tests/projection.strategy.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/projection.strategy.spec.ts index 71a531c5eb..1166d44f57 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/projection.strategy.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/projection.strategy.spec.ts @@ -178,7 +178,7 @@ describe('TemplateProjectionStrategy', () => { it('should should insert content into container and return an EmbeddedViewRef', () => { const templateRef = spectator.component.templateRef; const strategy = new TemplateProjectionStrategy(templateRef, containerStrategy); - embeddedViewRef = strategy.injectContent(spectator); + embeddedViewRef = strategy.injectContent(); spectator.detectChanges(); const div = spectator.query('div.foo'); @@ -200,7 +200,7 @@ describe('TemplateProjectionStrategy', () => { containerStrategy, contextStrategy, ); - embeddedViewRef = strategy.injectContent(spectator); + embeddedViewRef = strategy.injectContent(); spectator.detectChanges(); const div = spectator.query('div.foo'); @@ -213,6 +213,8 @@ describe('TemplateProjectionStrategy', () => { describe('PROJECTION_STRATEGY', () => { const content = undefined; const containerRef = ({ length: 0 } as any) as ViewContainerRef; + let context = undefined; + test.each` name | Strategy | containerStrategy ${'AppendComponentToContainer'} | ${ComponentProjectionStrategy} | ${CONTAINER_STRATEGY.Append} @@ -222,23 +224,52 @@ describe('PROJECTION_STRATEGY', () => { ${'ProjectComponentToContainer'} | ${ComponentProjectionStrategy} | ${CONTAINER_STRATEGY.Clear} ${'ProjectTemplateToContainer'} | ${TemplateProjectionStrategy} | ${CONTAINER_STRATEGY.Clear} `( - 'should successfully map $name to $Strategy.name with $containerStrategy.name container strategy', + 'should successfully map $name to $Strategy.name with $containerStrategy.name container strategy and $contextStrategy.name context strategy', ({ name, Strategy, containerStrategy }) => { - expect(PROJECTION_STRATEGY[name](content, containerRef)).toEqual( - new Strategy(content, containerStrategy(containerRef)), + expect(PROJECTION_STRATEGY[name](content, containerRef, context)).toEqual( + new Strategy(content, containerStrategy(containerRef), CONTEXT_STRATEGY.None()), ); }, ); - - const contextStrategy = undefined; test.each` name | Strategy | domStrategy ${'AppendComponentToBody'} | ${RootComponentProjectionStrategy} | ${DOM_STRATEGY.AppendToBody} `( 'should successfully map $name to $Strategy.name with $domStrategy.name dom strategy', ({ name, Strategy, domStrategy }) => { - expect(PROJECTION_STRATEGY[name](content, contextStrategy)).toEqual( - new Strategy(content, contextStrategy, domStrategy()), + expect(PROJECTION_STRATEGY[name](content, context)).toEqual( + new Strategy(content, CONTEXT_STRATEGY.None(), domStrategy()), + ); + }, + ); + + test.each` + name | Strategy | containerStrategy | contextStrategy + ${'AppendComponentToContainer'} | ${ComponentProjectionStrategy} | ${CONTAINER_STRATEGY.Append} | ${CONTEXT_STRATEGY.Component} + ${'AppendTemplateToContainer'} | ${TemplateProjectionStrategy} | ${CONTAINER_STRATEGY.Append} | ${CONTEXT_STRATEGY.Template} + ${'PrependComponentToContainer'} | ${ComponentProjectionStrategy} | ${CONTAINER_STRATEGY.Prepend} | ${CONTEXT_STRATEGY.Component} + ${'PrependTemplateToContainer'} | ${TemplateProjectionStrategy} | ${CONTAINER_STRATEGY.Prepend} | ${CONTEXT_STRATEGY.Template} + ${'ProjectComponentToContainer'} | ${ComponentProjectionStrategy} | ${CONTAINER_STRATEGY.Clear} | ${CONTEXT_STRATEGY.Component} + ${'ProjectTemplateToContainer'} | ${TemplateProjectionStrategy} | ${CONTAINER_STRATEGY.Clear} | ${CONTEXT_STRATEGY.Template} + `( + 'should successfully map $name to $Strategy.name with $containerStrategy.name container strategy and $contextStrategy.name context strategy', + ({ name, Strategy, containerStrategy, contextStrategy }) => { + context = { x: true }; + expect(PROJECTION_STRATEGY[name](content, containerRef, context)).toEqual( + new Strategy(content, containerStrategy(containerRef), contextStrategy(context)), + ); + }, + ); + + test.each` + name | Strategy | contextStrategy | domStrategy + ${'AppendComponentToBody'} | ${RootComponentProjectionStrategy} | ${CONTEXT_STRATEGY.Component} | ${DOM_STRATEGY.AppendToBody} + `( + 'should successfully map $name to $Strategy.name with $contextStrategy.name context strategy and $domStrategy.name dom strategy', + ({ name, Strategy, domStrategy, contextStrategy }) => { + context = { x: true }; + expect(PROJECTION_STRATEGY[name](content, context)).toEqual( + new Strategy(content, contextStrategy(context), domStrategy()), ); }, );