Skip to content

refactor: inject migration #513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
Expand All @@ -75,7 +89,6 @@ describe('IntlCountryPipe', () => {
it('should not override the type option', () => {
TestBed.configureTestingModule({
providers: [
IntlCountryPipe,
{
provide: INTL_LOCALES,
useValue: 'de-DE',
Expand All @@ -88,7 +101,7 @@ describe('IntlCountryPipe', () => {
},
],
});
testUnit = TestBed.inject(IntlCountryPipe);
TestBed.runInInjectionContext(() => (testUnit = new IntlCountryPipe()));

expect(testUnit.transform('DE', { type: 'language' })).toEqual(
'Deutschland',
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<IntlCountryPipeOptions, 'locale'> | null,
) {}
private readonly locale? = inject(INTL_LOCALES, { optional: true });
private readonly defaultOptions? = inject<Omit<
IntlCountryPipeOptions,
'locale'
> | null>(INTL_COUNTRY_PIPE_DEFAULT_OPTIONS, { optional: true });

transform(
value: string | null | undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
Expand All @@ -85,7 +93,6 @@ describe('IntlCurrencyPipe', () => {
it('should respect the setting from default config', () => {
TestBed.configureTestingModule({
providers: [
IntlCurrencyPipe,
{
provide: INTL_LOCALES,
useValue: 'en-US',
Expand All @@ -98,15 +105,14 @@ describe('IntlCurrencyPipe', () => {
},
],
});
testUnit = TestBed.inject(IntlCurrencyPipe);
TestBed.runInInjectionContext(() => (testUnit = new IntlCurrencyPipe()));

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

it('should give the user options a higher priority', () => {
TestBed.configureTestingModule({
providers: [
IntlCurrencyPipe,
{
provide: INTL_LOCALES,
useValue: 'en-US',
Expand All @@ -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',
Expand All @@ -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$',
Expand All @@ -147,7 +152,6 @@ describe('IntlCurrencyPipe', () => {
it('should not override the style option', () => {
TestBed.configureTestingModule({
providers: [
IntlCurrencyPipe,
{
provide: INTL_LOCALES,
useValue: 'en-US',
Expand All @@ -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');
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<IntlCurrencyPipeOptions, 'locale'> | null,
) {}
private readonly locale? = inject(INTL_LOCALES, { optional: true });
private readonly defaultOptions? = inject<Omit<
IntlCurrencyPipeOptions,
'locale'
> | null>(INTL_CURRENCY_PIPE_DEFAULT_OPTIONS, { optional: true });

transform(
value: number | string | null | undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
Expand All @@ -97,7 +109,6 @@ describe('DatePipe', () => {
it('should respect the setting from default config', () => {
TestBed.configureTestingModule({
providers: [
IntlDatePipe,
{
provide: INTL_LOCALES,
useValue: 'en-US',
Expand All @@ -110,15 +121,14 @@ describe('DatePipe', () => {
},
],
});
testUnit = TestBed.inject(IntlDatePipe);
TestBed.runInInjectionContext(() => (testUnit = new IntlDatePipe()));

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

it('should give the user options a higher priority', () => {
TestBed.configureTestingModule({
providers: [
IntlDatePipe,
{
provide: INTL_LOCALES,
useValue: 'en-US',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
15 changes: 6 additions & 9 deletions projects/angular-ecmascript-intl/src/lib/date/intl-date.pipe.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,14 +11,11 @@ export type IntlDatePipeOptions = Partial<Intl.DateTimeFormatOptions> &
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<IntlDatePipeOptions, 'locale'> | null,
) {}
private readonly locale? = inject(INTL_LOCALES, { optional: true });
private readonly defaultOptions? = inject<Omit<
IntlDatePipeOptions,
'locale'
> | null>(INTL_DATE_PIPE_DEFAULT_OPTIONS, { optional: true });

transform(
value: string | number | Date | null | undefined,
Expand Down
Loading