Skip to content

Commit

Permalink
#26399 include in 23.10.24 LTS
Browse files Browse the repository at this point in the history
  • Loading branch information
erickgonzalez committed Nov 4, 2024
1 parent 6d699cf commit ab6a84b
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 24 deletions.
7 changes: 6 additions & 1 deletion core-web/libs/dotcms-js/src/lib/core/login.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export const LOGOUT_URL = '/dotAdmin/logout';
* This Service get the server configuration to display in the login component
* and execute the login and forgot password routines
*/
@Injectable()
@Injectable({
providedIn: 'root'
})
export class LoginService {
currentUserLanguageId = '';
private country = '';
private lang = '';
private urls: Record<string, string>;
Expand Down Expand Up @@ -327,6 +330,8 @@ export class LoginService {
this._auth = this.getFullAuth(auth);
this._auth$.next(this.getFullAuth(auth));

this.currentUserLanguageId = auth.user.languageId;

// When not logged user we need to fire the observable chain
if (!auth.user) {
this._logout$.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,24 @@
{{ experiment.name }}
</td>
<td data-testId="experiment-row__createdDate">
{{ experiment.creationDate | dotRelativeDate }}
{{ experiment.creationDate | dotTimestampToDate }}
</td>
<td data-testId="experiment-row__modDate">
{{ experiment.modDate | dotRelativeDate }}
{{ experiment.modDate | dotTimestampToDate }}
</td>
<td class="text-right" data-testId="experiment-row__actions">
<p-menu
#menu
[model]="experiment.actionsItemsMenu"
[popup]="true"
appendTo="body"
></p-menu>
appendTo="body"></p-menu>
<button
class="p-button-rounded p-button-text"
(click)="menu.toggle($event); $event.stopPropagation()"
data-testId="experiment-row__action-button"
icon="pi pi-ellipsis-v"
pButton
type="button"
></button>
type="button"></button>
</td>
</tr>
</ng-template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import { Menu, MenuItemContent } from 'primeng/menu';
import { Table } from 'primeng/table';

import { DotMessageService } from '@dotcms/data-access';
import { LoginService } from '@dotcms/dotcms-js';
import { DotExperimentStatus, GroupedExperimentByStatus } from '@dotcms/dotcms-models';
import { DotEmptyContainerComponent, DotFormatDateService } from '@dotcms/ui';
import {
DotEmptyContainerComponent,
DotFormatDateService,
DotTimestampToDatePipe
} from '@dotcms/ui';
import {
DotFormatDateServiceMock,
getExperimentMock,
Expand Down Expand Up @@ -77,6 +82,7 @@ describe('DotExperimentsListTableComponent', () => {

const createComponent = createComponentFactory({
component: DotExperimentsListTableComponent,
imports: [DotTimestampToDatePipe],
componentMocks: [ConfirmPopup],
declarations: [MockDatePipe],
providers: [
Expand All @@ -86,7 +92,11 @@ describe('DotExperimentsListTableComponent', () => {
},
MessageService,
ConfirmationService,
{ provide: DotFormatDateService, useClass: DotFormatDateServiceMock }
{ provide: DotFormatDateService, useClass: DotFormatDateServiceMock },
{
provide: LoginService,
useValue: { currentUserLanguageId: 'en-US' }
}
],
schemas: [NO_ERRORS_SCHEMA]
});
Expand Down Expand Up @@ -134,9 +144,11 @@ describe('DotExperimentsListTableComponent', () => {
DRAFT_EXPERIMENT_MOCK.name
);
expect(spectator.query(byTestId('experiment-row__createdDate'))).toHaveText(
'1 hour ago'
'10/10/2020 10:10 PM'
);
expect(spectator.query(byTestId('experiment-row__modDate'))).toHaveText(
'10/10/2020 10:10 PM'
);
expect(spectator.query(byTestId('experiment-row__modDate'))).toHaveText('1 hour ago');
});

it('should emit action when a row is clicked', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
DotEmptyContainerComponent,
DotMessagePipe,
DotRelativeDatePipe,
DotTimestampToDatePipe,
PrincipalConfiguration
} from '@dotcms/ui';

Expand All @@ -41,7 +42,8 @@ import {
ButtonModule,
TooltipModule,
MenuModule,
DotEmptyContainerComponent
DotEmptyContainerComponent,
DotTimestampToDatePipe
],
templateUrl: './dot-experiments-list-table.component.html',
styleUrls: ['./dot-experiments-list-table.component.scss'],
Expand All @@ -50,8 +52,10 @@ import {
})
export class DotExperimentsListTableComponent {
@Input() experimentGroupedByStatus: GroupedExperimentByStatus[] = [];

@Output()
goToContainer = new EventEmitter<DotExperiment>();

private dotMessageService: DotMessageService = inject(DotMessageService);
protected readonly emptyConfiguration: PrincipalConfiguration = {
title: this.dotMessageService.get('experimentspage.not.experiments.found.filtered'),
Expand Down
1 change: 1 addition & 0 deletions core-web/libs/ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from './lib/services/clipboard/ClipboardUtil';
export * from './lib/pipes/dot-relative-date/dot-relative-date.pipe';
export * from './lib/dot-message/dot-message.pipe';
export * from './lib/pipes/dot-string-format/dot-string-format.pipe';
export * from './lib/pipes/dot-timestamp-to-date/dot-timestamp-to-date.pipe';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fakeAsync, tick } from '@angular/core/testing';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';

import { DotcmsConfigService } from '@dotcms/dotcms-js';
import { DotcmsConfigService, LoginService } from '@dotcms/dotcms-js';
import { DotFormatDateService, DotRelativeDatePipe } from '@dotcms/ui';
import { DotcmsConfigServiceMock } from '@dotcms/utils-testing';

Expand All @@ -18,11 +18,23 @@ describe('DotRelativeDatePipe', () => {
let pipe: DotRelativeDatePipe;

beforeEach(() => {
formatDateService = new DotFormatDateService(
new DotcmsConfigServiceMock() as unknown as DotcmsConfigService
);

pipe = new DotRelativeDatePipe(formatDateService as unknown as DotFormatDateService);
TestBed.configureTestingModule({
providers: [
{
provide: LoginService,
useValue: { currentUserLanguageId: 'en-US' }
},
{
provide: DotcmsConfigService,
useClass: DotcmsConfigServiceMock
},
DotFormatDateService,
DotRelativeDatePipe
]
});

formatDateService = TestBed.inject(DotFormatDateService);
pipe = TestBed.inject(DotRelativeDatePipe);
});

describe('relative', () => {
Expand Down
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);
});
});
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 core-web/libs/ui/src/lib/services/dot-format-date-service.spec.ts
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
);
});
});
});
Loading

0 comments on commit ab6a84b

Please sign in to comment.