Skip to content

Commit

Permalink
feat(TsTimeAgoPipe): Compare date default to current date
Browse files Browse the repository at this point in the history
  • Loading branch information
atlwendy authored and benjamincharity committed May 12, 2020
1 parent 2e407e4 commit 6c192d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions projects/library/pipes/src/time-ago/time-ago.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,16 @@ describe(`TsTimeAgoPipe`, function() {
});
});
});

test(`should compare to current date if no comparedDate passed in`, () => {
const mockDate = new Date(2018, 1, 8);
const spy = jest
.spyOn(global, 'Date')
.mockImplementation(() => mockDate);
const actual = pipe(date.toISOString());
const expected = 'less than a minute';
expect(actual).toEqual(expected);
spy.mockRestore();
});
});

2 changes: 1 addition & 1 deletion projects/library/pipes/src/time-ago/time-ago.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { formatDistance } from 'date-fns';
*/
@Pipe({ name: 'tsTimeAgo' })
export class TsTimeAgoPipe implements PipeTransform {
public transform(value: string | Date, comparedDate: string | Date): string | undefined {
public transform(value: string | Date, comparedDate: string | Date = new Date()): string | undefined {
// Check for null values to avoid issues during data-binding
if (value == null || value === '') {
return undefined;
Expand Down

0 comments on commit 6c192d5

Please sign in to comment.