Skip to content

Commit

Permalink
feat: memoize function by arg
Browse files Browse the repository at this point in the history
  • Loading branch information
ike18t committed Feb 3, 2018
1 parent 155ade2 commit 031e3a6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 9 additions & 5 deletions lib/mock_directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class ExampleComponentContainer {}

describe('MockComponent', () => {
let fixture: ComponentFixture<ExampleComponentContainer>;
let directive: Type<ExampleDirective>;

getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
Expand All @@ -38,7 +37,7 @@ describe('MockComponent', () => {
TestBed.configureTestingModule({
declarations: [
ExampleComponentContainer,
directive = MockDirective(ExampleDirective)
MockDirective(ExampleDirective)
]
})
.compileComponents();
Expand All @@ -50,14 +49,19 @@ describe('MockComponent', () => {
});

it('should have use the original component\'s selector', () => {
const element = fixture.debugElement.query(By.directive(directive));
const element = fixture.debugElement.query(By.directive(MockDirective(ExampleDirective)));
expect(element).not.toBeNull();
});

it('should have the input set on the mock component', () => {
const debugElement = fixture.debugElement.query(By.directive(directive));
const element = debugElement.injector.get(directive);
const debugElement = fixture.debugElement.query(By.directive(MockDirective(ExampleDirective)));
const element = debugElement.injector.get(MockDirective(ExampleDirective));
expect(element.something).toEqual('hi');
expect(element.exampleDirective).toEqual('bye');
});

it('should memoize the return value by argument', () => {
expect(MockDirective(ExampleDirective)).toEqual(MockDirective(ExampleDirective));
expect(MockDirective(ExampleDirective)).not.toEqual(ExampleDirective);
});
});
12 changes: 11 additions & 1 deletion lib/mock_directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Directive, Type } from '@angular/core';

const cache = new Map<Type<Directive>, Type<Directive>>();

export function MockDirective<TDirective>(directive: Type<TDirective>): Type<TDirective> {
const cacheHit = cache.get(directive);
if (cacheHit) {
return cacheHit as Type<TDirective>;
}

const annotations = (directive as any).__annotations__[0] || {};
const propertyMetadata = (directive as any).__prop__metadata__ || {};

Expand All @@ -12,7 +19,10 @@ export function MockDirective<TDirective>(directive: Type<TDirective>): Type<TDi
selector: annotations.selector
};

return Directive(options)(class DirectiveMock {} as Type<TDirective>);
const mockedDirective = Directive(options)(class DirectiveMock {} as Type<TDirective>);
cache.set(directive, mockedDirective);

return mockedDirective;
}

function isInput(propertyMetadata: any): boolean {
Expand Down

0 comments on commit 031e3a6

Please sign in to comment.