|
| 1 | +import {IntlCountryPipe} from './intl-country.pipe'; |
| 2 | +import {TestBed} from "@angular/core/testing"; |
| 3 | +import {INTL_LOCALES} from "../locale"; |
| 4 | +import {INTL_COUNTRY_PIPE_DEFAULT_OPTIONS} from "./intl-country-pipe-default-options"; |
| 5 | + |
| 6 | +describe('IntlCountryPipe', () => { |
| 7 | + let testUnit: IntlCountryPipe; |
| 8 | + |
| 9 | + describe('parsing', () => { |
| 10 | + beforeEach(() => { |
| 11 | + testUnit = new IntlCountryPipe('en-US'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should create an instance', () => { |
| 15 | + expect(testUnit).toBeTruthy(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should handle null values', () => { |
| 19 | + expect(testUnit.transform(null)).toBeNull(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should handle undefined values', () => { |
| 23 | + expect(testUnit.transform(undefined)).toBeNull(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should handle empty strings', () => { |
| 27 | + expect(testUnit.transform('')).toBeNull(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should transform numbers', () => { |
| 31 | + expect(testUnit.transform('US')).toEqual('United States'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should handle missing Intl.DisplayNames browser API', () => { |
| 35 | + // @ts-expect-error Intl APIs are not expected to be undefined |
| 36 | + spyOn(Intl, 'DisplayNames').and.returnValue(undefined); |
| 37 | + const consoleError = spyOn(console, 'error'); |
| 38 | + |
| 39 | + expect(testUnit.transform('US')).toBeNull(); |
| 40 | + expect(consoleError).toHaveBeenCalledTimes(1); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('internationalization', () => { |
| 45 | + it('should respect the set locale', () => { |
| 46 | + TestBed.configureTestingModule({ |
| 47 | + providers: [ |
| 48 | + IntlCountryPipe, |
| 49 | + { |
| 50 | + provide: INTL_LOCALES, |
| 51 | + useValue: 'de-DE', |
| 52 | + }, |
| 53 | + ], |
| 54 | + }); |
| 55 | + testUnit = TestBed.inject(IntlCountryPipe); |
| 56 | + |
| 57 | + expect(testUnit.transform('AT')).toEqual('Österreich'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should fall back to the browser default locale', () => { |
| 61 | + TestBed.configureTestingModule({providers: [IntlCountryPipe]}); |
| 62 | + |
| 63 | + const result1 = TestBed.inject(IntlCountryPipe).transform('US'); |
| 64 | + const result2 = new IntlCountryPipe(navigator.language).transform('US'); |
| 65 | + |
| 66 | + expect(result1).toEqual(result2); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('options', () => { |
| 71 | + it('should not override the type option', () => { |
| 72 | + TestBed.configureTestingModule({ |
| 73 | + providers: [ |
| 74 | + IntlCountryPipe, |
| 75 | + { |
| 76 | + provide: INTL_LOCALES, |
| 77 | + useValue: 'de-DE', |
| 78 | + }, |
| 79 | + { |
| 80 | + provide: INTL_COUNTRY_PIPE_DEFAULT_OPTIONS, |
| 81 | + useValue: { |
| 82 | + type: 'language', |
| 83 | + }, |
| 84 | + }, |
| 85 | + ], |
| 86 | + }); |
| 87 | + testUnit = TestBed.inject(IntlCountryPipe); |
| 88 | + |
| 89 | + expect(testUnit.transform('DE', {type: 'language'})).toEqual('Deutschland'); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should respect locale option', () => { |
| 94 | + TestBed.configureTestingModule({ |
| 95 | + providers: [ |
| 96 | + IntlCountryPipe, |
| 97 | + { |
| 98 | + provide: INTL_LOCALES, |
| 99 | + useValue: 'en-US', |
| 100 | + }, |
| 101 | + ], |
| 102 | + }); |
| 103 | + testUnit = TestBed.inject(IntlCountryPipe); |
| 104 | + |
| 105 | + expect(testUnit.transform('US', {locale: 'de-DE'})).toEqual('Vereinigte Staaten'); |
| 106 | + }); |
| 107 | +}); |
0 commit comments