| 
									
										
										
										
											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-07-22 09:20:51 -07:00
										 |  |  | import {AsyncTestCompleter, beforeEachProviders, beforeEach, ddescribe, describe, iit, inject, it, xit,} from '@angular/core/testing/testing_internal'; | 
					
						
							|  |  |  | import {expect} from '@angular/platform-browser/testing/matchers'; | 
					
						
							| 
									
										
										
										
											2016-07-11 16:04:32 -07:00
										 |  |  | import {TestComponentBuilder} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  | import {Component, Injectable} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {NgPlural, NgPluralCase, NgLocalization} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('switch', () => { | 
					
						
							| 
									
										
										
										
											2016-06-23 11:44:05 -07:00
										 |  |  |     beforeEachProviders(() => [{provide: NgLocalization, useClass: TestLocalization}]); | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should display the template according to the exact value', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var template = '<div>' + | 
					
						
							|  |  |  |                  '<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>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, template) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.switchValue = 0; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('you have no messages.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.switchValue = 1; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('you have one message.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |     it('should not throw when ngPluralCase contains expressions', | 
					
						
							|  |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var template = '<div>' + | 
					
						
							|  |  |  |                  '<ul [ngPlural]="switchValue">' + | 
					
						
							|  |  |  |                  '<template ngPluralCase="=0"><li>{{ switchValue }}</li></template>' + | 
					
						
							|  |  |  |                  '</ul></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, template) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.switchValue = 0; | 
					
						
							|  |  |  |                    expect(() => fixture.detectChanges()).not.toThrow(); | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 11:38:24 -07:00
										 |  |  |     it('should be applicable to <ng-container> elements', | 
					
						
							|  |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var template = '<div>' + | 
					
						
							|  |  |  |                  '<ng-container [ngPlural]="switchValue">' + | 
					
						
							|  |  |  |                  '<template ngPluralCase="=0">you have no messages.</template>' + | 
					
						
							|  |  |  |                  '<template ngPluralCase="=1">you have one message.</template>' + | 
					
						
							|  |  |  |                  '</ng-container></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, template) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.switchValue = 0; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('you have no messages.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.switchValue = 1; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('you have one message.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  |     it('should display the template according to the category', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var template = '<div>' + | 
					
						
							|  |  |  |                  '<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>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, template) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.switchValue = 2; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement) | 
					
						
							|  |  |  |                        .toHaveText('you have a few messages.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.switchValue = 8; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('you have many messages.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should default to other when no matches are found', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var template = '<div>' + | 
					
						
							|  |  |  |                  '<ul [ngPlural]="switchValue">' + | 
					
						
							|  |  |  |                  '<template ngPluralCase="few"><li>you have a few messages.</li></template>' + | 
					
						
							|  |  |  |                  '<template ngPluralCase="other"><li>default message.</li></template>' + | 
					
						
							|  |  |  |                  '</ul></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, template) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.switchValue = 100; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('default message.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							| 
									
										
										
										
											2016-03-04 16:32:22 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should prioritize value matches over category matches', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var template = '<div>' + | 
					
						
							|  |  |  |                  '<ul [ngPlural]="switchValue">' + | 
					
						
							|  |  |  |                  '<template ngPluralCase="few"><li>you have a few messages.</li></template>' + | 
					
						
							|  |  |  |                  '<template ngPluralCase="=2">you have two messages.</template>' + | 
					
						
							|  |  |  |                  '</ul></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, template) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.switchValue = 2; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('you have two messages.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.switchValue = 3; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement) | 
					
						
							|  |  |  |                        .toHaveText('you have a few messages.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  |     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-06-23 11:44:05 -07:00
										 |  |  | @Component({selector: 'test-cmp', directives: [NgPlural, NgPluralCase], 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
										 |  |  | } |