From 4ad6bcce54b2c26a5f989b5eb13c548fd6dd3276 Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Thu, 28 Jul 2016 06:40:50 -0700 Subject: [PATCH] feat(compiler): add `MockPipeResolver` --- .../compiler/test/pipe_resolver_mock_spec.ts | 39 ++++++++++++++++ .../testing/ng_module_resolver_mock.ts | 3 +- .../compiler/testing/pipe_resolver_mock.ts | 45 +++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 modules/@angular/compiler/test/pipe_resolver_mock_spec.ts create mode 100644 modules/@angular/compiler/testing/pipe_resolver_mock.ts diff --git a/modules/@angular/compiler/test/pipe_resolver_mock_spec.ts b/modules/@angular/compiler/test/pipe_resolver_mock_spec.ts new file mode 100644 index 0000000000..ff40b096e9 --- /dev/null +++ b/modules/@angular/compiler/test/pipe_resolver_mock_spec.ts @@ -0,0 +1,39 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {beforeEach, ddescribe, describe, expect, iit, it, inject,} from '@angular/core/testing/testing_internal'; + +import {stringify, isBlank} from '../src/facade/lang'; +import {MockPipeResolver} from '../testing'; +import {Pipe, PipeMetadata, Injector} from '@angular/core'; + +export function main() { + describe('MockPipeResolver', () => { + var pipeResolver: MockPipeResolver; + + beforeEach(inject( + [Injector], (injector: Injector) => { pipeResolver = new MockPipeResolver(injector); })); + + describe('Pipe overriding', () => { + it('should fallback to the default PipeResolver when templates are not overridden', () => { + var pipe = pipeResolver.resolve(SomePipe); + expect(pipe.name).toEqual('somePipe'); + }); + + it('should allow overriding the @Pipe', () => { + pipeResolver.setPipe(SomePipe, new PipeMetadata({name: 'someOtherName'})); + var pipe = pipeResolver.resolve(SomePipe); + expect(pipe.name).toEqual('someOtherName'); + }); + }); + }); +} + +@Pipe({name: 'somePipe'}) +class SomePipe { +} diff --git a/modules/@angular/compiler/testing/ng_module_resolver_mock.ts b/modules/@angular/compiler/testing/ng_module_resolver_mock.ts index fc270f6b4e..105fcf0ad7 100644 --- a/modules/@angular/compiler/testing/ng_module_resolver_mock.ts +++ b/modules/@angular/compiler/testing/ng_module_resolver_mock.ts @@ -13,8 +13,7 @@ import {Map} from '../src/facade/collection'; @Injectable() export class MockNgModuleResolver extends NgModuleResolver { - /** @internal */ - _ngModules = new Map(); + private _ngModules = new Map(); constructor(private _injector: Injector) { super(); } diff --git a/modules/@angular/compiler/testing/pipe_resolver_mock.ts b/modules/@angular/compiler/testing/pipe_resolver_mock.ts new file mode 100644 index 0000000000..a0ef928ef2 --- /dev/null +++ b/modules/@angular/compiler/testing/pipe_resolver_mock.ts @@ -0,0 +1,45 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {Compiler, Injectable, Injector, PipeMetadata, Type} from '@angular/core'; + +import {PipeResolver} from '../index'; +import {Map} from '../src/facade/collection'; + +@Injectable() +export class MockPipeResolver extends PipeResolver { + private _pipes = new Map(); + + constructor(private _injector: Injector) { super(); } + + private get _compiler(): Compiler { return this._injector.get(Compiler); } + + private _clearCacheFor(component: Type) { this._compiler.clearCacheFor(component); } + + /** + * Overrides the {@link PipeMetadata} for a pipe. + */ + setPipe(type: Type, metadata: PipeMetadata): void { + this._pipes.set(type, metadata); + this._clearCacheFor(type); + } + + /** + * Returns the {@link PipeMetadata} for a pipe: + * - Set the {@link PipeMetadata} to the overridden view when it exists or fallback to the + * default + * `PipeResolver`, see `setPipe`. + */ + resolve(type: Type, throwIfNotFound = true): PipeMetadata { + var metadata = this._pipes.get(type); + if (!metadata) { + metadata = super.resolve(type, throwIfNotFound); + } + return metadata; + } +}