Skip to content

Commit 135ed4d

Browse files
committed
fix(#635) Locale pipes optimizations unit tests
1 parent 0c7f383 commit 135ed4d

6 files changed

+77
-3
lines changed

libs/transloco-locale/src/lib/pipes/transloco-date.pipe.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
OnDestroy,
77
} from '@angular/core';
88
import { isNil } from '@ngneat/transloco';
9+
import { isDate } from '../helpers';
910
import { getDefaultOptions } from '../shared';
1011
import { LOCALE_CONFIG } from '../transloco-locale.config';
1112
import { TranslocoLocaleService } from '../transloco-locale.service';
@@ -61,8 +62,8 @@ export class TranslocoDatePipe
6162
return this.getComparableDate(this.lastValue) === this.getComparableDate(value);
6263
}
6364

64-
private getComparableDate(value?: any) {
65-
return value?.getTime ? value.getTime() : value;
65+
private getComparableDate(value?: ValidDate) {
66+
return isDate(value) ? (value as Date).getTime() : value;
6667
}
6768

6869
ngOnDestroy(): void {

libs/transloco-locale/src/lib/pipes/transloco-locale.pipe.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export abstract class TranslocoLocalePipe<VALUE = unknown, ARGS extends unknown[
4444
return JSON.stringify(args) === this.lastArgs;
4545
}
4646

47-
invalidate() {
47+
protected invalidate() {
4848
this.lastValue = undefined;
4949
this.lastArgs = undefined;
5050
this.lastResult = '';

libs/transloco-locale/src/lib/tests/pipes/transloco-currency.pipe.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ describe('TranslocoCurrencyPipe', () => {
6969
expect(call[1].maximumFractionDigits).toEqual(3);
7070
});
7171

72+
it('should return previous result with same config', () => {
73+
const config1 = { useGrouping: false, maximumFractionDigits: 3 };
74+
const first = pipe.transform('123', undefined, config1);
75+
76+
const call = (Intl.NumberFormat as any).calls.argsFor(0);
77+
expect(call[1].useGrouping).toBeFalsy();
78+
expect(call[1].maximumFractionDigits).toEqual(3);
79+
80+
const config2 = { useGrouping: false, maximumFractionDigits: 3 };
81+
(Intl.NumberFormat as any).calls.reset();
82+
const second = pipe.transform('123', undefined, config2);
83+
84+
expect(Intl.NumberFormat).not.toHaveBeenCalled();
85+
expect(second).toBe(first);
86+
});
87+
7288
it('should take number options from locale settings', () => {
7389
service = mockLocaleService('es-ES');
7490

libs/transloco-locale/src/lib/tests/pipes/transloco-date.pipe.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ describe('TranslocoDatePipe', () => {
3838
expect(call[1].timeStyle).toEqual('medium');
3939
});
4040

41+
it('should return previous result with same config', () => {
42+
spyOn(Intl, 'DateTimeFormat').and.callThrough();
43+
44+
const pipe = new TranslocoDatePipe(
45+
service,
46+
cdr,
47+
defaultConfig.localeConfig
48+
);
49+
const first = pipe.transform(date, { dateStyle: 'medium', timeStyle: 'medium' });
50+
51+
const call = (Intl.DateTimeFormat as any).calls.argsFor(0);
52+
expect(call[1].dateStyle).toEqual('medium');
53+
expect(call[1].timeStyle).toEqual('medium');
54+
55+
(Intl.DateTimeFormat as any).calls.reset();
56+
const second = pipe.transform(date, { dateStyle: 'medium', timeStyle: 'medium' });
57+
58+
expect(Intl.DateTimeFormat).not.toHaveBeenCalled();
59+
expect(second).toBe(first);
60+
});
61+
4162
it('should consider a global date config', () => {
4263
spyOn(Intl, 'DateTimeFormat').and.callThrough();
4364
const pipe = new TranslocoDatePipe(service, cdr, LOCALE_CONFIG_MOCK);

libs/transloco-locale/src/lib/tests/pipes/transloco-decimal.pipe.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ describe('TranslocoDecimalPipe', () => {
5656
expect(call[1].maximumFractionDigits).toEqual(3);
5757
});
5858

59+
it('should return previous result with same config', () => {
60+
spyOn(Intl, 'NumberFormat').and.callThrough();
61+
62+
const config1 = { useGrouping: false, maximumFractionDigits: 3 };
63+
const first = pipe.transform('123', config1);
64+
65+
const call = (Intl.NumberFormat as any).calls.argsFor(0);
66+
expect(call[1].useGrouping).toBeFalsy();
67+
expect(call[1].maximumFractionDigits).toEqual(3);
68+
69+
const config2 = { useGrouping: false, maximumFractionDigits: 3 };
70+
(Intl.NumberFormat as any).calls.reset();
71+
const second = pipe.transform('123', config2);
72+
73+
expect(Intl.NumberFormat).not.toHaveBeenCalled();
74+
expect(second).toBe(first);
75+
});
76+
5977
it('should handle none transformable values', () => {
6078
expect(pipe.transform(null as any)).toEqual('');
6179
expect(pipe.transform({} as any)).toEqual('');

libs/transloco-locale/src/lib/tests/pipes/transloco-percent.pipe.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,22 @@ describe('TranslocoPercentPipe', () => {
5151
expect(call[1].useGrouping).toBeTruthy();
5252
expect(call[1].maximumFractionDigits).toEqual(3);
5353
});
54+
55+
it('should return previous result with same config', () => {
56+
spyOn(Intl, 'NumberFormat').and.callThrough();
57+
58+
const config1 = { useGrouping: false, maximumFractionDigits: 3 };
59+
const first = pipe.transform('123', config1);
60+
61+
const call = (Intl.NumberFormat as any).calls.argsFor(0);
62+
expect(call[1].useGrouping).toBeFalsy();
63+
expect(call[1].maximumFractionDigits).toEqual(3);
64+
65+
const config2 = { useGrouping: false, maximumFractionDigits: 3 };
66+
(Intl.NumberFormat as any).calls.reset();
67+
const second = pipe.transform('123', config2);
68+
69+
expect(Intl.NumberFormat).not.toHaveBeenCalled();
70+
expect(second).toBe(first);
71+
});
5472
});

0 commit comments

Comments
 (0)