Skip to content

Commit

Permalink
fix(#635) Locale pipes optimizations unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ben12 committed Mar 19, 2023
1 parent 0c7f383 commit 135ed4d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
5 changes: 3 additions & 2 deletions libs/transloco-locale/src/lib/pipes/transloco-date.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
OnDestroy,
} from '@angular/core';
import { isNil } from '@ngneat/transloco';
import { isDate } from '../helpers';
import { getDefaultOptions } from '../shared';
import { LOCALE_CONFIG } from '../transloco-locale.config';
import { TranslocoLocaleService } from '../transloco-locale.service';
Expand Down Expand Up @@ -61,8 +62,8 @@ export class TranslocoDatePipe
return this.getComparableDate(this.lastValue) === this.getComparableDate(value);
}

private getComparableDate(value?: any) {
return value?.getTime ? value.getTime() : value;
private getComparableDate(value?: ValidDate) {
return isDate(value) ? (value as Date).getTime() : value;
}

ngOnDestroy(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export abstract class TranslocoLocalePipe<VALUE = unknown, ARGS extends unknown[
return JSON.stringify(args) === this.lastArgs;
}

invalidate() {
protected invalidate() {
this.lastValue = undefined;
this.lastArgs = undefined;
this.lastResult = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ describe('TranslocoCurrencyPipe', () => {
expect(call[1].maximumFractionDigits).toEqual(3);
});

it('should return previous result with same config', () => {
const config1 = { useGrouping: false, maximumFractionDigits: 3 };
const first = pipe.transform('123', undefined, config1);

const call = (Intl.NumberFormat as any).calls.argsFor(0);
expect(call[1].useGrouping).toBeFalsy();
expect(call[1].maximumFractionDigits).toEqual(3);

const config2 = { useGrouping: false, maximumFractionDigits: 3 };
(Intl.NumberFormat as any).calls.reset();
const second = pipe.transform('123', undefined, config2);

expect(Intl.NumberFormat).not.toHaveBeenCalled();
expect(second).toBe(first);
});

it('should take number options from locale settings', () => {
service = mockLocaleService('es-ES');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ describe('TranslocoDatePipe', () => {
expect(call[1].timeStyle).toEqual('medium');
});

it('should return previous result with same config', () => {
spyOn(Intl, 'DateTimeFormat').and.callThrough();

const pipe = new TranslocoDatePipe(
service,
cdr,
defaultConfig.localeConfig
);
const first = pipe.transform(date, { dateStyle: 'medium', timeStyle: 'medium' });

const call = (Intl.DateTimeFormat as any).calls.argsFor(0);
expect(call[1].dateStyle).toEqual('medium');
expect(call[1].timeStyle).toEqual('medium');

(Intl.DateTimeFormat as any).calls.reset();
const second = pipe.transform(date, { dateStyle: 'medium', timeStyle: 'medium' });

expect(Intl.DateTimeFormat).not.toHaveBeenCalled();
expect(second).toBe(first);
});

it('should consider a global date config', () => {
spyOn(Intl, 'DateTimeFormat').and.callThrough();
const pipe = new TranslocoDatePipe(service, cdr, LOCALE_CONFIG_MOCK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ describe('TranslocoDecimalPipe', () => {
expect(call[1].maximumFractionDigits).toEqual(3);
});

it('should return previous result with same config', () => {
spyOn(Intl, 'NumberFormat').and.callThrough();

const config1 = { useGrouping: false, maximumFractionDigits: 3 };
const first = pipe.transform('123', config1);

const call = (Intl.NumberFormat as any).calls.argsFor(0);
expect(call[1].useGrouping).toBeFalsy();
expect(call[1].maximumFractionDigits).toEqual(3);

const config2 = { useGrouping: false, maximumFractionDigits: 3 };
(Intl.NumberFormat as any).calls.reset();
const second = pipe.transform('123', config2);

expect(Intl.NumberFormat).not.toHaveBeenCalled();
expect(second).toBe(first);
});

it('should handle none transformable values', () => {
expect(pipe.transform(null as any)).toEqual('');
expect(pipe.transform({} as any)).toEqual('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,22 @@ describe('TranslocoPercentPipe', () => {
expect(call[1].useGrouping).toBeTruthy();
expect(call[1].maximumFractionDigits).toEqual(3);
});

it('should return previous result with same config', () => {
spyOn(Intl, 'NumberFormat').and.callThrough();

const config1 = { useGrouping: false, maximumFractionDigits: 3 };
const first = pipe.transform('123', config1);

const call = (Intl.NumberFormat as any).calls.argsFor(0);
expect(call[1].useGrouping).toBeFalsy();
expect(call[1].maximumFractionDigits).toEqual(3);

const config2 = { useGrouping: false, maximumFractionDigits: 3 };
(Intl.NumberFormat as any).calls.reset();
const second = pipe.transform('123', config2);

expect(Intl.NumberFormat).not.toHaveBeenCalled();
expect(second).toBe(first);
});
});

0 comments on commit 135ed4d

Please sign in to comment.