Skip to content

Commit ce948b6

Browse files
committed
refactor: inject migration
1 parent cd7eeef commit ce948b6

18 files changed

+273
-217
lines changed

projects/angular-ecmascript-intl/src/lib/country/intl-country.pipe.spec.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ describe('IntlCountryPipe', () => {
88

99
describe('parsing', () => {
1010
beforeEach(() => {
11-
testUnit = new IntlCountryPipe('en-US');
11+
TestBed.runInInjectionContext(() => {
12+
testUnit = new IntlCountryPipe();
13+
Object.defineProperty(testUnit, 'locale', { value: 'en-US' });
14+
});
1215
});
1316

1417
it('should create an instance', () => {
@@ -49,23 +52,34 @@ describe('IntlCountryPipe', () => {
4952
it('should respect the set locale', () => {
5053
TestBed.configureTestingModule({
5154
providers: [
52-
IntlCountryPipe,
5355
{
5456
provide: INTL_LOCALES,
5557
useValue: 'de-DE',
5658
},
5759
],
5860
});
59-
testUnit = TestBed.inject(IntlCountryPipe);
61+
TestBed.runInInjectionContext(() => (testUnit = new IntlCountryPipe()));
6062

6163
expect(testUnit.transform('AT')).toEqual('Österreich');
6264
});
6365

6466
it('should fall back to the browser default locale', () => {
65-
TestBed.configureTestingModule({ providers: [IntlCountryPipe] });
67+
let defaultLanguageTestUnit!: IntlCountryPipe;
68+
let browserLanguageTestUnit!: IntlCountryPipe;
69+
70+
TestBed.runInInjectionContext(() => {
71+
defaultLanguageTestUnit = new IntlCountryPipe();
72+
browserLanguageTestUnit = new IntlCountryPipe();
73+
Object.defineProperty(browserLanguageTestUnit, 'locale', {
74+
value: undefined,
75+
});
76+
Object.defineProperty(defaultLanguageTestUnit, 'locale', {
77+
value: navigator.language,
78+
});
79+
});
6680

67-
const result1 = TestBed.inject(IntlCountryPipe).transform('US');
68-
const result2 = new IntlCountryPipe(navigator.language).transform('US');
81+
const result1 = browserLanguageTestUnit.transform('US');
82+
const result2 = defaultLanguageTestUnit.transform('US');
6983

7084
expect(result1).toEqual(result2);
7185
});
@@ -75,7 +89,6 @@ describe('IntlCountryPipe', () => {
7589
it('should not override the type option', () => {
7690
TestBed.configureTestingModule({
7791
providers: [
78-
IntlCountryPipe,
7992
{
8093
provide: INTL_LOCALES,
8194
useValue: 'de-DE',
@@ -88,7 +101,7 @@ describe('IntlCountryPipe', () => {
88101
},
89102
],
90103
});
91-
testUnit = TestBed.inject(IntlCountryPipe);
104+
TestBed.runInInjectionContext(() => (testUnit = new IntlCountryPipe()));
92105

93106
expect(testUnit.transform('DE', { type: 'language' })).toEqual(
94107
'Deutschland',
@@ -99,14 +112,13 @@ describe('IntlCountryPipe', () => {
99112
it('should respect locale option', () => {
100113
TestBed.configureTestingModule({
101114
providers: [
102-
IntlCountryPipe,
103115
{
104116
provide: INTL_LOCALES,
105117
useValue: 'en-US',
106118
},
107119
],
108120
});
109-
testUnit = TestBed.inject(IntlCountryPipe);
121+
TestBed.runInInjectionContext(() => (testUnit = new IntlCountryPipe()));
110122

111123
expect(testUnit.transform('US', { locale: 'de-DE' })).toEqual(
112124
'Vereinigte Staaten',

projects/angular-ecmascript-intl/src/lib/country/intl-country.pipe.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Optional, Pipe, PipeTransform } from '@angular/core';
1+
import { Pipe, PipeTransform, inject } from '@angular/core';
22
import { IntlPipeOptions } from '../intl-pipe-options';
33
import { INTL_LOCALES } from '../locale';
44
import { INTL_COUNTRY_PIPE_DEFAULT_OPTIONS } from './intl-country-pipe-default-options';
@@ -14,14 +14,11 @@ export type IntlCountryPipeOptions = Omit<
1414
standalone: true,
1515
})
1616
export class IntlCountryPipe implements PipeTransform {
17-
constructor(
18-
@Optional()
19-
@Inject(INTL_LOCALES)
20-
readonly locale?: string | string[] | null,
21-
@Optional()
22-
@Inject(INTL_COUNTRY_PIPE_DEFAULT_OPTIONS)
23-
readonly defaultOptions?: Omit<IntlCountryPipeOptions, 'locale'> | null,
24-
) {}
17+
readonly locale? = inject(INTL_LOCALES, { optional: true });
18+
readonly defaultOptions? = inject<Omit<
19+
IntlCountryPipeOptions,
20+
'locale'
21+
> | null>(INTL_COUNTRY_PIPE_DEFAULT_OPTIONS, { optional: true });
2522

2623
transform(
2724
value: string | null | undefined,

projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.spec.ts

+24-20
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ describe('IntlCurrencyPipe', () => {
88

99
describe('parsing', () => {
1010
beforeEach(() => {
11-
testUnit = new IntlCurrencyPipe('en-US');
11+
TestBed.runInInjectionContext(() => {
12+
testUnit = new IntlCurrencyPipe();
13+
Object.defineProperty(testUnit, 'locale', { value: 'en-US' });
14+
});
1215
});
1316

1417
it('should create an instance', () => {
@@ -53,29 +56,34 @@ describe('IntlCurrencyPipe', () => {
5356
it('should respect the set locale', () => {
5457
TestBed.configureTestingModule({
5558
providers: [
56-
IntlCurrencyPipe,
5759
{
5860
provide: INTL_LOCALES,
5961
useValue: 'de-DE',
6062
},
6163
],
6264
});
63-
testUnit = TestBed.inject(IntlCurrencyPipe);
65+
TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe()));
6466

6567
expect(testUnit.transform(1024.2249, 'EUR')).toEqual('1.024,22\xa0€');
6668
});
6769

6870
it('should fall back to the browser default locale', () => {
69-
TestBed.configureTestingModule({ providers: [IntlCurrencyPipe] });
71+
let defaultLanguageTestUnit!: IntlCurrencyPipe;
72+
let browserLanguageTestUnit!: IntlCurrencyPipe;
73+
74+
TestBed.runInInjectionContext(() => {
75+
defaultLanguageTestUnit = new IntlCurrencyPipe();
76+
browserLanguageTestUnit = new IntlCurrencyPipe();
77+
Object.defineProperty(browserLanguageTestUnit, 'locale', {
78+
value: undefined,
79+
});
80+
Object.defineProperty(defaultLanguageTestUnit, 'locale', {
81+
value: navigator.language,
82+
});
83+
});
7084

71-
const result1 = TestBed.inject(IntlCurrencyPipe).transform(
72-
1024.2249,
73-
'EUR',
74-
);
75-
const result2 = new IntlCurrencyPipe(navigator.language).transform(
76-
1024.2249,
77-
'EUR',
78-
);
85+
const result1 = browserLanguageTestUnit.transform(1024.2249, 'EUR');
86+
const result2 = defaultLanguageTestUnit.transform(1024.2249, 'EUR');
7987

8088
expect(result1).toEqual(result2);
8189
});
@@ -85,7 +93,6 @@ describe('IntlCurrencyPipe', () => {
8593
it('should respect the setting from default config', () => {
8694
TestBed.configureTestingModule({
8795
providers: [
88-
IntlCurrencyPipe,
8996
{
9097
provide: INTL_LOCALES,
9198
useValue: 'en-US',
@@ -98,15 +105,14 @@ describe('IntlCurrencyPipe', () => {
98105
},
99106
],
100107
});
101-
testUnit = TestBed.inject(IntlCurrencyPipe);
108+
TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe()));
102109

103110
expect(testUnit.transform(1, 'USD')).toEqual('+$1.00');
104111
});
105112

106113
it('should give the user options a higher priority', () => {
107114
TestBed.configureTestingModule({
108115
providers: [
109-
IntlCurrencyPipe,
110116
{
111117
provide: INTL_LOCALES,
112118
useValue: 'en-US',
@@ -119,7 +125,7 @@ describe('IntlCurrencyPipe', () => {
119125
},
120126
],
121127
});
122-
testUnit = TestBed.inject(IntlCurrencyPipe);
128+
TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe()));
123129

124130
expect(testUnit.transform(1, 'USD', { signDisplay: 'never' })).toEqual(
125131
'$1.00',
@@ -130,14 +136,13 @@ describe('IntlCurrencyPipe', () => {
130136
it('should respect locale option', () => {
131137
TestBed.configureTestingModule({
132138
providers: [
133-
IntlCurrencyPipe,
134139
{
135140
provide: INTL_LOCALES,
136141
useValue: 'en-US',
137142
},
138143
],
139144
});
140-
testUnit = TestBed.inject(IntlCurrencyPipe);
145+
TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe()));
141146

142147
expect(testUnit.transform(1024, 'USD', { locale: 'de-DE' })).toEqual(
143148
'1.024,00\xa0$',
@@ -147,7 +152,6 @@ describe('IntlCurrencyPipe', () => {
147152
it('should not override the style option', () => {
148153
TestBed.configureTestingModule({
149154
providers: [
150-
IntlCurrencyPipe,
151155
{
152156
provide: INTL_LOCALES,
153157
useValue: 'en-US',
@@ -160,7 +164,7 @@ describe('IntlCurrencyPipe', () => {
160164
},
161165
],
162166
});
163-
testUnit = TestBed.inject(IntlCurrencyPipe);
167+
TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe()));
164168

165169
expect(testUnit.transform(1, 'USD', { style: 'percent' })).toEqual('$1.00');
166170
});

projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Optional, Pipe, PipeTransform } from '@angular/core';
1+
import { Pipe, PipeTransform, inject } from '@angular/core';
22
import { IntlPipeOptions } from '../intl-pipe-options';
33
import { INTL_LOCALES } from '../locale';
44
import { getNumericValue } from '../utils/number-utils';
@@ -16,14 +16,11 @@ export type IntlCurrencyPipeOptions = Omit<
1616
standalone: true,
1717
})
1818
export class IntlCurrencyPipe implements PipeTransform {
19-
constructor(
20-
@Optional()
21-
@Inject(INTL_LOCALES)
22-
readonly locale?: string | string[] | null,
23-
@Optional()
24-
@Inject(INTL_CURRENCY_PIPE_DEFAULT_OPTIONS)
25-
readonly defaultOptions?: Omit<IntlCurrencyPipeOptions, 'locale'> | null,
26-
) {}
19+
readonly locale? = inject(INTL_LOCALES, { optional: true });
20+
readonly defaultOptions? = inject<Omit<
21+
IntlCurrencyPipeOptions,
22+
'locale'
23+
> | null>(INTL_CURRENCY_PIPE_DEFAULT_OPTIONS, { optional: true });
2724

2825
transform(
2926
value: number | string | null | undefined,

projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.spec.ts

+23-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ describe('DatePipe', () => {
88

99
describe('date parsing', () => {
1010
beforeEach(() => {
11-
testUnit = new IntlDatePipe('en-US');
11+
TestBed.runInInjectionContext(() => {
12+
testUnit = new IntlDatePipe();
13+
Object.defineProperty(testUnit, 'locale', { value: 'en-US' });
14+
});
1215
});
1316

1417
it('should create an instance', () => {
@@ -69,25 +72,34 @@ describe('DatePipe', () => {
6972
it('should respect the set locale', () => {
7073
TestBed.configureTestingModule({
7174
providers: [
72-
IntlDatePipe,
7375
{
7476
provide: INTL_LOCALES,
7577
useValue: 'de-DE',
7678
},
7779
],
7880
});
79-
testUnit = TestBed.inject(IntlDatePipe);
81+
TestBed.runInInjectionContext(() => (testUnit = new IntlDatePipe()));
8082

8183
expect(testUnit.transform('2023-02-19')).toEqual('19.2.2023');
8284
});
8385

8486
it('should fall back to the browser default locale', () => {
85-
TestBed.configureTestingModule({ providers: [IntlDatePipe] });
87+
let defaultLanguageTestUnit!: IntlDatePipe;
88+
let browserLanguageTestUnit!: IntlDatePipe;
89+
90+
TestBed.runInInjectionContext(() => {
91+
defaultLanguageTestUnit = new IntlDatePipe();
92+
browserLanguageTestUnit = new IntlDatePipe();
93+
Object.defineProperty(browserLanguageTestUnit, 'locale', {
94+
value: undefined,
95+
});
96+
Object.defineProperty(defaultLanguageTestUnit, 'locale', {
97+
value: navigator.language,
98+
});
99+
});
86100

87-
const result1 = TestBed.inject(IntlDatePipe).transform('2023-02-19');
88-
const result2 = new IntlDatePipe(navigator.language).transform(
89-
'2023-02-19',
90-
);
101+
const result1 = browserLanguageTestUnit.transform('2023-02-19');
102+
const result2 = defaultLanguageTestUnit.transform('2023-02-19');
91103

92104
expect(result1).toEqual(result2);
93105
});
@@ -97,7 +109,6 @@ describe('DatePipe', () => {
97109
it('should respect the setting from default config', () => {
98110
TestBed.configureTestingModule({
99111
providers: [
100-
IntlDatePipe,
101112
{
102113
provide: INTL_LOCALES,
103114
useValue: 'en-US',
@@ -110,15 +121,14 @@ describe('DatePipe', () => {
110121
},
111122
],
112123
});
113-
testUnit = TestBed.inject(IntlDatePipe);
124+
TestBed.runInInjectionContext(() => (testUnit = new IntlDatePipe()));
114125

115126
expect(testUnit.transform('2023-02-19')).toEqual('Feb 19, 2023');
116127
});
117128

118129
it('should give the user options a higher priority', () => {
119130
TestBed.configureTestingModule({
120131
providers: [
121-
IntlDatePipe,
122132
{
123133
provide: INTL_LOCALES,
124134
useValue: 'en-US',
@@ -131,7 +141,7 @@ describe('DatePipe', () => {
131141
},
132142
],
133143
});
134-
testUnit = TestBed.inject(IntlDatePipe);
144+
TestBed.runInInjectionContext(() => (testUnit = new IntlDatePipe()));
135145

136146
expect(testUnit.transform('2023-02-19', { dateStyle: 'medium' })).toEqual(
137147
'Feb 19, 2023',
@@ -142,14 +152,13 @@ describe('DatePipe', () => {
142152
it('should respect locale option', () => {
143153
TestBed.configureTestingModule({
144154
providers: [
145-
IntlDatePipe,
146155
{
147156
provide: INTL_LOCALES,
148157
useValue: 'en-US',
149158
},
150159
],
151160
});
152-
testUnit = TestBed.inject(IntlDatePipe);
161+
TestBed.runInInjectionContext(() => (testUnit = new IntlDatePipe()));
153162

154163
expect(testUnit.transform('2023-02-19', { locale: 'de-DE' })).toEqual(
155164
'19.2.2023',

projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Optional, Pipe, PipeTransform } from '@angular/core';
1+
import { Pipe, PipeTransform, inject } from '@angular/core';
22
import { IntlPipeOptions } from '../intl-pipe-options';
33
import { INTL_LOCALES } from '../locale';
44
import { INTL_DATE_PIPE_DEFAULT_OPTIONS } from './intl-date-pipe-default-options';
@@ -11,14 +11,11 @@ export type IntlDatePipeOptions = Partial<Intl.DateTimeFormatOptions> &
1111
standalone: true,
1212
})
1313
export class IntlDatePipe implements PipeTransform {
14-
constructor(
15-
@Optional()
16-
@Inject(INTL_LOCALES)
17-
readonly locale?: string | string[] | null,
18-
@Optional()
19-
@Inject(INTL_DATE_PIPE_DEFAULT_OPTIONS)
20-
readonly defaultOptions?: Omit<IntlDatePipeOptions, 'locale'> | null,
21-
) {}
14+
readonly locale? = inject(INTL_LOCALES, { optional: true });
15+
readonly defaultOptions? = inject<Omit<IntlDatePipeOptions, 'locale'> | null>(
16+
INTL_DATE_PIPE_DEFAULT_OPTIONS,
17+
{ optional: true },
18+
);
2219

2320
transform(
2421
value: string | number | Date | null | undefined,

0 commit comments

Comments
 (0)