diff --git a/src/app/components/toolbar/toggle-edit-offline/toggle-edit-offline.component.spec.ts b/src/app/components/toolbar/toggle-edit-offline/toggle-edit-offline.component.spec.ts index babbc0f80..0980eb970 100644 --- a/src/app/components/toolbar/toggle-edit-offline/toggle-edit-offline.component.spec.ts +++ b/src/app/components/toolbar/toggle-edit-offline/toggle-edit-offline.component.spec.ts @@ -24,9 +24,8 @@ */ import { ToggleEditOfflineComponent } from './toggle-edit-offline.component'; -import { LockNodeDirective } from '../../../directives/lock-node.directive'; -import { setupTestBed, CoreModule } from '@alfresco/adf-core'; -import { TestBed } from '@angular/core/testing'; +import { CoreModule } from '@alfresco/adf-core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; import { of } from 'rxjs'; import { Store } from '@ngrx/store'; import { NodeEntry } from '@alfresco/js-api'; @@ -34,38 +33,38 @@ import { DownloadNodesAction, EditOfflineAction, SnackbarErrorAction } from '@al import { TranslateModule } from '@ngx-translate/core'; describe('ToggleEditOfflineComponent', () => { - let fixture; - let component; - let store; - let dispatchSpy; - let selectSpy; - const selection = { file: { entry: { name: 'test', properties: {} } } }; - - setupTestBed({ - imports: [TranslateModule.forRoot(), CoreModule.forRoot()], - declarations: [ToggleEditOfflineComponent, LockNodeDirective], - providers: [ - { - provide: Store, - useValue: { - select: () => {}, - dispatch: () => {} - } - } - ] - }); + let fixture: ComponentFixture; + let component: ToggleEditOfflineComponent; + let store: Store; + let dispatchSpy: jasmine.Spy; + let selectSpy: jasmine.Spy; + let selection: any; beforeEach(() => { + TestBed.configureTestingModule({ + imports: [TranslateModule.forRoot(), CoreModule.forRoot()], + declarations: [ToggleEditOfflineComponent], + providers: [ + { + provide: Store, + useValue: { + select: () => {}, + dispatch: () => {} + } + } + ] + }); + fixture = TestBed.createComponent(ToggleEditOfflineComponent); component = fixture.componentInstance; + spyOn(component, 'unlockNode').and.returnValue(Promise.resolve(null)); + spyOn(component, 'lockNode').and.returnValue(Promise.resolve(null)); store = TestBed.inject(Store); dispatchSpy = spyOn(store, 'dispatch'); selectSpy = spyOn(store, 'select'); - }); - afterEach(() => { - dispatchSpy.calls.reset(); + selection = { file: { entry: { name: 'test', properties: {}, isLocked: false } } }; }); it('should initialized with data from store', () => { @@ -73,15 +72,15 @@ describe('ToggleEditOfflineComponent', () => { fixture.detectChanges(); - expect(component.selection).toEqual(selection.file); + expect(component.selection).toEqual(selection.file as any); }); - it('should download content if node is locked', () => { + it('should download content when node is locked', async () => { selectSpy.and.returnValue(of(selection)); fixture.detectChanges(); - const isLocked = true; - component.onToggleEvent(isLocked); + selection.file.entry.isLocked = false; + await component.onClick(); fixture.detectChanges(); @@ -90,23 +89,21 @@ describe('ToggleEditOfflineComponent', () => { it('should not download content if node is not locked', () => { selectSpy.and.returnValue(of(selection)); + fixture.detectChanges(); - - const isLocked = false; - component.onToggleEvent(isLocked); - + component.onClick(); fixture.detectChanges(); expect(dispatchSpy.calls.argsFor(0)).not.toEqual([new DownloadNodesAction([selection.file as NodeEntry])]); }); - it('should dispatch EditOfflineAction action', () => { + it('should dispatch EditOfflineAction action', async () => { selectSpy.and.returnValue(of(selection)); + + selection.file.entry.isLocked = true; + fixture.detectChanges(); - - const isLocked = false; - component.onToggleEvent(isLocked); - + await component.onClick(); fixture.detectChanges(); expect(dispatchSpy.calls.argsFor(0)).toEqual([new EditOfflineAction(selection.file as NodeEntry)]); @@ -114,8 +111,8 @@ describe('ToggleEditOfflineComponent', () => { it('should raise notification on lock error', () => { selectSpy.and.returnValue(of(selection)); - fixture.detectChanges(); + fixture.detectChanges(); component.onLockError(); fixture.detectChanges(); @@ -128,9 +125,9 @@ describe('ToggleEditOfflineComponent', () => { it('should raise notification on unlock error', () => { selectSpy.and.returnValue(of(selection)); - fixture.detectChanges(); - component.onUnlockLockError(); + fixture.detectChanges(); + component.onUnlockError(); fixture.detectChanges(); expect(dispatchSpy.calls.argsFor(0)).toEqual([ diff --git a/src/app/components/toolbar/toggle-edit-offline/toggle-edit-offline.component.ts b/src/app/components/toolbar/toggle-edit-offline/toggle-edit-offline.component.ts index a4982929e..e6af3319e 100644 --- a/src/app/components/toolbar/toggle-edit-offline/toggle-edit-offline.component.ts +++ b/src/app/components/toolbar/toggle-edit-offline/toggle-edit-offline.component.ts @@ -24,28 +24,26 @@ */ import { AppStore, DownloadNodesAction, EditOfflineAction, SnackbarErrorAction, getAppSelection } from '@alfresco/aca-shared/store'; -import { MinimalNodeEntity } from '@alfresco/js-api'; +import { MinimalNodeEntity, NodeEntry, SharedLinkEntry, Node } from '@alfresco/js-api'; import { Component, OnInit, ViewEncapsulation } from '@angular/core'; import { Store } from '@ngrx/store'; +import { isLocked } from '@alfresco/aca-shared'; +import { AlfrescoApiService } from '@alfresco/adf-core'; @Component({ selector: 'app-toggle-edit-offline', template: ` ` -}) -class TestComponent { - @ViewChild('lock') - directive: LockNodeDirective; - - selection = null; -} - -describe('LockNodeDirective', () => { - let fixture; - let api; - let component; - - setupTestBed({ - imports: [TranslateModule.forRoot(), CoreModule.forRoot(), AppTestingModule], - declarations: [TestComponent, LockNodeDirective] - }); - - beforeEach(() => { - fixture = TestBed.createComponent(TestComponent); - component = fixture.componentInstance; - component.selection = null; - api = TestBed.inject(AlfrescoApiService); - }); - - it('should return false if selection is null', () => { - component.selection = null; - fixture.detectChanges(); - expect(component.directive.isNodeLocked()).toBe(false); - }); - - it('should return false if selection is not locked', () => { - component.selection = { entry: { name: 'test-name', properties: {} } }; - fixture.detectChanges(); - expect(component.directive.isNodeLocked()).toBe(false); - }); - - it('should return true if selection is locked', () => { - component.selection = { - entry: { - name: 'test-name', - properties: { 'cm:lockType': 'WRITE_LOCK' } - } - }; - - fixture.detectChanges(); - expect(component.directive.isNodeLocked()).toBe(true); - }); - - it('should lock selection', fakeAsync(() => { - component.selection = { - entry: { - id: 'id', - name: 'test-name', - properties: {} - } - }; - - spyOn(api.nodesApi, 'lockNode').and.returnValue( - Promise.resolve({ - entry: { properties: { 'cm:lockType': 'WRITE_LOCK' } } - }) - ); - - fixture.detectChanges(); - - component.directive.onClick(); - tick(); - fixture.detectChanges(); - - expect(component.selection.entry.properties['cm:lockType']).toBe('WRITE_LOCK'); - })); - - it('should unlock selection', fakeAsync(() => { - component.selection = { - entry: { - id: 'id', - name: 'test-name', - properties: { - 'cm:lockType': 'WRITE_LOCK' - } - } - }; - - spyOn(api.nodesApi, 'unlockNode').and.returnValue( - Promise.resolve({ - entry: { properties: {} } - }) - ); - - fixture.detectChanges(); - component.directive.onClick(); - - tick(); - fixture.detectChanges(); - - expect(component.selection.entry.properties['cm:lockType']).toBe(undefined); - })); -}); diff --git a/src/app/directives/lock-node.directive.ts b/src/app/directives/lock-node.directive.ts deleted file mode 100644 index 2740801c2..000000000 --- a/src/app/directives/lock-node.directive.ts +++ /dev/null @@ -1,96 +0,0 @@ -/*! - * @license - * Alfresco Example Content Application - * - * Copyright (C) 2005 - 2020 Alfresco Software Limited - * - * This file is part of the Alfresco Example Content Application. - * If the software was purchased under a paid Alfresco license, the terms of - * the paid license agreement will prevail. Otherwise, the software is - * provided under the following open source license terms: - * - * The Alfresco Example Content Application is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * The Alfresco Example Content Application is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - */ - -import { Directive, EventEmitter, HostListener, Input, Output } from '@angular/core'; -import { NodeEntry, SharedLinkEntry } from '@alfresco/js-api'; -import { AlfrescoApiService } from '@alfresco/adf-core'; -import { isLocked } from '@alfresco/aca-shared'; - -@Directive({ - selector: '[acaLockNode]', - exportAs: 'lockNode' -}) -export class LockNodeDirective { - @Input('acaLockNode') - node: NodeEntry = null; - - @Output() toggle: EventEmitter = new EventEmitter(); - @Output() lockError: EventEmitter = new EventEmitter(); - @Output() unlockError: EventEmitter = new EventEmitter(); - - @HostListener('click') - onClick() { - this.toggleLock(this.node); - } - - constructor(private alfrescoApiService: AlfrescoApiService) {} - - isNodeLocked(): boolean { - return !!(this.node && isLocked(this.node)); - } - - private async toggleLock(node: NodeEntry | SharedLinkEntry) { - const id = (node as SharedLinkEntry).entry.nodeId || node.entry.id; - - if (isLocked(this.node)) { - try { - const response = await this.unlockNode(id); - - this.update(response.entry); - this.toggle.emit(false); - } catch (error) { - this.unlockError.emit(error); - } - } else { - try { - const response = await this.lockNode(id); - - this.update(response.entry); - this.toggle.emit(true); - } catch (error) { - this.lockError.emit(error); - } - } - } - - private lockNode(nodeId: string) { - return this.alfrescoApiService.nodesApi.lockNode(nodeId, { - type: 'ALLOW_OWNER_CHANGES', - lifetime: 'PERSISTENT' - }); - } - - private unlockNode(nodeId: string) { - return this.alfrescoApiService.nodesApi.unlockNode(nodeId); - } - - private update(data) { - const properties = this.node.entry.properties || {}; - - properties['cm:lockLifetime'] = data.properties['cm:lockLifetime']; - properties['cm:lockOwner'] = data.properties['cm:lockOwner']; - properties['cm:lockType'] = data.properties['cm:lockType']; - } -}