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