| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 |  |  | import {CommonModule, JsonPipe} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | import {Component} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  | import {TestBed, waitForAsync} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {expect} from '@angular/platform-browser/testing/src/matchers'; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   describe('JsonPipe', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const regNewLine = '\n'; | 
					
						
							|  |  |  |     let inceptionObj: any; | 
					
						
							|  |  |  |     let inceptionObjString: string; | 
					
						
							|  |  |  |     let pipe: JsonPipe; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     function normalize(obj: string): string { | 
					
						
							|  |  |  |       return obj.replace(regNewLine, ''); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2015-06-12 07:50:45 -07:00
										 |  |  |       inceptionObj = {dream: {dream: {dream: 'Limbo'}}}; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -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-06-08 16:38:52 -07:00
										 |  |  |     describe('transform', () => { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       it('should return JSON-formatted string', () => { | 
					
						
							|  |  |  |         expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       it('should return JSON-formatted string even when normalized', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const dream1 = normalize(pipe.transform(inceptionObj)); | 
					
						
							|  |  |  |         const dream2 = normalize(inceptionObjString); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         expect(dream1).toEqual(dream2); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       it('should return JSON-formatted string similar to Json.stringify', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const dream1 = normalize(pipe.transform(inceptionObj)); | 
					
						
							|  |  |  |         const dream2 = normalize(JSON.stringify(inceptionObj, null, 2)); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         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', () => { | 
					
						
							| 
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 |  |  |       @Component({selector: 'test-comp', template: '{{data | json}}'}) | 
					
						
							|  |  |  |       class TestComp { | 
					
						
							|  |  |  |         data: any; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 |  |  |         TestBed.configureTestingModule({declarations: [TestComp], imports: [CommonModule]}); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  |       it('should work with mutable objects', waitForAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const fixture = TestBed.createComponent(TestComp); | 
					
						
							|  |  |  |            const mutable: number[] = [1]; | 
					
						
							| 
									
										
										
										
											2016-09-09 12:04:38 -07:00
										 |  |  |            fixture.componentInstance.data = mutable; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |            fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-09-09 12:04:38 -07:00
										 |  |  |            expect(fixture.nativeElement).toHaveText('[\n  1\n]'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            mutable.push(2); | 
					
						
							|  |  |  |            fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-09-09 12:04:38 -07:00
										 |  |  |            expect(fixture.nativeElement).toHaveText('[\n  1,\n  2\n]'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |