diff --git a/src/time-ago.pipe.spec.ts b/src/time-ago.pipe.spec.ts index 0a9f011..19ef28d 100644 --- a/src/time-ago.pipe.spec.ts +++ b/src/time-ago.pipe.spec.ts @@ -24,7 +24,10 @@ function fakeDate(defaultDate: string | number) { describe('TimeAgoPipe', () => { describe('#transform', () => { - beforeEach(() => jest.useFakeTimers()); + beforeEach(() => { + moment.locale('en-gb'); + jest.useFakeTimers(); + }); afterEach(() => { global.Date = _Date; @@ -83,6 +86,15 @@ describe('TimeAgoPipe', () => { expect(pipe.transform(new Date(0))).toBe('46 years ago'); }); + it('should update the text when using Date Objects and locale changes', () => { + const changeDetectorMock = { markForCheck: jest.fn() }; + const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone); + fakeDate('2016-05-01'); + expect(pipe.transform(new Date(0))).toBe('46 years ago'); + moment.locale('de'); + expect(pipe.transform(new Date(0))).toBe('vor 46 Jahren'); + }); + it('should update the text when the date instance time is updated', () => { const changeDetectorMock = { markForCheck: jest.fn() }; const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone); diff --git a/src/time-ago.pipe.ts b/src/time-ago.pipe.ts index d0d4840..fc60c20 100644 --- a/src/time-ago.pipe.ts +++ b/src/time-ago.pipe.ts @@ -98,6 +98,6 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { } private getLocale(value: moment.MomentInput): string | null { - return moment.isMoment(value) ? value.locale() : null; + return moment.isMoment(value) ? value.locale() : moment.locale(); } }