| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   afterEach, | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   proxy, | 
					
						
							|  |  |  |   TestComponentBuilder | 
					
						
							|  |  |  | } from 'angular2/testing_internal'; | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {Json, RegExp, NumberWrapper, StringWrapper} from 'angular2/src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 15:55:43 -08:00
										 |  |  | import {Component} from 'angular2/core'; | 
					
						
							|  |  |  | import {JsonPipe} from 'angular2/common'; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |   describe("JsonPipe", () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |     var regNewLine = '\n'; | 
					
						
							|  |  |  |     var inceptionObj; | 
					
						
							|  |  |  |     var inceptionObjString; | 
					
						
							|  |  |  |     var pipe; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function normalize(obj: string): string { return StringWrapper.replace(obj, regNewLine, ''); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2015-06-12 07:50:45 -07:00
										 |  |  |       inceptionObj = {dream: {dream: {dream: 'Limbo'}}}; | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       inceptionObjString = "{\n" + "  \"dream\": {\n" + "    \"dream\": {\n" + | 
					
						
							|  |  |  |                            "      \"dream\": \"Limbo\"\n" + "    }\n" + "  }\n" + "}"; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       pipe = new JsonPipe(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |     describe("transform", () => { | 
					
						
							|  |  |  |       it("should return JSON-formatted string", | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |          () => { expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString); }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       it("should return JSON-formatted string even when normalized", () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         var dream1 = normalize(pipe.transform(inceptionObj)); | 
					
						
							|  |  |  |         var dream2 = normalize(inceptionObjString); | 
					
						
							|  |  |  |         expect(dream1).toEqual(dream2); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       it("should return JSON-formatted string similar to Json.stringify", () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         var dream1 = normalize(pipe.transform(inceptionObj)); | 
					
						
							|  |  |  |         var dream2 = normalize(Json.stringify(inceptionObj)); | 
					
						
							|  |  |  |         expect(dream1).toEqual(dream2); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-10-27 12:43:34 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 12:43:34 -07:00
										 |  |  |     describe('integration', () => { | 
					
						
							|  |  |  |       it('should work with mutable objects', | 
					
						
							|  |  |  |          inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |            tcb.createAsync(TestComp).then((fixture) => { | 
					
						
							| 
									
										
										
										
											2015-10-27 12:43:34 -07:00
										 |  |  |              let mutable: number[] = [1]; | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |              fixture.debugElement.componentInstance.data = mutable; | 
					
						
							|  |  |  |              fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |              expect(fixture.debugElement.nativeElement).toHaveText("[\n  1\n]"); | 
					
						
							| 
									
										
										
										
											2015-10-27 12:43:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |              mutable.push(2); | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |              fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |              expect(fixture.debugElement.nativeElement).toHaveText("[\n  1,\n  2\n]"); | 
					
						
							| 
									
										
										
										
											2015-10-27 12:43:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |            }); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-10-27 12:43:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'test-comp', template: '{{data | json}}', pipes: [JsonPipe]}) | 
					
						
							|  |  |  | class TestComp { | 
					
						
							|  |  |  |   data: any; | 
					
						
							|  |  |  | } |