-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d699cf
commit ab6a84b
Showing
12 changed files
with
265 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
core-web/libs/ui/src/lib/pipes/dot-timestamp-to-date/dot-timestamp-to-date.pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { createPipeFactory, mockProvider, SpectatorPipe } from '@ngneat/spectator'; | ||
|
||
import { DotFormatDateService } from '@dotcms/ui'; | ||
|
||
import { DotTimestampToDatePipe } from './dot-timestamp-to-date.pipe'; | ||
|
||
const TIMESTAMP_MOCK = 1698789866; | ||
const EXPECTED_DATE_MOCK = '10/29/2023, 12:43 PM'; | ||
describe('DotTimestampPipe ', () => { | ||
let spectator: SpectatorPipe<DotTimestampToDatePipe>; | ||
|
||
const createPipe = createPipeFactory({ | ||
pipe: DotTimestampToDatePipe, | ||
providers: [ | ||
DotFormatDateService, | ||
mockProvider(DotFormatDateService, { getDateFromTimestamp: () => EXPECTED_DATE_MOCK }) | ||
] | ||
}); | ||
|
||
it('should transform the timestamp using getDateFromTimestamp to date format', () => { | ||
spectator = createPipe(`<div>{{ timestamp | dotTimestampToDate }}</div>`, { | ||
hostProps: { | ||
TIMESTAMP_MOCK | ||
} | ||
}); | ||
|
||
expect(spectator.element).toHaveText(EXPECTED_DATE_MOCK); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
core-web/libs/ui/src/lib/pipes/dot-timestamp-to-date/dot-timestamp-to-date.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { inject, Pipe, PipeTransform } from '@angular/core'; | ||
|
||
import { DotFormatDateService } from '@dotcms/ui'; | ||
|
||
/** | ||
* Transforms a timestamp into a formatted date string based on the user's selected language at login | ||
* | ||
* @remarks | ||
* This pipe is a pure pipe, meaning it is only re-evaluated when the input value changes. | ||
* | ||
* @example | ||
* ```html | ||
* <p>{{ timestampValue | dotTimestampToDate }}</p> | ||
* ``` | ||
* | ||
* @param time - The timestamp to be transformed into a date string. | ||
* @param userDateFormatOptions - Optional. The formatting options for the date string. | ||
* @returns A formatted date string based on the provided timestamp and formatting options. | ||
*/ | ||
@Pipe({ | ||
name: 'dotTimestampToDate', | ||
standalone: true, | ||
pure: true | ||
}) | ||
export class DotTimestampToDatePipe implements PipeTransform { | ||
private dotFormatDateService: DotFormatDateService = inject(DotFormatDateService); | ||
transform(time: number, userDateFormatOptions?: Intl.DateTimeFormatOptions): string { | ||
return this.dotFormatDateService.getDateFromTimestamp(time, userDateFormatOptions); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
core-web/libs/ui/src/lib/services/dot-format-date-service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { createServiceFactory, mockProvider, SpectatorService, SpyObject } from '@ngneat/spectator'; | ||
import { of } from 'rxjs'; | ||
|
||
import { DotcmsConfigService, LoginService } from '@dotcms/dotcms-js'; | ||
import { DotFormatDateService } from '@dotcms/ui'; | ||
|
||
const INVALID_DATE_MSG = 'Invalid date'; | ||
const VALID_TIMESTAMP = 1701189800000; | ||
const WRONG_TIMESTAMP = 1651337877000000; | ||
const DateFormatOptions: Intl.DateTimeFormatOptions = { | ||
year: 'numeric', | ||
month: '2-digit', | ||
day: '2-digit', | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
hour12: true, | ||
timeZone: 'UTC' | ||
}; | ||
|
||
describe('DotFormatDateService', () => { | ||
let spectator: SpectatorService<DotFormatDateService>; | ||
let loginService: SpyObject<LoginService>; | ||
const createService = createServiceFactory({ | ||
service: DotFormatDateService, | ||
providers: [ | ||
mockProvider(DotcmsConfigService, { | ||
getSystemTimeZone: () => of('idk') | ||
}), | ||
mockProvider(LoginService) | ||
] | ||
}); | ||
|
||
beforeEach(() => { | ||
spectator = createService(); | ||
loginService = spectator.inject(LoginService); | ||
loginService.currentUserLanguageId = 'en-US'; | ||
}); | ||
|
||
describe('getDateFromTimestamp', () => { | ||
it('should return `Invalid date` when is not a timestamp', () => { | ||
expect(spectator.service.getDateFromTimestamp(WRONG_TIMESTAMP)).toContain( | ||
INVALID_DATE_MSG | ||
); | ||
}); | ||
|
||
it('should return a string date using timestamp using `currentUserLanguageId`with us-US', () => { | ||
const EXPECTED_DATE = '11/28/2023, 04:43 PM'; | ||
expect(spectator.service.getDateFromTimestamp(VALID_TIMESTAMP, DateFormatOptions)).toBe( | ||
EXPECTED_DATE | ||
); | ||
}); | ||
|
||
it('should return a string with correct date format using timestamp using `currentUserLanguageId` with es-ES', () => { | ||
const EXPECTED_DATE = '28/11/2023, 04:43 p. m.'; | ||
|
||
loginService.currentUserLanguageId = 'es-ES'; | ||
expect(spectator.service.getDateFromTimestamp(VALID_TIMESTAMP, DateFormatOptions)).toBe( | ||
EXPECTED_DATE | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.