Browse Source

feat: add container strategies

pull/3544/head
Arman Ozak 7 years ago
parent
commit
4f31c1bfc0
  1. 44
      npm/ng-packs/packages/core/src/lib/strategies/container.strategy.ts
  2. 1
      npm/ng-packs/packages/core/src/lib/strategies/index.ts
  3. 80
      npm/ng-packs/packages/core/src/lib/tests/container.strategy.spec.ts

44
npm/ng-packs/packages/core/src/lib/strategies/container.strategy.ts

@ -0,0 +1,44 @@
import { ViewContainerRef } from '@angular/core';
export abstract class ContainerStrategy {
constructor(public containerRef: ViewContainerRef) {}
abstract getIndex(): number;
prepare(): void {}
}
export class ClearContainerStrategy extends ContainerStrategy {
getIndex(): number {
return 0;
}
prepare() {
this.containerRef.clear();
}
}
export class InsertIntoContainerStrategy extends ContainerStrategy {
constructor(containerRef: ViewContainerRef, private index: number) {
super(containerRef);
}
getIndex() {
return Math.min(Math.max(0, this.index), this.containerRef.length);
}
}
export const CONTAINER_STRATEGY = {
Clear(containerRef: ViewContainerRef) {
return new ClearContainerStrategy(containerRef);
},
Append(containerRef: ViewContainerRef) {
return new InsertIntoContainerStrategy(containerRef, containerRef.length);
},
Prepend(containerRef: ViewContainerRef) {
return new InsertIntoContainerStrategy(containerRef, 0);
},
Insert(containerRef: ViewContainerRef, index: number) {
return new InsertIntoContainerStrategy(containerRef, index);
},
};

1
npm/ng-packs/packages/core/src/lib/strategies/index.ts

@ -1,3 +1,4 @@
export * from './container.strategy';
export * from './content-security.strategy';
export * from './content.strategy';
export * from './context.strategy';

80
npm/ng-packs/packages/core/src/lib/tests/container.strategy.spec.ts

@ -0,0 +1,80 @@
import { ViewContainerRef } from '@angular/core';
import {
ClearContainerStrategy,
CONTAINER_STRATEGY,
InsertIntoContainerStrategy,
} from '../strategies';
describe('ClearContainerStrategy', () => {
const containerRef = ({
clear: jest.fn(),
length: 7,
} as any) as ViewContainerRef;
describe('#getIndex', () => {
it('should return 0', () => {
const strategy = new ClearContainerStrategy(containerRef);
expect(strategy.getIndex()).toBe(0);
});
});
describe('#prepare', () => {
it('should call clear method of containerRef once', () => {
const strategy = new ClearContainerStrategy(containerRef);
strategy.prepare();
expect(strategy.getIndex()).toBe(0);
expect(containerRef.clear).toHaveBeenCalledTimes(1);
});
});
});
describe('InsertIntoContainerStrategy', () => {
const containerRef = ({
clear: jest.fn(),
length: 7,
} as any) as ViewContainerRef;
describe('#getIndex', () => {
test.each`
index | expected
${0} | ${0}
${4} | ${4}
${9} | ${7}
${-1} | ${0}
${Infinity} | ${7}
`(
'should return $expected when index is given $index',
({ index, expected }: { index: number; expected: number }) => {
const strategy = new InsertIntoContainerStrategy(containerRef, index);
expect(strategy.getIndex()).toBe(expected);
},
);
});
describe('#prepare', () => {
it('should not call clear method of containerRef', () => {
const strategy = new InsertIntoContainerStrategy(containerRef, 0);
strategy.prepare();
expect(containerRef.clear).not.toHaveBeenCalled();
});
});
});
describe('CONTAINER_STRATEGY', () => {
const containerRef = ({
clear: jest.fn(),
length: 7,
} as any) as ViewContainerRef;
test.each`
name | Strategy | index
${'Clear'} | ${ClearContainerStrategy} | ${undefined}
${'Append'} | ${InsertIntoContainerStrategy} | ${containerRef.length}
${'Prepend'} | ${InsertIntoContainerStrategy} | ${0}
${'Insert'} | ${InsertIntoContainerStrategy} | ${4}
`('should successfully map $name to $Strategy.name', ({ name, Strategy, index }) => {
expect(CONTAINER_STRATEGY[name](containerRef, index)).toEqual(
new Strategy(containerRef, index),
);
});
});
Loading…
Cancel
Save