Skip to content

Commit

Permalink
Merge pull request #210 from jensweigele/master
Browse files Browse the repository at this point in the history
TimeAgoPipe: Improved Text Update on locale change when not using MomentInput
  • Loading branch information
urish authored Feb 23, 2019
2 parents 163348e + dadf11a commit 430add1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/time-ago.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/time-ago.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 430add1

Please sign in to comment.