mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
migrate node lock dialog to date-fns (#8970)
* migrate node lock dialog to date-fns * cleanup
This commit is contained in:
@@ -15,19 +15,19 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import moment from 'moment';
|
|
||||||
|
|
||||||
import { TestBed, fakeAsync, tick, ComponentFixture } from '@angular/core/testing';
|
import { TestBed, fakeAsync, tick, ComponentFixture } from '@angular/core/testing';
|
||||||
import { MatDialogRef } from '@angular/material/dialog';
|
import { MatDialogRef } from '@angular/material/dialog';
|
||||||
import { NodeLockDialogComponent } from './node-lock.dialog';
|
import { NodeLockDialogComponent } from './node-lock.dialog';
|
||||||
import { ContentTestingModule } from '../testing/content.testing.module';
|
import { ContentTestingModule } from '../testing/content.testing.module';
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
|
import { addMinutes } from 'date-fns';
|
||||||
|
|
||||||
describe('NodeLockDialogComponent', () => {
|
describe('NodeLockDialogComponent', () => {
|
||||||
|
|
||||||
let fixture: ComponentFixture<NodeLockDialogComponent>;
|
let fixture: ComponentFixture<NodeLockDialogComponent>;
|
||||||
let component: NodeLockDialogComponent;
|
let component: NodeLockDialogComponent;
|
||||||
let expiryDate;
|
let expiryDate: Date;
|
||||||
|
|
||||||
const dialogRef = {
|
const dialogRef = {
|
||||||
close: jasmine.createSpy('close')
|
close: jasmine.createSpy('close')
|
||||||
};
|
};
|
||||||
@@ -54,7 +54,7 @@ describe('NodeLockDialogComponent', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jasmine.clock().mockDate(new Date());
|
jasmine.clock().mockDate(new Date());
|
||||||
expiryDate = moment().add(1, 'minutes');
|
expiryDate = addMinutes(new Date(), 1);
|
||||||
|
|
||||||
component.data = {
|
component.data = {
|
||||||
node: {
|
node: {
|
||||||
@@ -77,25 +77,12 @@ describe('NodeLockDialogComponent', () => {
|
|||||||
expect(component.form.value.isLocked).toBe(true);
|
expect(component.form.value.isLocked).toBe(true);
|
||||||
expect(component.form.value.allowOwner).toBe(true);
|
expect(component.form.value.allowOwner).toBe(true);
|
||||||
expect(component.form.value.isTimeLock).toBe(true);
|
expect(component.form.value.isTimeLock).toBe(true);
|
||||||
expect(component.form.value.time.format()).toBe(expiryDate.format());
|
expect(component.form.value.time).toEqual(expiryDate);
|
||||||
});
|
|
||||||
|
|
||||||
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());
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call dialog to close with form data when submit is successfully', fakeAsync(() => {
|
it('should call dialog to close with form data when submit is successfully', fakeAsync(() => {
|
||||||
const node: any = { entry: {} };
|
const node: any = { entry: {} };
|
||||||
spyOn(component['nodesApi'], 'lockNode').and.returnValue(Promise.resolve(node));
|
spyOn(component.nodesApi, 'lockNode').and.returnValue(Promise.resolve(node));
|
||||||
|
|
||||||
component.submit();
|
component.submit();
|
||||||
tick();
|
tick();
|
||||||
@@ -105,7 +92,7 @@ describe('NodeLockDialogComponent', () => {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
it('should call onError if submit fails', fakeAsync(() => {
|
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');
|
spyOn(component.data, 'onError');
|
||||||
|
|
||||||
component.submit();
|
component.submit();
|
||||||
|
@@ -15,12 +15,10 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import moment from 'moment';
|
|
||||||
|
|
||||||
import { Component, Inject, OnInit, Optional, ViewEncapsulation } from '@angular/core';
|
import { Component, Inject, OnInit, Optional, ViewEncapsulation } from '@angular/core';
|
||||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
||||||
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
|
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
|
||||||
|
import { differenceInSeconds } from 'date-fns';
|
||||||
import { NodeBodyLock, Node, NodeEntry, NodesApi } from '@alfresco/js-api';
|
import { NodeBodyLock, Node, NodeEntry, NodesApi } from '@alfresco/js-api';
|
||||||
import { AlfrescoApiService } from '@alfresco/adf-core';
|
import { AlfrescoApiService } from '@alfresco/adf-core';
|
||||||
|
|
||||||
@@ -35,7 +33,7 @@ export class NodeLockDialogComponent implements OnInit {
|
|||||||
node: Node = null;
|
node: Node = null;
|
||||||
nodeName: string;
|
nodeName: string;
|
||||||
|
|
||||||
_nodesApi: NodesApi;
|
private _nodesApi: NodesApi;
|
||||||
get nodesApi(): NodesApi {
|
get nodesApi(): NodesApi {
|
||||||
this._nodesApi = this._nodesApi ?? new NodesApi(this.alfrescoApi.getInstance());
|
this._nodesApi = this._nodesApi ?? new NodesApi(this.alfrescoApi.getInstance());
|
||||||
return this._nodesApi;
|
return this._nodesApi;
|
||||||
@@ -55,18 +53,20 @@ export class NodeLockDialogComponent implements OnInit {
|
|||||||
const { node } = this.data;
|
const { node } = this.data;
|
||||||
this.nodeName = node.name;
|
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({
|
this.form = this.formBuilder.group({
|
||||||
isLocked: node.isLocked || false,
|
isLocked: node.isLocked || false,
|
||||||
allowOwner: node.properties['cm:lockType'] === 'WRITE_LOCK',
|
allowOwner: node.properties['cm:lockType'] === 'WRITE_LOCK',
|
||||||
isTimeLock: !!node.properties['cm:expiryDate'],
|
isTimeLock,
|
||||||
time: node.properties['cm:expiryDate'] ? moment(node.properties['cm:expiryDate']) : moment()
|
time
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private get lockTimeInSeconds(): number {
|
private get lockTimeInSeconds(): number {
|
||||||
if (this.form.value.isTimeLock) {
|
if (this.form.value.isTimeLock) {
|
||||||
const duration = moment.duration(moment(this.form.value.time).diff(moment()));
|
return differenceInSeconds(new Date(this.form.value.time), Date.now());
|
||||||
return duration.asSeconds();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Reference in New Issue
Block a user