From a21e1222afcb929ece3a2ed4c0c604d712109f74 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Fri, 6 Oct 2023 08:37:49 +0100 Subject: [PATCH] migrate node lock dialog to date-fns --- .../src/lib/dialogs/node-lock.dialog.spec.ts | 30 ++++++------------- .../src/lib/dialogs/node-lock.dialog.ts | 16 +++++----- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/lib/content-services/src/lib/dialogs/node-lock.dialog.spec.ts b/lib/content-services/src/lib/dialogs/node-lock.dialog.spec.ts index 583b75adbd8..9da51df3429 100644 --- a/lib/content-services/src/lib/dialogs/node-lock.dialog.spec.ts +++ b/lib/content-services/src/lib/dialogs/node-lock.dialog.spec.ts @@ -15,19 +15,20 @@ * limitations under the License. */ -import moment from 'moment'; - import { TestBed, fakeAsync, tick, ComponentFixture } from '@angular/core/testing'; import { MatDialogRef } from '@angular/material/dialog'; import { NodeLockDialogComponent } from './node-lock.dialog'; import { ContentTestingModule } from '../testing/content.testing.module'; import { TranslateModule } from '@ngx-translate/core'; +import { addMinutes } from 'date-fns'; -describe('NodeLockDialogComponent', () => { +// eslint-disable-next-line ban/ban, @cspell/spellchecker +fdescribe('NodeLockDialogComponent', () => { let fixture: ComponentFixture; let component: NodeLockDialogComponent; - let expiryDate; + let expiryDate: Date; + const dialogRef = { close: jasmine.createSpy('close') }; @@ -54,7 +55,7 @@ describe('NodeLockDialogComponent', () => { beforeEach(() => { jasmine.clock().mockDate(new Date()); - expiryDate = moment().add(1, 'minutes'); + expiryDate = addMinutes(new Date(), 1); component.data = { node: { @@ -77,25 +78,12 @@ describe('NodeLockDialogComponent', () => { expect(component.form.value.isLocked).toBe(true); expect(component.form.value.allowOwner).toBe(true); expect(component.form.value.isTimeLock).toBe(true); - expect(component.form.value.time.format()).toBe(expiryDate.format()); - }); - - it('should update form inputs', () => { - const newTime = moment(); - component.form.controls['isLocked'].setValue(false); - component.form.controls['allowOwner'].setValue(false); - component.form.controls['isTimeLock'].setValue(false); - component.form.controls['time'].setValue(newTime); - - expect(component.form.value.isLocked).toBe(false); - expect(component.form.value.allowOwner).toBe(false); - expect(component.form.value.isTimeLock).toBe(false); - expect(component.form.value.time.format()).toBe(newTime.format()); + expect(component.form.value.time).toEqual(expiryDate); }); it('should call dialog to close with form data when submit is successfully', fakeAsync(() => { const node: any = { entry: {} }; - spyOn(component['nodesApi'], 'lockNode').and.returnValue(Promise.resolve(node)); + spyOn(component.nodesApi, 'lockNode').and.returnValue(Promise.resolve(node)); component.submit(); tick(); @@ -105,7 +93,7 @@ describe('NodeLockDialogComponent', () => { })); it('should call onError if submit fails', fakeAsync(() => { - spyOn(component['nodesApi'], 'lockNode').and.returnValue(Promise.reject('error')); + spyOn(component.nodesApi, 'lockNode').and.returnValue(Promise.reject('error')); spyOn(component.data, 'onError'); component.submit(); diff --git a/lib/content-services/src/lib/dialogs/node-lock.dialog.ts b/lib/content-services/src/lib/dialogs/node-lock.dialog.ts index dac7f34cd51..ae96f02566b 100644 --- a/lib/content-services/src/lib/dialogs/node-lock.dialog.ts +++ b/lib/content-services/src/lib/dialogs/node-lock.dialog.ts @@ -15,12 +15,10 @@ * limitations under the License. */ -import moment from 'moment'; - import { Component, Inject, OnInit, Optional, ViewEncapsulation } from '@angular/core'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; - +import { differenceInSeconds } from 'date-fns'; import { NodeBodyLock, Node, NodeEntry, NodesApi } from '@alfresco/js-api'; import { AlfrescoApiService } from '@alfresco/adf-core'; @@ -35,7 +33,7 @@ export class NodeLockDialogComponent implements OnInit { node: Node = null; nodeName: string; - _nodesApi: NodesApi; + private _nodesApi: NodesApi; get nodesApi(): NodesApi { this._nodesApi = this._nodesApi ?? new NodesApi(this.alfrescoApi.getInstance()); return this._nodesApi; @@ -55,18 +53,20 @@ export class NodeLockDialogComponent implements OnInit { const { node } = this.data; this.nodeName = node.name; + const isTimeLock = !!node.properties['cm:expiryDate']; + const time = isTimeLock ? new Date(node.properties['cm:expiryDate']) : new Date(); + this.form = this.formBuilder.group({ isLocked: node.isLocked || false, allowOwner: node.properties['cm:lockType'] === 'WRITE_LOCK', - isTimeLock: !!node.properties['cm:expiryDate'], - time: node.properties['cm:expiryDate'] ? moment(node.properties['cm:expiryDate']) : moment() + isTimeLock, + time }); } private get lockTimeInSeconds(): number { if (this.form.value.isTimeLock) { - const duration = moment.duration(moment(this.form.value.time).diff(moment())); - return duration.asSeconds(); + return differenceInSeconds(new Date(this.form.value.time), Date.now()); } return 0;