| 
									
										
										
										
											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, NgLocalization} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  | import {Component, Injectable} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  | import {ComponentFixture, TestBed, async} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | import {expect} from '@angular/platform-browser/testing/matchers'; | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('switch', () => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |     let fixture: ComponentFixture<any>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getComponent(): TestComponent { return fixture.componentInstance; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function detectChangesAndExpectText<T>(text: string): void { | 
					
						
							|  |  |  |       fixture.detectChanges(); | 
					
						
							|  |  |  |       expect(fixture.nativeElement).toHaveText(text); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     afterEach(() => { fixture = null; }); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       TestBed.configureTestingModule({ | 
					
						
							|  |  |  |         declarations: [TestComponent], | 
					
						
							| 
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 |  |  |         providers: [{provide: NgLocalization, useClass: TestLocalization}], | 
					
						
							|  |  |  |         imports: [CommonModule] | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should display the template according to the exact value', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = '<div>' + | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |              '<ul [ngPlural]="switchValue">' + | 
					
						
							|  |  |  |              '<template ngPluralCase="=0"><li>you have no messages.</li></template>' + | 
					
						
							|  |  |  |              '<template ngPluralCase="=1"><li>you have one message.</li></template>' + | 
					
						
							|  |  |  |              '</ul></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().switchValue = 0; | 
					
						
							|  |  |  |          detectChangesAndExpectText('you have no messages.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().switchValue = 1; | 
					
						
							|  |  |  |          detectChangesAndExpectText('you have one message.'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 14:47:06 -07:00
										 |  |  |     // https://github.com/angular/angular/issues/9868
 | 
					
						
							|  |  |  |     // https://github.com/angular/angular/issues/9882
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     it('should not throw when ngPluralCase contains expressions', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = '<div>' + | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |              '<ul [ngPlural]="switchValue">' + | 
					
						
							|  |  |  |              '<template ngPluralCase="=0"><li>{{ switchValue }}</li></template>' + | 
					
						
							|  |  |  |              '</ul></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().switchValue = 0; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          expect(() => fixture.detectChanges()).not.toThrow(); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should be applicable to <ng-container> elements', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = '<div>' + | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |              '<ng-container [ngPlural]="switchValue">' + | 
					
						
							|  |  |  |              '<template ngPluralCase="=0">you have no messages.</template>' + | 
					
						
							|  |  |  |              '<template ngPluralCase="=1">you have one message.</template>' + | 
					
						
							|  |  |  |              '</ng-container></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().switchValue = 0; | 
					
						
							|  |  |  |          detectChangesAndExpectText('you have no messages.'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().switchValue = 1; | 
					
						
							|  |  |  |          detectChangesAndExpectText('you have one message.'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should display the template according to the category', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = '<div>' + | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |              '<ul [ngPlural]="switchValue">' + | 
					
						
							|  |  |  |              '<template ngPluralCase="few"><li>you have a few messages.</li></template>' + | 
					
						
							|  |  |  |              '<template ngPluralCase="many"><li>you have many messages.</li></template>' + | 
					
						
							|  |  |  |              '</ul></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().switchValue = 2; | 
					
						
							|  |  |  |          detectChangesAndExpectText('you have a few messages.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().switchValue = 8; | 
					
						
							|  |  |  |          detectChangesAndExpectText('you have many messages.'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should default to other when no matches are found', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = '<div>' + | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |              '<ul [ngPlural]="switchValue">' + | 
					
						
							|  |  |  |              '<template ngPluralCase="few"><li>you have a few messages.</li></template>' + | 
					
						
							|  |  |  |              '<template ngPluralCase="other"><li>default message.</li></template>' + | 
					
						
							|  |  |  |              '</ul></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().switchValue = 100; | 
					
						
							|  |  |  |          detectChangesAndExpectText('default message.'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should prioritize value matches over category matches', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = '<div>' + | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |              '<ul [ngPlural]="switchValue">' + | 
					
						
							|  |  |  |              '<template ngPluralCase="few"><li>you have a few messages.</li></template>' + | 
					
						
							|  |  |  |              '<template ngPluralCase="=2">you have two messages.</template>' + | 
					
						
							|  |  |  |              '</ul></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().switchValue = 2; | 
					
						
							|  |  |  |          detectChangesAndExpectText('you have two messages.'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().switchValue = 3; | 
					
						
							|  |  |  |          detectChangesAndExpectText('you have a few messages.'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Injectable() | 
					
						
							| 
									
										
										
										
											2016-06-23 11:44:05 -07:00
										 |  |  | class TestLocalization extends NgLocalization { | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  |   getPluralCategory(value: number): string { | 
					
						
							|  |  |  |     if (value > 1 && value < 4) { | 
					
						
							|  |  |  |       return 'few'; | 
					
						
							| 
									
										
										
										
											2016-06-23 11:44:05 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:44:05 -07:00
										 |  |  |     if (value >= 4 && value < 10) { | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  |       return 'many'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-06-23 11:44:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 'other'; | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 |  |  | @Component({selector: 'test-cmp', template: ''}) | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  | class TestComponent { | 
					
						
							| 
									
										
										
										
											2016-06-17 11:38:24 -07:00
										 |  |  |   switchValue: number = null; | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | function createTestComponent(template: string): ComponentFixture<TestComponent> { | 
					
						
							|  |  |  |   return TestBed.overrideComponent(TestComponent, {set: {template: template}}) | 
					
						
							|  |  |  |       .createComponent(TestComponent); | 
					
						
							|  |  |  | } |