| 
									
										
										
										
											2015-06-18 15:40:12 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   afterEach, | 
					
						
							|  |  |  |   SpyPipe, | 
					
						
							|  |  |  |   SpyPipeFactory | 
					
						
							|  |  |  | } from 'angular2/test_lib'; | 
					
						
							| 
									
										
										
										
											2015-02-12 14:56:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  | import {Injector, bind} from 'angular2/di'; | 
					
						
							| 
									
										
										
										
											2015-07-09 10:34:51 -07:00
										 |  |  | import {Pipes} from 'angular2/src/change_detection/pipes/pipes'; | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  | import {PipeFactory} from 'angular2/src/change_detection/pipes/pipe'; | 
					
						
							| 
									
										
										
										
											2015-02-12 14:56:41 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe("pipe registry", () => { | 
					
						
							| 
									
										
										
										
											2015-06-18 15:40:12 -07:00
										 |  |  |     var firstPipe; | 
					
						
							|  |  |  |     var secondPipe; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var firstPipeFactory; | 
					
						
							|  |  |  |     var secondPipeFactory; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       firstPipe = <any>new SpyPipe(); | 
					
						
							|  |  |  |       secondPipe = <any>new SpyPipe(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       firstPipeFactory = <any>new SpyPipeFactory(); | 
					
						
							|  |  |  |       secondPipeFactory = <any>new SpyPipeFactory(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should return an existing pipe if it can support the passed in object", () => { | 
					
						
							| 
									
										
										
										
											2015-07-09 10:34:51 -07:00
										 |  |  |       var r = new Pipes({"type": []}); | 
					
						
							| 
									
										
										
										
											2015-06-18 15:40:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       firstPipe.spy("supports").andReturn(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(r.get("type", "some object", null, firstPipe)).toEqual(firstPipe); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should call onDestroy on the provided pipe if it cannot support the provided object", | 
					
						
							|  |  |  |        () => { | 
					
						
							|  |  |  |          firstPipe.spy("supports").andReturn(false); | 
					
						
							|  |  |  |          firstPipeFactory.spy("supports").andReturn(true); | 
					
						
							|  |  |  |          firstPipeFactory.spy("create").andReturn(secondPipe); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 10:34:51 -07:00
										 |  |  |          var r = new Pipes({"type": [firstPipeFactory]}); | 
					
						
							| 
									
										
										
										
											2015-06-18 15:40:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          expect(r.get("type", "some object", null, firstPipe)).toEqual(secondPipe); | 
					
						
							|  |  |  |          expect(firstPipe.spy("onDestroy")).toHaveBeenCalled(); | 
					
						
							|  |  |  |        }); | 
					
						
							| 
									
										
										
										
											2015-02-12 14:56:41 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it("should return the first pipe supporting the data type", () => { | 
					
						
							| 
									
										
										
										
											2015-06-18 15:40:12 -07:00
										 |  |  |       firstPipeFactory.spy("supports").andReturn(false); | 
					
						
							|  |  |  |       firstPipeFactory.spy("create").andReturn(firstPipe); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       secondPipeFactory.spy("supports").andReturn(true); | 
					
						
							|  |  |  |       secondPipeFactory.spy("create").andReturn(secondPipe); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 10:34:51 -07:00
										 |  |  |       var r = new Pipes({"type": [firstPipeFactory, secondPipeFactory]}); | 
					
						
							| 
									
										
										
										
											2015-02-12 14:56:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-18 15:40:12 -07:00
										 |  |  |       expect(r.get("type", "some object")).toBe(secondPipe); | 
					
						
							| 
									
										
										
										
											2015-02-12 14:56:41 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should throw when no matching type", () => { | 
					
						
							| 
									
										
										
										
											2015-07-09 10:34:51 -07:00
										 |  |  |       var r = new Pipes({}); | 
					
						
							| 
									
										
										
										
											2015-06-18 15:40:12 -07:00
										 |  |  |       expect(() => r.get("unknown", "some object")) | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           .toThrowError(`Cannot find 'unknown' pipe supporting object 'some object'`); | 
					
						
							| 
									
										
										
										
											2015-02-12 14:56:41 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should throw when no matching pipe", () => { | 
					
						
							| 
									
										
										
										
											2015-07-09 10:34:51 -07:00
										 |  |  |       var r = new Pipes({"type": []}); | 
					
						
							| 
									
										
										
										
											2015-02-12 14:56:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-18 15:40:12 -07:00
										 |  |  |       expect(() => r.get("type", "some object")) | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           .toThrowError(`Cannot find 'type' pipe supporting object 'some object'`); | 
					
						
							| 
									
										
										
										
											2015-02-12 14:56:41 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 13:26:26 -07:00
										 |  |  |     describe('.create()', () => { | 
					
						
							|  |  |  |       it("should create a new Pipes object", () => { | 
					
						
							|  |  |  |         firstPipeFactory.spy("supports").andReturn(true); | 
					
						
							|  |  |  |         firstPipeFactory.spy("create").andReturn(firstPipe); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var pipes = Pipes.create({'async': [firstPipeFactory]}); | 
					
						
							|  |  |  |         expect(pipes.get("async", "first")).toBe(firstPipe); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should prepend passed it config in existing registry", () => { | 
					
						
							|  |  |  |         firstPipeFactory.spy("supports").andReturn(true); | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  |         secondPipeFactory.spy("supports").andReturn(true); | 
					
						
							|  |  |  |         secondPipeFactory.spy("create").andReturn(secondPipe); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:26:26 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         var pipes1 = Pipes.create({'async': [firstPipeFactory]}); | 
					
						
							|  |  |  |         var pipes2 = Pipes.create({'async': [secondPipeFactory]}, pipes1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(pipes2.get("async", "first")).toBe(secondPipe); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should use inherited pipes when no overrides support the provided object", () => { | 
					
						
							|  |  |  |         firstPipeFactory.spy("supports").andReturn(true); | 
					
						
							|  |  |  |         firstPipeFactory.spy("create").andReturn(firstPipe); | 
					
						
							|  |  |  |         secondPipeFactory.spy("supports").andReturn(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var pipes1 = Pipes.create({'async': [firstPipeFactory], 'date': [firstPipeFactory]}); | 
					
						
							|  |  |  |         var pipes2 = Pipes.create({'async': [secondPipeFactory]}, pipes1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(pipes2.get("async", "first")).toBe(firstPipe); | 
					
						
							|  |  |  |         expect(pipes2.get("date", "first")).toBe(firstPipe); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe(".extend()", () => { | 
					
						
							|  |  |  |       it('should create a factory that prepend new pipes to old', () => { | 
					
						
							|  |  |  |         firstPipeFactory.spy("supports").andReturn(true); | 
					
						
							|  |  |  |         secondPipeFactory.spy("supports").andReturn(true); | 
					
						
							|  |  |  |         secondPipeFactory.spy("create").andReturn(secondPipe); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  |         var originalPipes = new Pipes({'async': [firstPipeFactory]}); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:26:26 -07:00
										 |  |  |         var binding = Pipes.extend({'async':<PipeFactory[]>[secondPipeFactory]}); | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  |         var pipes: Pipes = binding.toFactory(originalPipes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(pipes.config['async'].length).toBe(2); | 
					
						
							|  |  |  |         expect(originalPipes.config['async'].length).toBe(1); | 
					
						
							|  |  |  |         expect(pipes.get('async', 'second plz')).toBe(secondPipe); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 13:26:26 -07:00
										 |  |  |       it('should throw if calling extend when creating root injector', () => { | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  |         secondPipeFactory.spy("supports").andReturn(true); | 
					
						
							|  |  |  |         secondPipeFactory.spy("create").andReturn(secondPipe); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 13:26:26 -07:00
										 |  |  |         var injector: Injector = | 
					
						
							|  |  |  |             Injector.resolveAndCreate([Pipes.extend({'async': [secondPipeFactory]})]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(() => injector.get(Pipes)) | 
					
						
							| 
									
										
										
										
											2015-07-23 18:00:19 -07:00
										 |  |  |             .toThrowErrorWith("Cannot extend Pipes without a parent injector"); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:26:26 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should extend di-inherited pipes', () => { | 
					
						
							|  |  |  |         firstPipeFactory.spy("supports").andReturn(true); | 
					
						
							|  |  |  |         firstPipeFactory.spy("create").andReturn(firstPipe); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         secondPipeFactory.spy("supports").andReturn(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  |         var originalPipes: Pipes = new Pipes({'async': [firstPipeFactory]}); | 
					
						
							|  |  |  |         var injector: Injector = Injector.resolveAndCreate([bind(Pipes).toValue(originalPipes)]); | 
					
						
							|  |  |  |         var childInjector: Injector = | 
					
						
							| 
									
										
										
										
											2015-07-17 13:26:26 -07:00
										 |  |  |             injector.resolveAndCreateChild([Pipes.extend({'async': [secondPipeFactory]})]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  |         var parentPipes: Pipes = injector.get(Pipes); | 
					
						
							|  |  |  |         var childPipes: Pipes = childInjector.get(Pipes); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:26:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  |         expect(childPipes.config['async'].length).toBe(2); | 
					
						
							|  |  |  |         expect(parentPipes.config['async'].length).toBe(1); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:26:26 -07:00
										 |  |  |         expect(childPipes.get('async', 'second plz')).toBe(firstPipe); | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-02-12 14:56:41 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-07-09 13:08:33 -07:00
										 |  |  | } |