| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 |  |  | import {CommonModule, SlicePipe} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | import {Component} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | import {TestBed, async} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {expect} from '@angular/platform-browser/testing/src/matchers'; | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   describe('SlicePipe', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     let list: number[]; | 
					
						
							|  |  |  |     let str: string; | 
					
						
							|  |  |  |     let pipe: SlicePipe; | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       list = [1, 2, 3, 4, 5]; | 
					
						
							|  |  |  |       str = 'tuvwxyz'; | 
					
						
							|  |  |  |       pipe = new SlicePipe(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     describe('supports', () => { | 
					
						
							| 
									
										
										
										
											2016-06-17 10:57:32 -07:00
										 |  |  |       it('should support strings', () => { expect(() => pipe.transform(str, 0)).not.toThrow(); }); | 
					
						
							|  |  |  |       it('should support lists', () => { expect(() => pipe.transform(list, 0)).not.toThrow(); }); | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 10:57:32 -07:00
										 |  |  |       it('should not support other objects', | 
					
						
							|  |  |  |          () => { expect(() => pipe.transform({}, 0)).toThrow(); }); | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     describe('transform', () => { | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 10:57:32 -07:00
										 |  |  |       it('should return null if the value is null', | 
					
						
							|  |  |  |          () => { expect(pipe.transform(null, 1)).toBe(null); }); | 
					
						
							| 
									
										
										
										
											2016-02-18 17:14:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  |       it('should return all items after START index when START is positive and END is omitted', | 
					
						
							|  |  |  |          () => { | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |            expect(pipe.transform(list, 3)).toEqual([4, 5]); | 
					
						
							|  |  |  |            expect(pipe.transform(str, 3)).toEqual('wxyz'); | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  |          }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should return last START items when START is negative and END is omitted', () => { | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |         expect(pipe.transform(list, -3)).toEqual([3, 4, 5]); | 
					
						
							|  |  |  |         expect(pipe.transform(str, -3)).toEqual('xyz'); | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should return all items between START and END index when START and END are positive', | 
					
						
							|  |  |  |          () => { | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |            expect(pipe.transform(list, 1, 3)).toEqual([2, 3]); | 
					
						
							|  |  |  |            expect(pipe.transform(str, 1, 3)).toEqual('uv'); | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  |          }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should return all items between START and END from the end when START and END are negative', | 
					
						
							|  |  |  |          () => { | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |            expect(pipe.transform(list, -4, -2)).toEqual([2, 3]); | 
					
						
							|  |  |  |            expect(pipe.transform(str, -4, -2)).toEqual('wx'); | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  |          }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should return an empty value if START is greater than END', () => { | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |         expect(pipe.transform(list, 4, 2)).toEqual([]); | 
					
						
							|  |  |  |         expect(pipe.transform(str, 4, 2)).toEqual(''); | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should return an empty value if START greater than input length', () => { | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |         expect(pipe.transform(list, 99)).toEqual([]); | 
					
						
							|  |  |  |         expect(pipe.transform(str, 99)).toEqual(''); | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 19:24:48 +02:00
										 |  |  |       it('should return entire input if START is negative and greater than input length', () => { | 
					
						
							|  |  |  |         expect(pipe.transform(list, -99)).toEqual([1, 2, 3, 4, 5]); | 
					
						
							|  |  |  |         expect(pipe.transform(str, -99)).toEqual('tuvwxyz'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not modify the input list', () => { | 
					
						
							|  |  |  |         expect(pipe.transform(list, 2)).toEqual([3, 4, 5]); | 
					
						
							|  |  |  |         expect(list).toEqual([1, 2, 3, 4, 5]); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -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 | slice:1).join(",") }}'}) | 
					
						
							|  |  |  |       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
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should work with mutable arrays', async(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const fixture = TestBed.createComponent(TestComp); | 
					
						
							|  |  |  |            const mutable: number[] = [1, 2]; | 
					
						
							| 
									
										
										
										
											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('2'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            mutable.push(3); | 
					
						
							|  |  |  |            fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-09-09 12:04:38 -07:00
										 |  |  |            expect(fixture.nativeElement).toHaveText('2,3'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-10-27 12:43:34 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-08-30 17:04:48 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-10-27 12:43:34 -07:00
										 |  |  | } |