From 6d0039712f622a29a7adb4c9cd6e4e3abed9ec22 Mon Sep 17 00:00:00 2001 From: Daniel Kimmich Date: Wed, 27 Nov 2024 22:10:59 +0100 Subject: [PATCH] refactor: inject migration --- .../src/lib/country/intl-country.pipe.spec.ts | 32 +++++++++---- .../src/lib/country/intl-country.pipe.ts | 15 +++--- .../lib/currency/intl-currency.pipe.spec.ts | 44 ++++++++++-------- .../src/lib/currency/intl-currency.pipe.ts | 15 +++--- .../src/lib/date/intl-date.pipe.spec.ts | 37 +++++++++------ .../src/lib/date/intl-date.pipe.ts | 15 +++--- .../src/lib/decimal/intl-decimal.pipe.spec.ts | 40 +++++++++------- .../src/lib/decimal/intl-decimal.pipe.ts | 15 +++--- .../lib/language/intl-language.pipe.spec.ts | 40 +++++++++------- .../src/lib/language/intl-language.pipe.ts | 15 +++--- .../src/lib/list/intl-list.pipe.spec.ts | 32 ++++++++----- .../src/lib/list/intl-list.pipe.ts | 15 +++--- .../src/lib/percent/intl-percent.pipe.spec.ts | 38 +++++++++------ .../src/lib/percent/intl-percent.pipe.ts | 15 +++--- .../relative-time/relative-time.pipe.spec.ts | 46 +++++++++++++------ .../lib/relative-time/relative-time.pipe.ts | 23 ++++------ .../src/lib/unit/intl-unit.pipe.spec.ts | 38 +++++++++------ .../src/lib/unit/intl-unit.pipe.ts | 15 +++--- 18 files changed, 273 insertions(+), 217 deletions(-) diff --git a/projects/angular-ecmascript-intl/src/lib/country/intl-country.pipe.spec.ts b/projects/angular-ecmascript-intl/src/lib/country/intl-country.pipe.spec.ts index 80c7482f..3d7353d2 100644 --- a/projects/angular-ecmascript-intl/src/lib/country/intl-country.pipe.spec.ts +++ b/projects/angular-ecmascript-intl/src/lib/country/intl-country.pipe.spec.ts @@ -8,7 +8,10 @@ describe('IntlCountryPipe', () => { describe('parsing', () => { beforeEach(() => { - testUnit = new IntlCountryPipe('en-US'); + TestBed.runInInjectionContext(() => { + testUnit = new IntlCountryPipe(); + Object.defineProperty(testUnit, 'locale', { value: 'en-US' }); + }); }); it('should create an instance', () => { @@ -49,23 +52,34 @@ describe('IntlCountryPipe', () => { it('should respect the set locale', () => { TestBed.configureTestingModule({ providers: [ - IntlCountryPipe, { provide: INTL_LOCALES, useValue: 'de-DE', }, ], }); - testUnit = TestBed.inject(IntlCountryPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlCountryPipe())); expect(testUnit.transform('AT')).toEqual('Österreich'); }); it('should fall back to the browser default locale', () => { - TestBed.configureTestingModule({ providers: [IntlCountryPipe] }); + let defaultLanguageTestUnit!: IntlCountryPipe; + let browserLanguageTestUnit!: IntlCountryPipe; + + TestBed.runInInjectionContext(() => { + defaultLanguageTestUnit = new IntlCountryPipe(); + browserLanguageTestUnit = new IntlCountryPipe(); + Object.defineProperty(browserLanguageTestUnit, 'locale', { + value: undefined, + }); + Object.defineProperty(defaultLanguageTestUnit, 'locale', { + value: navigator.language, + }); + }); - const result1 = TestBed.inject(IntlCountryPipe).transform('US'); - const result2 = new IntlCountryPipe(navigator.language).transform('US'); + const result1 = browserLanguageTestUnit.transform('US'); + const result2 = defaultLanguageTestUnit.transform('US'); expect(result1).toEqual(result2); }); @@ -75,7 +89,6 @@ describe('IntlCountryPipe', () => { it('should not override the type option', () => { TestBed.configureTestingModule({ providers: [ - IntlCountryPipe, { provide: INTL_LOCALES, useValue: 'de-DE', @@ -88,7 +101,7 @@ describe('IntlCountryPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlCountryPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlCountryPipe())); expect(testUnit.transform('DE', { type: 'language' })).toEqual( 'Deutschland', @@ -99,14 +112,13 @@ describe('IntlCountryPipe', () => { it('should respect locale option', () => { TestBed.configureTestingModule({ providers: [ - IntlCountryPipe, { provide: INTL_LOCALES, useValue: 'en-US', }, ], }); - testUnit = TestBed.inject(IntlCountryPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlCountryPipe())); expect(testUnit.transform('US', { locale: 'de-DE' })).toEqual( 'Vereinigte Staaten', diff --git a/projects/angular-ecmascript-intl/src/lib/country/intl-country.pipe.ts b/projects/angular-ecmascript-intl/src/lib/country/intl-country.pipe.ts index b6826f13..eaac54ef 100644 --- a/projects/angular-ecmascript-intl/src/lib/country/intl-country.pipe.ts +++ b/projects/angular-ecmascript-intl/src/lib/country/intl-country.pipe.ts @@ -1,4 +1,4 @@ -import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; +import { Pipe, PipeTransform, inject } from '@angular/core'; import { IntlPipeOptions } from '../intl-pipe-options'; import { INTL_LOCALES } from '../locale'; import { INTL_COUNTRY_PIPE_DEFAULT_OPTIONS } from './intl-country-pipe-default-options'; @@ -14,14 +14,11 @@ export type IntlCountryPipeOptions = Omit< standalone: true, }) export class IntlCountryPipe implements PipeTransform { - constructor( - @Optional() - @Inject(INTL_LOCALES) - readonly locale?: string | string[] | null, - @Optional() - @Inject(INTL_COUNTRY_PIPE_DEFAULT_OPTIONS) - readonly defaultOptions?: Omit | null, - ) {} + private readonly locale? = inject(INTL_LOCALES, { optional: true }); + private readonly defaultOptions? = inject | null>(INTL_COUNTRY_PIPE_DEFAULT_OPTIONS, { optional: true }); transform( value: string | null | undefined, diff --git a/projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.spec.ts b/projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.spec.ts index d3765e2b..c405cf06 100644 --- a/projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.spec.ts +++ b/projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.spec.ts @@ -8,7 +8,10 @@ describe('IntlCurrencyPipe', () => { describe('parsing', () => { beforeEach(() => { - testUnit = new IntlCurrencyPipe('en-US'); + TestBed.runInInjectionContext(() => { + testUnit = new IntlCurrencyPipe(); + Object.defineProperty(testUnit, 'locale', { value: 'en-US' }); + }); }); it('should create an instance', () => { @@ -53,29 +56,34 @@ describe('IntlCurrencyPipe', () => { it('should respect the set locale', () => { TestBed.configureTestingModule({ providers: [ - IntlCurrencyPipe, { provide: INTL_LOCALES, useValue: 'de-DE', }, ], }); - testUnit = TestBed.inject(IntlCurrencyPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe())); expect(testUnit.transform(1024.2249, 'EUR')).toEqual('1.024,22\xa0€'); }); it('should fall back to the browser default locale', () => { - TestBed.configureTestingModule({ providers: [IntlCurrencyPipe] }); + let defaultLanguageTestUnit!: IntlCurrencyPipe; + let browserLanguageTestUnit!: IntlCurrencyPipe; + + TestBed.runInInjectionContext(() => { + defaultLanguageTestUnit = new IntlCurrencyPipe(); + browserLanguageTestUnit = new IntlCurrencyPipe(); + Object.defineProperty(browserLanguageTestUnit, 'locale', { + value: undefined, + }); + Object.defineProperty(defaultLanguageTestUnit, 'locale', { + value: navigator.language, + }); + }); - const result1 = TestBed.inject(IntlCurrencyPipe).transform( - 1024.2249, - 'EUR', - ); - const result2 = new IntlCurrencyPipe(navigator.language).transform( - 1024.2249, - 'EUR', - ); + const result1 = browserLanguageTestUnit.transform(1024.2249, 'EUR'); + const result2 = defaultLanguageTestUnit.transform(1024.2249, 'EUR'); expect(result1).toEqual(result2); }); @@ -85,7 +93,6 @@ describe('IntlCurrencyPipe', () => { it('should respect the setting from default config', () => { TestBed.configureTestingModule({ providers: [ - IntlCurrencyPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -98,7 +105,7 @@ describe('IntlCurrencyPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlCurrencyPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe())); expect(testUnit.transform(1, 'USD')).toEqual('+$1.00'); }); @@ -106,7 +113,6 @@ describe('IntlCurrencyPipe', () => { it('should give the user options a higher priority', () => { TestBed.configureTestingModule({ providers: [ - IntlCurrencyPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -119,7 +125,7 @@ describe('IntlCurrencyPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlCurrencyPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe())); expect(testUnit.transform(1, 'USD', { signDisplay: 'never' })).toEqual( '$1.00', @@ -130,14 +136,13 @@ describe('IntlCurrencyPipe', () => { it('should respect locale option', () => { TestBed.configureTestingModule({ providers: [ - IntlCurrencyPipe, { provide: INTL_LOCALES, useValue: 'en-US', }, ], }); - testUnit = TestBed.inject(IntlCurrencyPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe())); expect(testUnit.transform(1024, 'USD', { locale: 'de-DE' })).toEqual( '1.024,00\xa0$', @@ -147,7 +152,6 @@ describe('IntlCurrencyPipe', () => { it('should not override the style option', () => { TestBed.configureTestingModule({ providers: [ - IntlCurrencyPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -160,7 +164,7 @@ describe('IntlCurrencyPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlCurrencyPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe())); expect(testUnit.transform(1, 'USD', { style: 'percent' })).toEqual('$1.00'); }); diff --git a/projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.ts b/projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.ts index 1fab8193..2ee878af 100644 --- a/projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.ts +++ b/projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.ts @@ -1,4 +1,4 @@ -import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; +import { Pipe, PipeTransform, inject } from '@angular/core'; import { IntlPipeOptions } from '../intl-pipe-options'; import { INTL_LOCALES } from '../locale'; import { getNumericValue } from '../utils/number-utils'; @@ -16,14 +16,11 @@ export type IntlCurrencyPipeOptions = Omit< standalone: true, }) export class IntlCurrencyPipe implements PipeTransform { - constructor( - @Optional() - @Inject(INTL_LOCALES) - readonly locale?: string | string[] | null, - @Optional() - @Inject(INTL_CURRENCY_PIPE_DEFAULT_OPTIONS) - readonly defaultOptions?: Omit | null, - ) {} + private readonly locale? = inject(INTL_LOCALES, { optional: true }); + private readonly defaultOptions? = inject | null>(INTL_CURRENCY_PIPE_DEFAULT_OPTIONS, { optional: true }); transform( value: number | string | null | undefined, diff --git a/projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.spec.ts b/projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.spec.ts index 955266f0..fc3c3f6c 100644 --- a/projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.spec.ts +++ b/projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.spec.ts @@ -8,7 +8,10 @@ describe('DatePipe', () => { describe('date parsing', () => { beforeEach(() => { - testUnit = new IntlDatePipe('en-US'); + TestBed.runInInjectionContext(() => { + testUnit = new IntlDatePipe(); + Object.defineProperty(testUnit, 'locale', { value: 'en-US' }); + }); }); it('should create an instance', () => { @@ -69,25 +72,34 @@ describe('DatePipe', () => { it('should respect the set locale', () => { TestBed.configureTestingModule({ providers: [ - IntlDatePipe, { provide: INTL_LOCALES, useValue: 'de-DE', }, ], }); - testUnit = TestBed.inject(IntlDatePipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlDatePipe())); expect(testUnit.transform('2023-02-19')).toEqual('19.2.2023'); }); it('should fall back to the browser default locale', () => { - TestBed.configureTestingModule({ providers: [IntlDatePipe] }); + let defaultLanguageTestUnit!: IntlDatePipe; + let browserLanguageTestUnit!: IntlDatePipe; + + TestBed.runInInjectionContext(() => { + defaultLanguageTestUnit = new IntlDatePipe(); + browserLanguageTestUnit = new IntlDatePipe(); + Object.defineProperty(browserLanguageTestUnit, 'locale', { + value: undefined, + }); + Object.defineProperty(defaultLanguageTestUnit, 'locale', { + value: navigator.language, + }); + }); - const result1 = TestBed.inject(IntlDatePipe).transform('2023-02-19'); - const result2 = new IntlDatePipe(navigator.language).transform( - '2023-02-19', - ); + const result1 = browserLanguageTestUnit.transform('2023-02-19'); + const result2 = defaultLanguageTestUnit.transform('2023-02-19'); expect(result1).toEqual(result2); }); @@ -97,7 +109,6 @@ describe('DatePipe', () => { it('should respect the setting from default config', () => { TestBed.configureTestingModule({ providers: [ - IntlDatePipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -110,7 +121,7 @@ describe('DatePipe', () => { }, ], }); - testUnit = TestBed.inject(IntlDatePipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlDatePipe())); expect(testUnit.transform('2023-02-19')).toEqual('Feb 19, 2023'); }); @@ -118,7 +129,6 @@ describe('DatePipe', () => { it('should give the user options a higher priority', () => { TestBed.configureTestingModule({ providers: [ - IntlDatePipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -131,7 +141,7 @@ describe('DatePipe', () => { }, ], }); - testUnit = TestBed.inject(IntlDatePipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlDatePipe())); expect(testUnit.transform('2023-02-19', { dateStyle: 'medium' })).toEqual( 'Feb 19, 2023', @@ -142,14 +152,13 @@ describe('DatePipe', () => { it('should respect locale option', () => { TestBed.configureTestingModule({ providers: [ - IntlDatePipe, { provide: INTL_LOCALES, useValue: 'en-US', }, ], }); - testUnit = TestBed.inject(IntlDatePipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlDatePipe())); expect(testUnit.transform('2023-02-19', { locale: 'de-DE' })).toEqual( '19.2.2023', diff --git a/projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.ts b/projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.ts index a8d03637..8b26aa2f 100644 --- a/projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.ts +++ b/projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.ts @@ -1,4 +1,4 @@ -import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; +import { Pipe, PipeTransform, inject } from '@angular/core'; import { IntlPipeOptions } from '../intl-pipe-options'; import { INTL_LOCALES } from '../locale'; import { INTL_DATE_PIPE_DEFAULT_OPTIONS } from './intl-date-pipe-default-options'; @@ -11,14 +11,11 @@ export type IntlDatePipeOptions = Partial & standalone: true, }) export class IntlDatePipe implements PipeTransform { - constructor( - @Optional() - @Inject(INTL_LOCALES) - readonly locale?: string | string[] | null, - @Optional() - @Inject(INTL_DATE_PIPE_DEFAULT_OPTIONS) - readonly defaultOptions?: Omit | null, - ) {} + private readonly locale? = inject(INTL_LOCALES, { optional: true }); + private readonly defaultOptions? = inject | null>(INTL_DATE_PIPE_DEFAULT_OPTIONS, { optional: true }); transform( value: string | number | Date | null | undefined, diff --git a/projects/angular-ecmascript-intl/src/lib/decimal/intl-decimal.pipe.spec.ts b/projects/angular-ecmascript-intl/src/lib/decimal/intl-decimal.pipe.spec.ts index 748f4ba5..7e51e66c 100644 --- a/projects/angular-ecmascript-intl/src/lib/decimal/intl-decimal.pipe.spec.ts +++ b/projects/angular-ecmascript-intl/src/lib/decimal/intl-decimal.pipe.spec.ts @@ -8,7 +8,10 @@ describe('IntlDecimalPipe', () => { describe('parsing', () => { beforeEach(() => { - testUnit = new IntlDecimalPipe('en-US'); + TestBed.runInInjectionContext(() => { + testUnit = new IntlDecimalPipe(); + Object.defineProperty(testUnit, 'locale', { value: 'en-US' }); + }); }); it('should create an instance', () => { @@ -53,25 +56,34 @@ describe('IntlDecimalPipe', () => { it('should respect the set locale', () => { TestBed.configureTestingModule({ providers: [ - IntlDecimalPipe, { provide: INTL_LOCALES, useValue: 'de-DE', }, ], }); - testUnit = TestBed.inject(IntlDecimalPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlDecimalPipe())); expect(testUnit.transform(1024.2249)).toEqual('1.024,225'); }); it('should fall back to the browser default locale', () => { - TestBed.configureTestingModule({ providers: [IntlDecimalPipe] }); + let defaultLanguageTestUnit!: IntlDecimalPipe; + let browserLanguageTestUnit!: IntlDecimalPipe; + + TestBed.runInInjectionContext(() => { + defaultLanguageTestUnit = new IntlDecimalPipe(); + browserLanguageTestUnit = new IntlDecimalPipe(); + Object.defineProperty(browserLanguageTestUnit, 'locale', { + value: undefined, + }); + Object.defineProperty(defaultLanguageTestUnit, 'locale', { + value: navigator.language, + }); + }); - const result1 = TestBed.inject(IntlDecimalPipe).transform(1024.2249); - const result2 = new IntlDecimalPipe(navigator.language).transform( - 1024.2249, - ); + const result1 = browserLanguageTestUnit.transform(1024.2249); + const result2 = defaultLanguageTestUnit.transform(1024.2249); expect(result1).toEqual(result2); }); @@ -81,7 +93,6 @@ describe('IntlDecimalPipe', () => { it('should respect the setting from default config', () => { TestBed.configureTestingModule({ providers: [ - IntlDecimalPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -94,7 +105,7 @@ describe('IntlDecimalPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlDecimalPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlDecimalPipe())); expect(testUnit.transform(1)).toEqual('+1'); }); @@ -102,7 +113,6 @@ describe('IntlDecimalPipe', () => { it('should give the user options a higher priority', () => { TestBed.configureTestingModule({ providers: [ - IntlDecimalPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -115,7 +125,7 @@ describe('IntlDecimalPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlDecimalPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlDecimalPipe())); expect(testUnit.transform(1, { signDisplay: 'never' })).toEqual('1'); }); @@ -124,14 +134,13 @@ describe('IntlDecimalPipe', () => { it('should respect locale option', () => { TestBed.configureTestingModule({ providers: [ - IntlDecimalPipe, { provide: INTL_LOCALES, useValue: 'en-US', }, ], }); - testUnit = TestBed.inject(IntlDecimalPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlDecimalPipe())); expect(testUnit.transform(1024, { locale: 'de-DE' })).toEqual('1.024'); }); @@ -139,7 +148,6 @@ describe('IntlDecimalPipe', () => { it('should not override the style option', () => { TestBed.configureTestingModule({ providers: [ - IntlDecimalPipe, { provide: INTL_LOCALES, useValue: 'de-DE', @@ -152,7 +160,7 @@ describe('IntlDecimalPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlDecimalPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlDecimalPipe())); expect(testUnit.transform(1, { style: 'percent' })).toEqual('1'); }); diff --git a/projects/angular-ecmascript-intl/src/lib/decimal/intl-decimal.pipe.ts b/projects/angular-ecmascript-intl/src/lib/decimal/intl-decimal.pipe.ts index a01d66d4..a6450278 100644 --- a/projects/angular-ecmascript-intl/src/lib/decimal/intl-decimal.pipe.ts +++ b/projects/angular-ecmascript-intl/src/lib/decimal/intl-decimal.pipe.ts @@ -1,4 +1,4 @@ -import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; +import { Pipe, PipeTransform, inject } from '@angular/core'; import { IntlPipeOptions } from '../intl-pipe-options'; import { INTL_LOCALES } from '../locale'; import { getNumericValue } from '../utils/number-utils'; @@ -21,14 +21,11 @@ export type IntlDecimalPipeOptions = Omit< standalone: true, }) export class IntlDecimalPipe implements PipeTransform { - constructor( - @Optional() - @Inject(INTL_LOCALES) - readonly locale?: string | string[] | null, - @Optional() - @Inject(INTL_DECIMAL_PIPE_DEFAULT_OPTIONS) - readonly defaultOptions?: Omit | null, - ) {} + private readonly locale? = inject(INTL_LOCALES, { optional: true }); + private readonly defaultOptions? = inject | null>(INTL_DECIMAL_PIPE_DEFAULT_OPTIONS, { optional: true }); transform( value: number | string | null | undefined, diff --git a/projects/angular-ecmascript-intl/src/lib/language/intl-language.pipe.spec.ts b/projects/angular-ecmascript-intl/src/lib/language/intl-language.pipe.spec.ts index 48afefce..52d03a3f 100644 --- a/projects/angular-ecmascript-intl/src/lib/language/intl-language.pipe.spec.ts +++ b/projects/angular-ecmascript-intl/src/lib/language/intl-language.pipe.spec.ts @@ -8,7 +8,10 @@ describe('IntlLanguagePipe', () => { describe('parsing', () => { beforeEach(() => { - testUnit = new IntlLanguagePipe('en-US'); + TestBed.runInInjectionContext(() => { + testUnit = new IntlLanguagePipe(); + Object.defineProperty(testUnit, 'locale', { value: 'en-US' }); + }); }); it('should create an instance', () => { @@ -49,25 +52,34 @@ describe('IntlLanguagePipe', () => { it('should respect the set locale', () => { TestBed.configureTestingModule({ providers: [ - IntlLanguagePipe, { provide: INTL_LOCALES, useValue: 'de-DE', }, ], }); - testUnit = TestBed.inject(IntlLanguagePipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlLanguagePipe())); expect(testUnit.transform('de-AT')).toEqual('Österreichisches Deutsch'); }); it('should fall back to the browser default locale', () => { - TestBed.configureTestingModule({ providers: [IntlLanguagePipe] }); + let defaultLanguageTestUnit!: IntlLanguagePipe; + let browserLanguageTestUnit!: IntlLanguagePipe; + + TestBed.runInInjectionContext(() => { + defaultLanguageTestUnit = new IntlLanguagePipe(); + browserLanguageTestUnit = new IntlLanguagePipe(); + Object.defineProperty(browserLanguageTestUnit, 'locale', { + value: undefined, + }); + Object.defineProperty(defaultLanguageTestUnit, 'locale', { + value: navigator.language, + }); + }); - const result1 = TestBed.inject(IntlLanguagePipe).transform('en-US'); - const result2 = new IntlLanguagePipe(navigator.language).transform( - 'en-US', - ); + const result1 = browserLanguageTestUnit.transform('en-US'); + const result2 = defaultLanguageTestUnit.transform('en-US'); expect(result1).toEqual(result2); }); @@ -77,7 +89,6 @@ describe('IntlLanguagePipe', () => { it('should respect the setting from default config', () => { TestBed.configureTestingModule({ providers: [ - IntlLanguagePipe, { provide: INTL_LOCALES, useValue: 'de-DE', @@ -90,7 +101,7 @@ describe('IntlLanguagePipe', () => { }, ], }); - testUnit = TestBed.inject(IntlLanguagePipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlLanguagePipe())); expect(testUnit.transform('de-AT')).toEqual('Deutsch (Österreich)'); }); @@ -98,7 +109,6 @@ describe('IntlLanguagePipe', () => { it('should give the user options a higher priority', () => { TestBed.configureTestingModule({ providers: [ - IntlLanguagePipe, { provide: INTL_LOCALES, useValue: 'de-DE', @@ -111,7 +121,7 @@ describe('IntlLanguagePipe', () => { }, ], }); - testUnit = TestBed.inject(IntlLanguagePipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlLanguagePipe())); expect( testUnit.transform('de-AT', { languageDisplay: 'standard' }), @@ -121,7 +131,6 @@ describe('IntlLanguagePipe', () => { it('should not override the type option', () => { TestBed.configureTestingModule({ providers: [ - IntlLanguagePipe, { provide: INTL_LOCALES, useValue: 'de-DE', @@ -134,7 +143,7 @@ describe('IntlLanguagePipe', () => { }, ], }); - testUnit = TestBed.inject(IntlLanguagePipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlLanguagePipe())); expect(testUnit.transform('de', { type: 'region' })).toEqual('Deutsch'); }); @@ -143,14 +152,13 @@ describe('IntlLanguagePipe', () => { it('should respect locale option', () => { TestBed.configureTestingModule({ providers: [ - IntlLanguagePipe, { provide: INTL_LOCALES, useValue: 'en-US', }, ], }); - testUnit = TestBed.inject(IntlLanguagePipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlLanguagePipe())); expect(testUnit.transform('de-DE', { locale: 'de-DE' })).toEqual( 'Deutsch (Deutschland)', diff --git a/projects/angular-ecmascript-intl/src/lib/language/intl-language.pipe.ts b/projects/angular-ecmascript-intl/src/lib/language/intl-language.pipe.ts index ec534bc5..10217e54 100644 --- a/projects/angular-ecmascript-intl/src/lib/language/intl-language.pipe.ts +++ b/projects/angular-ecmascript-intl/src/lib/language/intl-language.pipe.ts @@ -1,4 +1,4 @@ -import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; +import { Pipe, PipeTransform, inject } from '@angular/core'; import { IntlPipeOptions } from '../intl-pipe-options'; import { INTL_LOCALES } from '../locale'; import { INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS } from './intl-language-pipe-default-options'; @@ -11,14 +11,11 @@ export type IntlLanguagePipeOptions = Partial & standalone: true, }) export class IntlLanguagePipe implements PipeTransform { - constructor( - @Optional() - @Inject(INTL_LOCALES) - readonly locale?: string | string[] | null, - @Optional() - @Inject(INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS) - readonly defaultOptions?: Omit | null, - ) {} + private readonly locale? = inject(INTL_LOCALES, { optional: true }); + private readonly defaultOptions? = inject | null>(INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS, { optional: true }); transform( value: string | null | undefined, diff --git a/projects/angular-ecmascript-intl/src/lib/list/intl-list.pipe.spec.ts b/projects/angular-ecmascript-intl/src/lib/list/intl-list.pipe.spec.ts index d68cc9e3..3942d5b4 100644 --- a/projects/angular-ecmascript-intl/src/lib/list/intl-list.pipe.spec.ts +++ b/projects/angular-ecmascript-intl/src/lib/list/intl-list.pipe.spec.ts @@ -7,7 +7,10 @@ describe('IntlListPipe', () => { describe('parsing', () => { beforeEach(() => { - testUnit = new IntlListPipe('en-US'); + TestBed.runInInjectionContext(() => { + testUnit = new IntlListPipe(); + Object.defineProperty(testUnit, 'locale', { value: 'en-US' }); + }); }); it('should create an instance', () => { @@ -44,14 +47,13 @@ describe('IntlListPipe', () => { it('should respect the set locale', () => { TestBed.configureTestingModule({ providers: [ - IntlListPipe, { provide: INTL_LOCALES, useValue: 'de-DE', }, ], }); - testUnit = TestBed.inject(IntlListPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlListPipe())); expect(testUnit.transform(['Äpfel', 'Birnen'])).toEqual( 'Äpfel und Birnen', @@ -59,13 +61,22 @@ describe('IntlListPipe', () => { }); it('should fall back to the browser default locale', () => { - TestBed.configureTestingModule({ providers: [IntlListPipe] }); + let defaultLanguageTestUnit!: IntlListPipe; + let browserLanguageTestUnit!: IntlListPipe; + + TestBed.runInInjectionContext(() => { + defaultLanguageTestUnit = new IntlListPipe(); + browserLanguageTestUnit = new IntlListPipe(); + Object.defineProperty(browserLanguageTestUnit, 'locale', { + value: undefined, + }); + Object.defineProperty(defaultLanguageTestUnit, 'locale', { + value: navigator.language, + }); + }); - const result1 = TestBed.inject(IntlListPipe).transform(['some', 'val']); - const result2 = new IntlListPipe(navigator.language).transform([ - 'some', - 'val', - ]); + const result1 = browserLanguageTestUnit.transform(['some', 'val']); + const result2 = defaultLanguageTestUnit.transform(['some', 'val']); expect(result1).toEqual(result2); }); @@ -74,14 +85,13 @@ describe('IntlListPipe', () => { it('should respect locale option', () => { TestBed.configureTestingModule({ providers: [ - IntlListPipe, { provide: INTL_LOCALES, useValue: 'en-US', }, ], }); - testUnit = TestBed.inject(IntlListPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlListPipe())); expect( testUnit.transform(['Äpfel', 'Birnen'], { locale: 'de-DE' }), diff --git a/projects/angular-ecmascript-intl/src/lib/list/intl-list.pipe.ts b/projects/angular-ecmascript-intl/src/lib/list/intl-list.pipe.ts index be8ac583..978e1c71 100644 --- a/projects/angular-ecmascript-intl/src/lib/list/intl-list.pipe.ts +++ b/projects/angular-ecmascript-intl/src/lib/list/intl-list.pipe.ts @@ -1,4 +1,4 @@ -import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; +import { Pipe, PipeTransform, inject } from '@angular/core'; import { IntlPipeOptions } from '../intl-pipe-options'; import { INTL_LOCALES } from '../locale'; import { INTL_LIST_PIPE_DEFAULT_OPTIONS } from './intl-list-pipe-default-options'; @@ -11,14 +11,11 @@ export type IntlListPipeOptions = Partial & standalone: true, }) export class IntlListPipe implements PipeTransform { - constructor( - @Optional() - @Inject(INTL_LOCALES) - readonly locale?: string | string[] | null, - @Optional() - @Inject(INTL_LIST_PIPE_DEFAULT_OPTIONS) - readonly defaultOptions?: Omit | null, - ) {} + private readonly locale? = inject(INTL_LOCALES, { optional: true }); + private readonly defaultOptions? = inject | null>(INTL_LIST_PIPE_DEFAULT_OPTIONS, { optional: true }); transform( value: Iterable | null | undefined, diff --git a/projects/angular-ecmascript-intl/src/lib/percent/intl-percent.pipe.spec.ts b/projects/angular-ecmascript-intl/src/lib/percent/intl-percent.pipe.spec.ts index 8f745f6a..4cb74b11 100644 --- a/projects/angular-ecmascript-intl/src/lib/percent/intl-percent.pipe.spec.ts +++ b/projects/angular-ecmascript-intl/src/lib/percent/intl-percent.pipe.spec.ts @@ -8,7 +8,10 @@ describe('IntlPercentPipe', () => { describe('parsing', () => { beforeEach(() => { - testUnit = new IntlPercentPipe('en-US'); + TestBed.runInInjectionContext(() => { + testUnit = new IntlPercentPipe(); + Object.defineProperty(testUnit, 'locale', { value: 'en-US' }); + }); }); it('should create an instance', () => { @@ -53,23 +56,34 @@ describe('IntlPercentPipe', () => { it('should respect the set locale', () => { TestBed.configureTestingModule({ providers: [ - IntlPercentPipe, { provide: INTL_LOCALES, useValue: 'de-DE', }, ], }); - testUnit = TestBed.inject(IntlPercentPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlPercentPipe())); expect(testUnit.transform(1)).toEqual('100\xa0%'); }); it('should fall back to the browser default locale', () => { - TestBed.configureTestingModule({ providers: [IntlPercentPipe] }); + let defaultLanguageTestUnit!: IntlPercentPipe; + let browserLanguageTestUnit!: IntlPercentPipe; + + TestBed.runInInjectionContext(() => { + defaultLanguageTestUnit = new IntlPercentPipe(); + browserLanguageTestUnit = new IntlPercentPipe(); + Object.defineProperty(browserLanguageTestUnit, 'locale', { + value: undefined, + }); + Object.defineProperty(defaultLanguageTestUnit, 'locale', { + value: navigator.language, + }); + }); - const result1 = TestBed.inject(IntlPercentPipe).transform(0.1); - const result2 = new IntlPercentPipe(navigator.language).transform(0.1); + const result1 = browserLanguageTestUnit.transform(0.1); + const result2 = defaultLanguageTestUnit.transform(0.1); expect(result1).toEqual(result2); }); @@ -79,7 +93,6 @@ describe('IntlPercentPipe', () => { it('should respect the setting from default config', () => { TestBed.configureTestingModule({ providers: [ - IntlPercentPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -92,7 +105,7 @@ describe('IntlPercentPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlPercentPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlPercentPipe())); expect(testUnit.transform(1)).toEqual('+100%'); }); @@ -100,7 +113,6 @@ describe('IntlPercentPipe', () => { it('should give the user options a higher priority', () => { TestBed.configureTestingModule({ providers: [ - IntlPercentPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -113,7 +125,7 @@ describe('IntlPercentPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlPercentPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlPercentPipe())); expect(testUnit.transform(1, { signDisplay: 'never' })).toEqual('100%'); }); @@ -122,14 +134,13 @@ describe('IntlPercentPipe', () => { it('should respect locale option', () => { TestBed.configureTestingModule({ providers: [ - IntlPercentPipe, { provide: INTL_LOCALES, useValue: 'en-US', }, ], }); - testUnit = TestBed.inject(IntlPercentPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlPercentPipe())); expect(testUnit.transform(1, { locale: 'de-DE' })).toEqual('100\xa0%'); }); @@ -137,7 +148,6 @@ describe('IntlPercentPipe', () => { it('should not override the style option', () => { TestBed.configureTestingModule({ providers: [ - IntlPercentPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -150,7 +160,7 @@ describe('IntlPercentPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlPercentPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlPercentPipe())); expect(testUnit.transform(1, { style: 'decimal' })).toEqual('100%'); }); diff --git a/projects/angular-ecmascript-intl/src/lib/percent/intl-percent.pipe.ts b/projects/angular-ecmascript-intl/src/lib/percent/intl-percent.pipe.ts index fb669e30..e79e4369 100644 --- a/projects/angular-ecmascript-intl/src/lib/percent/intl-percent.pipe.ts +++ b/projects/angular-ecmascript-intl/src/lib/percent/intl-percent.pipe.ts @@ -1,4 +1,4 @@ -import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; +import { Pipe, PipeTransform, inject } from '@angular/core'; import { IntlPipeOptions } from '../intl-pipe-options'; import { INTL_LOCALES } from '../locale'; import { getNumericValue } from '../utils/number-utils'; @@ -21,14 +21,11 @@ export type IntlPercentPipeOptions = Omit< standalone: true, }) export class IntlPercentPipe implements PipeTransform { - constructor( - @Optional() - @Inject(INTL_LOCALES) - readonly locale?: string | string[] | null, - @Optional() - @Inject(INTL_PERCENT_PIPE_DEFAULT_OPTIONS) - readonly defaultOptions?: Omit | null, - ) {} + private readonly locale? = inject(INTL_LOCALES, { optional: true }); + private readonly defaultOptions? = inject | null>(INTL_PERCENT_PIPE_DEFAULT_OPTIONS, { optional: true }); transform( value: number | string | null | undefined, diff --git a/projects/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.spec.ts b/projects/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.spec.ts index 4c29f411..cbb843f7 100644 --- a/projects/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.spec.ts +++ b/projects/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.spec.ts @@ -1,4 +1,3 @@ -import { ChangeDetectorRef } from '@angular/core'; import { fakeAsync, TestBed, tick } from '@angular/core/testing'; import dayjs from 'dayjs'; import { INTL_LOCALES } from '../locale'; @@ -9,13 +8,18 @@ describe('RelativeTimePipe', () => { let testUnit: IntlRelativeTimePipe; it('should create an instance', () => { - testUnit = new IntlRelativeTimePipe(); + TestBed.runInInjectionContext( + () => (testUnit = new IntlRelativeTimePipe()), + ); expect(testUnit).toBeTruthy(); }); describe('parsing', () => { beforeEach(() => { - testUnit = new IntlRelativeTimePipe('en-US'); + TestBed.runInInjectionContext(() => { + testUnit = new IntlRelativeTimePipe(); + Object.defineProperty(testUnit, 'locales', { value: 'en-US' }); + }); }); it('should handle null values', () => { @@ -157,7 +161,6 @@ describe('RelativeTimePipe', () => { beforeEach(() => { TestBed.configureTestingModule({ providers: [ - IntlRelativeTimePipe, { provide: INTL_RELATIVE_TIME_PIPE_DEFAULT_OPTIONS, useValue: { numeric: 'auto', style: 'short' }, @@ -168,7 +171,9 @@ describe('RelativeTimePipe', () => { }, ], }); - testUnit = TestBed.inject(IntlRelativeTimePipe); + TestBed.runInInjectionContext( + () => (testUnit = new IntlRelativeTimePipe()), + ); }); it('should respect the default options', () => { @@ -189,12 +194,22 @@ describe('RelativeTimePipe', () => { }); it('should fall back to the default locale', () => { - TestBed.configureTestingModule({ providers: [IntlRelativeTimePipe] }); + let defaultLanguageTestUnit!: IntlRelativeTimePipe; + let browserLanguageTestUnit!: IntlRelativeTimePipe; + + TestBed.runInInjectionContext(() => { + defaultLanguageTestUnit = new IntlRelativeTimePipe(); + browserLanguageTestUnit = new IntlRelativeTimePipe(); + Object.defineProperty(browserLanguageTestUnit, 'locales', { + value: undefined, + }); + Object.defineProperty(defaultLanguageTestUnit, 'locales', { + value: navigator.language, + }); + }); - const result1 = TestBed.inject(IntlRelativeTimePipe).transform(new Date()); - const result2 = new IntlRelativeTimePipe(navigator.language).transform( - new Date(), - ); + const result1 = browserLanguageTestUnit.transform(new Date()); + const result2 = defaultLanguageTestUnit.transform(new Date()); expect(result1).toEqual(result2); }); @@ -202,17 +217,19 @@ describe('RelativeTimePipe', () => { describe('timer', () => { const cdrMock = { markForCheck: jasmine.createSpy(), - } as unknown as ChangeDetectorRef; + }; beforeEach(() => { - testUnit = new IntlRelativeTimePipe(null, null, cdrMock); + TestBed.runInInjectionContext(() => { + testUnit = new IntlRelativeTimePipe(); + Object.defineProperty(testUnit, 'cdr', { value: cdrMock }); + }); }); it('should mark for check once after 1 minute', fakeAsync(() => { testUnit.transform(0); tick(60000); - // eslint-disable-next-line @typescript-eslint/unbound-method expect(cdrMock.markForCheck).toHaveBeenCalledTimes(1); testUnit.ngOnDestroy(); @@ -222,14 +239,13 @@ describe('RelativeTimePipe', () => { testUnit.transform(new Date()); tick(600000); - // eslint-disable-next-line @typescript-eslint/unbound-method expect(cdrMock.markForCheck).toHaveBeenCalledTimes(10); testUnit.ngOnDestroy(); })); afterEach(() => { - (cdrMock.markForCheck as jasmine.Spy).calls.reset(); + cdrMock.markForCheck.calls.reset(); }); }); }); diff --git a/projects/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.ts b/projects/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.ts index e00398ef..5ce8d29b 100644 --- a/projects/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.ts +++ b/projects/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.ts @@ -1,10 +1,9 @@ import { ChangeDetectorRef, - Inject, OnDestroy, - Optional, Pipe, PipeTransform, + inject, } from '@angular/core'; import { Subject, interval, takeUntil } from 'rxjs'; import { IntlPipeOptions } from '../intl-pipe-options'; @@ -30,20 +29,14 @@ enum Time { pure: false, }) export class IntlRelativeTimePipe implements PipeTransform, OnDestroy { - #destroy$?: Subject; + private readonly locales? = inject(INTL_LOCALES, { optional: true }); + private readonly defaultOptions? = inject | null>(INTL_RELATIVE_TIME_PIPE_DEFAULT_OPTIONS, { optional: true }); + private readonly cdr? = inject(ChangeDetectorRef, { optional: true }); - constructor( - @Optional() - @Inject(INTL_LOCALES) - readonly locales?: string | string[] | null, - @Optional() - @Inject(INTL_RELATIVE_TIME_PIPE_DEFAULT_OPTIONS) - readonly defaultOptions?: Omit< - IntlRelativeTimePipeOptions, - 'locale' - > | null, - @Optional() readonly cdr?: ChangeDetectorRef, - ) {} + #destroy$?: Subject; transform( value: string | number | Date | null | undefined, diff --git a/projects/angular-ecmascript-intl/src/lib/unit/intl-unit.pipe.spec.ts b/projects/angular-ecmascript-intl/src/lib/unit/intl-unit.pipe.spec.ts index 3ddfd775..26c4f381 100644 --- a/projects/angular-ecmascript-intl/src/lib/unit/intl-unit.pipe.spec.ts +++ b/projects/angular-ecmascript-intl/src/lib/unit/intl-unit.pipe.spec.ts @@ -8,7 +8,10 @@ describe('IntlUnitPipe', () => { describe('parsing', () => { beforeEach(() => { - testUnit = new IntlUnitPipe('en-US'); + TestBed.runInInjectionContext(() => { + testUnit = new IntlUnitPipe(); + Object.defineProperty(testUnit, 'locale', { value: 'en-US' }); + }); }); it('should create an instance', () => { @@ -53,23 +56,34 @@ describe('IntlUnitPipe', () => { it('should respect the set locale', () => { TestBed.configureTestingModule({ providers: [ - IntlUnitPipe, { provide: INTL_LOCALES, useValue: 'de-DE', }, ], }); - testUnit = TestBed.inject(IntlUnitPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlUnitPipe())); expect(testUnit.transform(1, 'hour')).toEqual('1 Std.'); }); it('should fall back to the browser default locale', () => { - TestBed.configureTestingModule({ providers: [IntlUnitPipe] }); + let defaultLanguageTestUnit!: IntlUnitPipe; + let browserLanguageTestUnit!: IntlUnitPipe; + + TestBed.runInInjectionContext(() => { + defaultLanguageTestUnit = new IntlUnitPipe(); + browserLanguageTestUnit = new IntlUnitPipe(); + Object.defineProperty(browserLanguageTestUnit, 'locale', { + value: undefined, + }); + Object.defineProperty(defaultLanguageTestUnit, 'locale', { + value: navigator.language, + }); + }); - const result1 = TestBed.inject(IntlUnitPipe).transform(1, 'hour'); - const result2 = new IntlUnitPipe(navigator.language).transform(1, 'hour'); + const result1 = browserLanguageTestUnit.transform(1, 'hour'); + const result2 = defaultLanguageTestUnit.transform(1, 'hour'); expect(result1).toEqual(result2); }); @@ -79,7 +93,6 @@ describe('IntlUnitPipe', () => { it('should respect the setting from default config', () => { TestBed.configureTestingModule({ providers: [ - IntlUnitPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -92,7 +105,7 @@ describe('IntlUnitPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlUnitPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlUnitPipe())); expect(testUnit.transform(1, 'liter')).toEqual('1L'); }); @@ -100,7 +113,6 @@ describe('IntlUnitPipe', () => { it('should give the user options a higher priority', () => { TestBed.configureTestingModule({ providers: [ - IntlUnitPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -113,7 +125,7 @@ describe('IntlUnitPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlUnitPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlUnitPipe())); expect(testUnit.transform(1, 'liter', { unitDisplay: 'narrow' })).toEqual( '1L', @@ -124,14 +136,13 @@ describe('IntlUnitPipe', () => { it('should respect locale option', () => { TestBed.configureTestingModule({ providers: [ - IntlUnitPipe, { provide: INTL_LOCALES, useValue: 'en-US', }, ], }); - testUnit = TestBed.inject(IntlUnitPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlUnitPipe())); expect(testUnit.transform(1, 'hour', { locale: 'de-DE' })).toEqual( '1 Std.', @@ -141,7 +152,6 @@ describe('IntlUnitPipe', () => { it('should not override the style option', () => { TestBed.configureTestingModule({ providers: [ - IntlUnitPipe, { provide: INTL_LOCALES, useValue: 'en-US', @@ -154,7 +164,7 @@ describe('IntlUnitPipe', () => { }, ], }); - testUnit = TestBed.inject(IntlUnitPipe); + TestBed.runInInjectionContext(() => (testUnit = new IntlUnitPipe())); expect(testUnit.transform(1, 'hour', { style: 'decimal' })).toEqual('1 hr'); }); diff --git a/projects/angular-ecmascript-intl/src/lib/unit/intl-unit.pipe.ts b/projects/angular-ecmascript-intl/src/lib/unit/intl-unit.pipe.ts index 17a6338e..979a49d3 100644 --- a/projects/angular-ecmascript-intl/src/lib/unit/intl-unit.pipe.ts +++ b/projects/angular-ecmascript-intl/src/lib/unit/intl-unit.pipe.ts @@ -1,4 +1,4 @@ -import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; +import { Pipe, PipeTransform, inject } from '@angular/core'; import { IntlPipeOptions } from '../intl-pipe-options'; import { INTL_LOCALES } from '../locale'; import { getNumericValue } from '../utils/number-utils'; @@ -16,14 +16,11 @@ export type IntlUnitPipeOptions = Omit< standalone: true, }) export class IntlUnitPipe implements PipeTransform { - constructor( - @Optional() - @Inject(INTL_LOCALES) - readonly locale?: string | string[] | null, - @Optional() - @Inject(INTL_UNIT_PIPE_DEFAULT_OPTIONS) - readonly defaultOptions?: Omit | null, - ) {} + private readonly locale? = inject(INTL_LOCALES, { optional: true }); + private readonly defaultOptions? = inject | null>(INTL_UNIT_PIPE_DEFAULT_OPTIONS, { optional: true }); transform( value: number | string | null | undefined,