2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-23 12:47:54 -04:00
|
|
|
*
|
|
|
|
* 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-06-23 14:44:05 -04:00
|
|
|
import {I18nPluralPipe, NgLocalization} from '@angular/common';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {PipeResolver} from '@angular/compiler/src/pipe_resolver';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal';
|
2017-08-16 12:00:03 -04:00
|
|
|
import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_reflector';
|
2016-02-26 13:02:52 -05:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('I18nPluralPipe', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let localization: NgLocalization;
|
|
|
|
let pipe: I18nPluralPipe;
|
2016-06-23 14:44:05 -04:00
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const mapping = {
|
2016-06-08 19:38:52 -04:00
|
|
|
'=0': 'No messages.',
|
|
|
|
'=1': 'One message.',
|
2016-06-23 14:44:05 -04:00
|
|
|
'many': 'Many messages.',
|
|
|
|
'other': 'There are # messages, that is #.',
|
2016-06-08 19:38:52 -04:00
|
|
|
};
|
2016-02-26 13:02:52 -05:00
|
|
|
|
2016-06-23 14:44:05 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
localization = new TestLocalization();
|
|
|
|
pipe = new I18nPluralPipe(localization);
|
|
|
|
});
|
2016-02-26 13:02:52 -05:00
|
|
|
|
2017-05-18 16:46:51 -04:00
|
|
|
it('should be marked as pure', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(new PipeResolver(new JitReflector()).resolve(I18nPluralPipe)!.pure).toEqual(true);
|
2017-05-18 16:46:51 -04:00
|
|
|
});
|
2016-02-26 13:02:52 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('transform', () => {
|
|
|
|
it('should return 0 text if value is 0', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const val = pipe.transform(0, mapping);
|
2016-02-26 13:02:52 -05:00
|
|
|
expect(val).toEqual('No messages.');
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should return 1 text if value is 1', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const val = pipe.transform(1, mapping);
|
2016-02-26 13:02:52 -05:00
|
|
|
expect(val).toEqual('One message.');
|
|
|
|
});
|
|
|
|
|
2016-06-23 14:44:05 -04:00
|
|
|
it('should return category messages', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const val = pipe.transform(4, mapping);
|
2016-06-23 14:44:05 -04:00
|
|
|
expect(val).toEqual('Many messages.');
|
2016-02-26 13:02:52 -05:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should interpolate the value into the text where indicated', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const val = pipe.transform(6, mapping);
|
2016-02-26 13:02:52 -05:00
|
|
|
expect(val).toEqual('There are 6 messages, that is 6.');
|
|
|
|
});
|
|
|
|
|
2016-06-23 14:44:05 -04:00
|
|
|
it('should use "" if value is undefined', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const val = pipe.transform(void (0) as any, mapping);
|
2016-06-23 14:44:05 -04:00
|
|
|
expect(val).toEqual('');
|
2016-02-26 13:02:52 -05:00
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('should not support bad arguments', () => {
|
|
|
|
expect(() => pipe.transform(0, <any>'hey')).toThrowError();
|
|
|
|
});
|
2016-02-26 13:02:52 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-06-23 14:44:05 -04:00
|
|
|
|
|
|
|
class TestLocalization extends NgLocalization {
|
2020-04-13 19:40:21 -04:00
|
|
|
getPluralCategory(value: number): string {
|
|
|
|
return value > 1 && value < 6 ? 'many' : 'other';
|
|
|
|
}
|
2016-06-23 14:44:05 -04:00
|
|
|
}
|