|
| 1 | +import {IntlUnitPipe} from './intl-unit.pipe'; |
| 2 | +import {TestBed} from "@angular/core/testing"; |
| 3 | +import {INTL_LOCALES} from "../locale"; |
| 4 | +import {INTL_UNIT_PIPE_DEFAULT_OPTIONS} from "./intl-unit-pipe-default-options"; |
| 5 | + |
| 6 | +describe('IntlUnitPipe', () => { |
| 7 | + let testUnit: IntlUnitPipe; |
| 8 | + |
| 9 | + describe('parsing', () => { |
| 10 | + beforeEach(() => { |
| 11 | + testUnit = new IntlUnitPipe('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, undefined)).toBeNull(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should handle undefined values', () => { |
| 23 | + expect(testUnit.transform(undefined, undefined)).toBeNull(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should handle empty strings', () => { |
| 27 | + expect(testUnit.transform('', undefined)).toBeNull(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should transform numbers', () => { |
| 31 | + expect(testUnit.transform(1, 'hour')).toEqual('1 hr'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should transform strings', () => { |
| 35 | + expect(testUnit.transform('2', 'hour')).toEqual('2 hr'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should handle invalid strings', () => { |
| 39 | + expect(() => testUnit.transform('invalid number', undefined)).toThrow(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle missing Intl.NumberFormat browser API', () => { |
| 43 | + // @ts-expect-error Intl APIs are not expected to be undefined |
| 44 | + spyOn(Intl, 'NumberFormat').and.returnValue(undefined); |
| 45 | + const consoleError = spyOn(console, 'error'); |
| 46 | + expect(testUnit.transform('1', 'hour')).toBeNull(); |
| 47 | + |
| 48 | + expect(consoleError).toHaveBeenCalledTimes(1); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('internationalization', () => { |
| 53 | + it('should respect the set locale', () => { |
| 54 | + TestBed.configureTestingModule({ |
| 55 | + providers: [ |
| 56 | + IntlUnitPipe, |
| 57 | + { |
| 58 | + provide: INTL_LOCALES, |
| 59 | + useValue: 'de-DE', |
| 60 | + }, |
| 61 | + ], |
| 62 | + }); |
| 63 | + testUnit = TestBed.inject(IntlUnitPipe); |
| 64 | + |
| 65 | + expect(testUnit.transform(1, 'hour')).toEqual('1 Std.'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should fall back to the browser default locale', () => { |
| 69 | + TestBed.configureTestingModule({providers: [IntlUnitPipe]}); |
| 70 | + |
| 71 | + const result1 = TestBed.inject(IntlUnitPipe).transform(1, 'hour'); |
| 72 | + const result2 = new IntlUnitPipe(navigator.language).transform(1, 'hour'); |
| 73 | + |
| 74 | + expect(result1).toEqual(result2); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('options', () => { |
| 79 | + it('should respect the setting from default config', () => { |
| 80 | + TestBed.configureTestingModule({ |
| 81 | + providers: [ |
| 82 | + IntlUnitPipe, |
| 83 | + { |
| 84 | + provide: INTL_LOCALES, |
| 85 | + useValue: 'en-US', |
| 86 | + }, |
| 87 | + { |
| 88 | + provide: INTL_UNIT_PIPE_DEFAULT_OPTIONS, |
| 89 | + useValue: { |
| 90 | + unitDisplay: 'narrow', |
| 91 | + }, |
| 92 | + }, |
| 93 | + ], |
| 94 | + }); |
| 95 | + testUnit = TestBed.inject(IntlUnitPipe); |
| 96 | + |
| 97 | + expect(testUnit.transform(1, 'liter')).toEqual('1L'); |
| 98 | + |
| 99 | + }); |
| 100 | + |
| 101 | + it('should give the user options a higher priority', () => { |
| 102 | + TestBed.configureTestingModule({ |
| 103 | + providers: [ |
| 104 | + IntlUnitPipe, |
| 105 | + { |
| 106 | + provide: INTL_LOCALES, |
| 107 | + useValue: 'en-US', |
| 108 | + }, |
| 109 | + { |
| 110 | + provide: INTL_UNIT_PIPE_DEFAULT_OPTIONS, |
| 111 | + useValue: { |
| 112 | + unitDisplay: 'short', |
| 113 | + }, |
| 114 | + }, |
| 115 | + ], |
| 116 | + }); |
| 117 | + testUnit = TestBed.inject(IntlUnitPipe); |
| 118 | + |
| 119 | + expect(testUnit.transform(1, 'liter', {unitDisplay: 'narrow'})).toEqual('1L'); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should respect locale option', () => { |
| 124 | + TestBed.configureTestingModule({ |
| 125 | + providers: [ |
| 126 | + IntlUnitPipe, |
| 127 | + { |
| 128 | + provide: INTL_LOCALES, |
| 129 | + useValue: 'en-US', |
| 130 | + }, |
| 131 | + ], |
| 132 | + }); |
| 133 | + testUnit = TestBed.inject(IntlUnitPipe); |
| 134 | + |
| 135 | + expect(testUnit.transform(1, 'hour', {locale: 'de-DE'})).toEqual('1 Std.'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should not override the style option', () => { |
| 139 | + TestBed.configureTestingModule({ |
| 140 | + providers: [ |
| 141 | + IntlUnitPipe, |
| 142 | + { |
| 143 | + provide: INTL_LOCALES, |
| 144 | + useValue: 'en-US', |
| 145 | + }, |
| 146 | + { |
| 147 | + provide: INTL_UNIT_PIPE_DEFAULT_OPTIONS, |
| 148 | + useValue: { |
| 149 | + style: 'decimal', |
| 150 | + }, |
| 151 | + }, |
| 152 | + ], |
| 153 | + }); |
| 154 | + testUnit = TestBed.inject(IntlUnitPipe); |
| 155 | + |
| 156 | + expect(testUnit.transform(1, 'hour', {style: 'decimal'})).toEqual('1 hr'); |
| 157 | + }); |
| 158 | +}); |
0 commit comments