fix(common): I18nSelectPipe selects other case on default

This commit is contained in:
Victor Berchet 2016-11-04 14:49:53 -07:00 committed by vikerman
parent 7694f974af
commit 4708b248d5
2 changed files with 27 additions and 24 deletions

View File

@ -7,7 +7,6 @@
*/ */
import {Pipe, PipeTransform} from '@angular/core'; import {Pipe, PipeTransform} from '@angular/core';
import {isBlank} from '../facade/lang';
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error'; import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
/** /**
@ -16,9 +15,10 @@ import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
* @howToUse `expression | i18nSelect:mapping` * @howToUse `expression | i18nSelect:mapping`
* @description * @description
* *
* Where: * Where `mapping` is an object that indicates the text that should be displayed
* - `mapping`: is an object that indicates the text that should be displayed
* for different values of the provided `expression`. * for different values of the provided `expression`.
* If none of the keys of the mapping match the value of the `expression`, then the content
* of the `other` key is returned when present, otherwise an empty string is returned.
* *
* ## Example * ## Example
* *
@ -29,12 +29,20 @@ import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
@Pipe({name: 'i18nSelect', pure: true}) @Pipe({name: 'i18nSelect', pure: true})
export class I18nSelectPipe implements PipeTransform { export class I18nSelectPipe implements PipeTransform {
transform(value: string, mapping: {[key: string]: string}): string { transform(value: string, mapping: {[key: string]: string}): string {
if (isBlank(value)) return ''; if (value == null) return '';
if (typeof mapping !== 'object' || mapping === null) { if (typeof mapping !== 'object' || typeof value !== 'string') {
throw new InvalidPipeArgumentError(I18nSelectPipe, mapping); throw new InvalidPipeArgumentError(I18nSelectPipe, mapping);
} }
return mapping[value] || ''; if (mapping.hasOwnProperty(value)) {
return mapping[value];
}
if (mapping.hasOwnProperty('other')) {
return mapping['other'];
}
return '';
} }
} }

View File

@ -8,40 +8,35 @@
import {I18nSelectPipe} from '@angular/common'; import {I18nSelectPipe} from '@angular/common';
import {PipeResolver} from '@angular/compiler/src/pipe_resolver'; import {PipeResolver} from '@angular/compiler/src/pipe_resolver';
import {beforeEach, describe, expect, it} from '@angular/core/testing/testing_internal';
export function main() { export function main() {
describe('I18nSelectPipe', () => { describe('I18nSelectPipe', () => {
var pipe: I18nSelectPipe; const pipe: I18nSelectPipe = new I18nSelectPipe();
var mapping = {'male': 'Invite him.', 'female': 'Invite her.', 'other': 'Invite them.'}; const mapping = {'male': 'Invite him.', 'female': 'Invite her.', 'other': 'Invite them.'};
beforeEach(() => { pipe = new I18nSelectPipe(); });
it('should be marked as pure', it('should be marked as pure',
() => { expect(new PipeResolver().resolve(I18nSelectPipe).pure).toEqual(true); }); () => { expect(new PipeResolver().resolve(I18nSelectPipe).pure).toEqual(true); });
describe('transform', () => { describe('transform', () => {
it('should return male text if value is male', () => { it('should return the "male" text if value is "male"', () => {
var val = pipe.transform('male', mapping); const val = pipe.transform('male', mapping);
expect(val).toEqual('Invite him.'); expect(val).toEqual('Invite him.');
}); });
it('should return female text if value is female', () => { it('should return the "female" text if value is "female"', () => {
var val = pipe.transform('female', mapping); const val = pipe.transform('female', mapping);
expect(val).toEqual('Invite her.'); expect(val).toEqual('Invite her.');
}); });
it('should return "" if value is anything other than male or female', () => { it('should return the "other" text if value is neither "male" nor "female"',
var val = pipe.transform('Anything else', mapping); () => { expect(pipe.transform('Anything else', mapping)).toEqual('Invite them.'); });
expect(val).toEqual('');
it('should return an empty text if value is null or undefined', () => {
expect(pipe.transform(null, mapping)).toEqual('');
expect(pipe.transform(void 0, mapping)).toEqual('');
}); });
it('should use "" if value is undefined', () => { it('should throw on bad arguments',
var val = pipe.transform(void(0), mapping);
expect(val).toEqual('');
});
it('should not support bad arguments',
() => { expect(() => pipe.transform('male', <any>'hey')).toThrowError(); }); () => { expect(() => pipe.transform('male', <any>'hey')).toThrowError(); });
}); });