2015-08-07 14:41:38 -04:00
|
|
|
import {
|
|
|
|
ddescribe,
|
|
|
|
xdescribe,
|
|
|
|
describe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
expect,
|
|
|
|
beforeEach,
|
|
|
|
afterEach
|
2015-10-13 03:29:13 -04:00
|
|
|
} from 'angular2/testing_internal';
|
2015-08-07 14:41:38 -04:00
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
import {Injector, Inject, provide, Pipe, PipeTransform} from 'angular2/core';
|
2015-08-07 14:41:38 -04:00
|
|
|
import {ProtoPipes, Pipes} from 'angular2/src/core/pipes/pipes';
|
2015-10-11 01:11:13 -04:00
|
|
|
import {PipeProvider} from 'angular2/src/core/pipes/pipe_provider';
|
2015-08-07 14:41:38 -04:00
|
|
|
|
|
|
|
class PipeA implements PipeTransform {
|
|
|
|
transform(a, b) {}
|
|
|
|
onDestroy() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PipeB implements PipeTransform {
|
|
|
|
dep;
|
|
|
|
constructor(@Inject("dep") dep: any) { this.dep = dep; }
|
|
|
|
transform(a, b) {}
|
|
|
|
onDestroy() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe("Pipes", () => {
|
|
|
|
var injector;
|
|
|
|
|
2015-10-12 14:30:34 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
injector = Injector.resolveAndCreate([provide('dep', {useValue: 'dependency'})]);
|
|
|
|
});
|
2015-08-07 14:41:38 -04:00
|
|
|
|
|
|
|
it('should instantiate a pipe', () => {
|
2015-10-01 13:07:49 -04:00
|
|
|
var proto =
|
2015-10-11 01:11:13 -04:00
|
|
|
ProtoPipes.fromProviders([PipeProvider.createFromType(PipeA, new Pipe({name: 'a'}))]);
|
2015-08-07 14:41:38 -04:00
|
|
|
var pipes = new Pipes(proto, injector);
|
2015-09-08 12:17:58 -04:00
|
|
|
|
|
|
|
expect(pipes.get("a").pipe).toBeAnInstanceOf(PipeA);
|
2015-08-07 14:41:38 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when no pipe found', () => {
|
2015-10-11 01:11:13 -04:00
|
|
|
var proto = ProtoPipes.fromProviders([]);
|
2015-08-07 14:41:38 -04:00
|
|
|
var pipes = new Pipes(proto, injector);
|
|
|
|
expect(() => pipes.get("invalid")).toThrowErrorWith("Cannot find pipe 'invalid'");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should inject dependencies from the provided injector', () => {
|
2015-10-01 13:07:49 -04:00
|
|
|
var proto =
|
2015-10-11 01:11:13 -04:00
|
|
|
ProtoPipes.fromProviders([PipeProvider.createFromType(PipeB, new Pipe({name: 'b'}))]);
|
2015-08-07 14:41:38 -04:00
|
|
|
var pipes = new Pipes(proto, injector);
|
2015-09-08 12:17:58 -04:00
|
|
|
expect((<any>pipes.get("b").pipe).dep).toEqual("dependency");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should cache pure pipes', () => {
|
2015-10-11 01:11:13 -04:00
|
|
|
var proto = ProtoPipes.fromProviders(
|
|
|
|
[PipeProvider.createFromType(PipeA, new Pipe({name: 'a', pure: true}))]);
|
2015-09-08 12:17:58 -04:00
|
|
|
var pipes = new Pipes(proto, injector);
|
|
|
|
|
|
|
|
expect(pipes.get("a").pure).toEqual(true);
|
|
|
|
expect(pipes.get("a")).toBe(pipes.get("a"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT cache impure pipes', () => {
|
2015-10-11 01:11:13 -04:00
|
|
|
var proto = ProtoPipes.fromProviders(
|
|
|
|
[PipeProvider.createFromType(PipeA, new Pipe({name: 'a', pure: false}))]);
|
2015-09-08 12:17:58 -04:00
|
|
|
var pipes = new Pipes(proto, injector);
|
|
|
|
|
|
|
|
expect(pipes.get("a").pure).toEqual(false);
|
|
|
|
expect(pipes.get("a")).not.toBe(pipes.get("a"));
|
2015-08-07 14:41:38 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|