Files
angular__angular/packages/common/test/pipes/i18n_select_pipe_spec.ts
Kristiyan Kostadinov c07805612f test(core): clean up unnecessary nesting in old tests (#52239)
A lot of our tests are wrapped in `{}` which serves no purpose, aside from increasing the nesting level and, in some cases, causing confusion. The braces appear to be a leftover from a time when all tests were wrapped in a `function main() {}`. The function declaration was removed in #21053, but the braces remained, presumably because it was easier to search&replace for `function main()`, but not to remove the braces at the same time.

PR Close #52239
2023-10-19 09:26:15 -07:00

61 lines
1.9 KiB
TypeScript

/**
* @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 {I18nSelectPipe} from '@angular/common';
import {Component} from '@angular/core';
import {TestBed} from '@angular/core/testing';
describe('I18nSelectPipe', () => {
const pipe: I18nSelectPipe = new I18nSelectPipe();
const mapping = {'male': 'Invite him.', 'female': 'Invite her.', 'other': 'Invite them.'};
describe('transform', () => {
it('should return the "male" text if value is "male"', () => {
const val = pipe.transform('male', mapping);
expect(val).toEqual('Invite him.');
});
it('should return the "female" text if value is "female"', () => {
const val = pipe.transform('female', mapping);
expect(val).toEqual('Invite her.');
});
it('should return the "other" text if value is neither "male" nor "female"', () => {
expect(pipe.transform('Anything else', mapping)).toEqual('Invite them.');
});
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 throw on bad arguments', () => {
expect(() => pipe.transform('male', 'hey' as any)).toThrowError();
});
it('should be available as a standalone pipe', () => {
@Component({
selector: 'test-component',
imports: [I18nSelectPipe],
template: '{{ value | i18nSelect:mapping }}',
standalone: true,
})
class TestComponent {
value = 'other';
mapping = mapping;
}
const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
const content = fixture.nativeElement.textContent;
expect(content).toBe('Invite them.');
});
});
});