/** * @license * Copyright Google LLC 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 */ import {CommonModule, NgLocalization} from '@angular/common'; import {Component, Injectable} from '@angular/core'; import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing'; import {expect} from '@angular/platform-browser/testing/src/matchers'; { describe('ngPlural', () => { let fixture: ComponentFixture; function getComponent(): TestComponent { return fixture.componentInstance; } function detectChangesAndExpectText(text: string): void { fixture.detectChanges(); expect(fixture.nativeElement).toHaveText(text); } afterEach(() => { fixture = null!; }); beforeEach(() => { TestBed.configureTestingModule({ declarations: [TestComponent], providers: [{provide: NgLocalization, useClass: TestLocalization}], imports: [CommonModule] }); }); it('should display the template according to the exact value', waitForAsync(() => { const template = ''; fixture = createTestComponent(template); getComponent().switchValue = 0; detectChangesAndExpectText('you have no messages.'); getComponent().switchValue = 1; detectChangesAndExpectText('you have one message.'); })); it('should display the template according to the exact numeric value', waitForAsync(() => { const template = '
' + '
    ' + '
  • you have no messages.
  • ' + '
  • you have one message.
  • ' + '
'; fixture = createTestComponent(template); getComponent().switchValue = 0; detectChangesAndExpectText('you have no messages.'); getComponent().switchValue = 1; detectChangesAndExpectText('you have one message.'); })); // https://github.com/angular/angular/issues/9868 // https://github.com/angular/angular/issues/9882 it('should not throw when ngPluralCase contains expressions', waitForAsync(() => { const template = ''; fixture = createTestComponent(template); getComponent().switchValue = 0; expect(() => fixture.detectChanges()).not.toThrow(); })); it('should be applicable to elements', waitForAsync(() => { const template = '' + 'you have no messages.' + 'you have one message.' + ''; fixture = createTestComponent(template); getComponent().switchValue = 0; detectChangesAndExpectText('you have no messages.'); getComponent().switchValue = 1; detectChangesAndExpectText('you have one message.'); })); it('should display the template according to the category', waitForAsync(() => { const template = '
    ' + '
  • you have a few messages.
  • ' + '
  • you have many messages.
  • ' + '
'; fixture = createTestComponent(template); getComponent().switchValue = 2; detectChangesAndExpectText('you have a few messages.'); getComponent().switchValue = 8; detectChangesAndExpectText('you have many messages.'); })); it('should default to other when no matches are found', waitForAsync(() => { const template = '
    ' + '
  • you have a few messages.
  • ' + '
  • default message.
  • ' + '
'; fixture = createTestComponent(template); getComponent().switchValue = 100; detectChangesAndExpectText('default message.'); })); it('should prioritize value matches over category matches', waitForAsync(() => { const template = '
    ' + '
  • you have a few messages.
  • ' + 'you have two messages.' + '
'; fixture = createTestComponent(template); getComponent().switchValue = 2; detectChangesAndExpectText('you have two messages.'); getComponent().switchValue = 3; detectChangesAndExpectText('you have a few messages.'); })); }); } @Injectable() class TestLocalization extends NgLocalization { getPluralCategory(value: number): string { if (value > 1 && value < 4) { return 'few'; } if (value >= 4 && value < 10) { return 'many'; } return 'other'; } } @Component({selector: 'test-cmp', template: ''}) class TestComponent { switchValue: number|null = null; } function createTestComponent(template: string): ComponentFixture { return TestBed.overrideComponent(TestComponent, {set: {template: template}}) .createComponent(TestComponent); }