|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { INTL_LOCALES } from '../locale'; |
| 3 | +import { INTL_DURATION_PIPE_DEFAULT_OPTIONS } from './intl-duration-pipe-default-options'; |
| 4 | +import { IntlDurationPipe } from './intl-duration.pipe'; |
| 5 | + |
| 6 | +describe('IntlDurationPipe', () => { |
| 7 | + let testUnit: IntlDurationPipe; |
| 8 | + |
| 9 | + describe('parsing', () => { |
| 10 | + beforeEach(() => { |
| 11 | + TestBed.runInInjectionContext(() => { |
| 12 | + testUnit = new IntlDurationPipe(); |
| 13 | + Object.defineProperty(testUnit, 'locale', { value: 'en-US' }); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should create an instance', () => { |
| 18 | + expect(testUnit).toBeTruthy(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should handle null values', () => { |
| 22 | + expect(testUnit.transform(null)).toBeNull(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should handle undefined values', () => { |
| 26 | + expect(testUnit.transform(undefined)).toBeNull(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should transform durations', () => { |
| 30 | + expect( |
| 31 | + testUnit.transform({ |
| 32 | + years: 2, |
| 33 | + months: 11, |
| 34 | + weeks: 2, |
| 35 | + days: 1, |
| 36 | + hours: 0, |
| 37 | + minutes: 55, |
| 38 | + seconds: 19, |
| 39 | + milliseconds: 940, |
| 40 | + microseconds: 10, |
| 41 | + nanoseconds: 3, |
| 42 | + }), |
| 43 | + ).toEqual( |
| 44 | + '2 yrs, 11 mths, 2 wks, 1 day, 55 min, 19 sec, 940 ms, 10 μs, 3 ns', |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should handle missing Intl.NumberFormat browser API', () => { |
| 49 | + // @ts-expect-error Intl APIs are not expected to be undefined |
| 50 | + spyOn(Intl, 'DurationFormat').and.returnValue(undefined); |
| 51 | + const consoleError = spyOn(console, 'error'); |
| 52 | + expect(testUnit.transform({ years: 1 })).toBeNull(); |
| 53 | + |
| 54 | + expect(consoleError).toHaveBeenCalledTimes(1); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('internationalization', () => { |
| 59 | + it('should respect the set locale', () => { |
| 60 | + TestBed.configureTestingModule({ |
| 61 | + providers: [ |
| 62 | + { |
| 63 | + provide: INTL_LOCALES, |
| 64 | + useValue: 'de-DE', |
| 65 | + }, |
| 66 | + ], |
| 67 | + }); |
| 68 | + TestBed.runInInjectionContext(() => (testUnit = new IntlDurationPipe())); |
| 69 | + |
| 70 | + expect(testUnit.transform({ years: 1 })).toEqual('1 J'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should fall back to the browser default locale', () => { |
| 74 | + let defaultLanguageTestUnit!: IntlDurationPipe; |
| 75 | + let browserLanguageTestUnit!: IntlDurationPipe; |
| 76 | + |
| 77 | + TestBed.runInInjectionContext(() => { |
| 78 | + defaultLanguageTestUnit = new IntlDurationPipe(); |
| 79 | + browserLanguageTestUnit = new IntlDurationPipe(); |
| 80 | + Object.defineProperty(browserLanguageTestUnit, 'locale', { |
| 81 | + value: undefined, |
| 82 | + }); |
| 83 | + Object.defineProperty(defaultLanguageTestUnit, 'locale', { |
| 84 | + value: navigator.language, |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + const result1 = browserLanguageTestUnit.transform({ years: 1 }); |
| 89 | + const result2 = defaultLanguageTestUnit.transform({ years: 1 }); |
| 90 | + |
| 91 | + expect(result1).toEqual(result2); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('options', () => { |
| 96 | + it('should respect the setting from default config', () => { |
| 97 | + TestBed.configureTestingModule({ |
| 98 | + providers: [ |
| 99 | + { |
| 100 | + provide: INTL_LOCALES, |
| 101 | + useValue: 'en-US', |
| 102 | + }, |
| 103 | + { |
| 104 | + provide: INTL_DURATION_PIPE_DEFAULT_OPTIONS, |
| 105 | + useValue: { |
| 106 | + style: 'long', |
| 107 | + }, |
| 108 | + }, |
| 109 | + ], |
| 110 | + }); |
| 111 | + TestBed.runInInjectionContext(() => (testUnit = new IntlDurationPipe())); |
| 112 | + |
| 113 | + expect(testUnit.transform({ years: 1 })).toEqual('1 year'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should give the user options a higher priority', () => { |
| 117 | + TestBed.configureTestingModule({ |
| 118 | + providers: [ |
| 119 | + IntlDurationPipe, |
| 120 | + { |
| 121 | + provide: INTL_LOCALES, |
| 122 | + useValue: 'en-US', |
| 123 | + }, |
| 124 | + { |
| 125 | + provide: INTL_DURATION_PIPE_DEFAULT_OPTIONS, |
| 126 | + useValue: { |
| 127 | + style: 'long', |
| 128 | + }, |
| 129 | + }, |
| 130 | + ], |
| 131 | + }); |
| 132 | + TestBed.runInInjectionContext(() => (testUnit = new IntlDurationPipe())); |
| 133 | + |
| 134 | + expect(testUnit.transform({ years: 1 }, { style: 'narrow' })).toEqual( |
| 135 | + '1y', |
| 136 | + ); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should respect locale option', () => { |
| 141 | + TestBed.configureTestingModule({ |
| 142 | + providers: [ |
| 143 | + { |
| 144 | + provide: INTL_LOCALES, |
| 145 | + useValue: 'en-US', |
| 146 | + }, |
| 147 | + ], |
| 148 | + }); |
| 149 | + TestBed.runInInjectionContext(() => (testUnit = new IntlDurationPipe())); |
| 150 | + |
| 151 | + expect(testUnit.transform({ years: 1 }, { locale: 'de-DE' })).toEqual( |
| 152 | + '1 J', |
| 153 | + ); |
| 154 | + }); |
| 155 | +}); |
0 commit comments