Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programming exercises: Calculate submission time difference with server time #10123

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/webapp/app/shared/pipes/artemis-time-ago.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChangeDetectorRef, NgZone, OnDestroy, Pipe, PipeTransform } from '@angu
import dayjs from 'dayjs/esm';
import { isDate } from 'app/shared/util/utils';
import { TranslateService } from '@ngx-translate/core';
import { ArtemisServerDateService } from 'app/shared/server-date.service';

@Pipe({
name: 'artemisTimeAgo',
Expand All @@ -21,10 +22,11 @@ export class ArtemisTimeAgoPipe implements PipeTransform, OnDestroy {
private cdRef: ChangeDetectorRef,
private ngZone: NgZone,
private translateService: TranslateService,
private serverDateService: ArtemisServerDateService,
) {}

format(date: dayjs.Dayjs) {
return date.locale(this.lastLocale).from(dayjs(), this.lastOmitSuffix);
return date.locale(this.lastLocale).from(this.serverDateService.now(), this.lastOmitSuffix);
}

transform(value: dayjs.ConfigType, omitSuffix?: boolean, formatFn?: (m: dayjs.Dayjs) => string): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import dayjs from 'dayjs/esm';

export class MockArtemisServerDateService {
now(): dayjs.Dayjs {
return dayjs();
}
}
11 changes: 9 additions & 2 deletions src/test/javascript/spec/pipe/artemis-time-ago.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,33 @@ import { ArtemisTimeAgoPipe } from 'app/shared/pipes/artemis-time-ago.pipe';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { TranslateService } from '@ngx-translate/core';
import { ChangeDetectorRef, NgZone } from '@angular/core';
import { ArtemisServerDateService } from 'app/shared/server-date.service';
import dayjs from 'dayjs/esm';
import { MockArtemisServerDateService } from '../helpers/mocks/service/mock-artemis-server-date.service';

describe('ArtemisTimeAgoPipe', () => {
let pipe: ArtemisTimeAgoPipe;
let translateService: TranslateService;
let cdRef: ChangeDetectorRef;
let serverTimeService: ArtemisServerDateService;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ArtemisTimeAgoPipe],
providers: [{ provide: TranslateService, useClass: MockTranslateService }],
providers: [
{ provide: TranslateService, useClass: MockTranslateService },
{ provide: ArtemisServerDateService, useClass: MockArtemisServerDateService },
],
})
.compileComponents()
.then(() => {
cdRef = { markForCheck: jest.fn() } as any as ChangeDetectorRef;

translateService = TestBed.inject(TranslateService);
translateService.use('en');
serverTimeService = TestBed.inject(ArtemisServerDateService);
const ngZone = TestBed.inject(NgZone);
pipe = new ArtemisTimeAgoPipe(cdRef, ngZone, translateService);
pipe = new ArtemisTimeAgoPipe(cdRef, ngZone, translateService, serverTimeService);
});
});

Expand Down
Loading