mirror of
https://github.com/Alfresco/alfresco-content-app.git
synced 2025-07-31 17:38:28 +00:00
[AAE-11496] Move 'content-plugin' to projects folder as 'aca-content' (#2817)
* [AAE-11496] Move content-plugin to projects * Fix unit test
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
/*!
|
||||
* @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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { ToggleEditOfflineComponent } from './toggle-edit-offline.component';
|
||||
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';
|
||||
import { DownloadNodesAction, EditOfflineAction, SnackbarErrorAction } from '@alfresco/aca-shared/store';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
|
||||
describe('ToggleEditOfflineComponent', () => {
|
||||
let fixture: ComponentFixture<ToggleEditOfflineComponent>;
|
||||
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');
|
||||
|
||||
selection = { file: { entry: { name: 'test', properties: {}, isLocked: false } } };
|
||||
});
|
||||
|
||||
it('should initialized with data from store', () => {
|
||||
selectSpy.and.returnValue(of(selection));
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(component.selection).toEqual(selection.file as any);
|
||||
});
|
||||
|
||||
it('should download content when node is locked', async () => {
|
||||
selectSpy.and.returnValue(of(selection));
|
||||
fixture.detectChanges();
|
||||
|
||||
selection.file.entry.isLocked = false;
|
||||
await component.onClick();
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(dispatchSpy.calls.argsFor(0)).toEqual([new DownloadNodesAction([selection.file as NodeEntry])]);
|
||||
});
|
||||
|
||||
it('should not download content if node is not locked', () => {
|
||||
selectSpy.and.returnValue(of(selection));
|
||||
|
||||
fixture.detectChanges();
|
||||
component.onClick();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(dispatchSpy.calls.argsFor(0)).not.toEqual([new DownloadNodesAction([selection.file as NodeEntry])]);
|
||||
});
|
||||
|
||||
it('should dispatch EditOfflineAction action', async () => {
|
||||
selectSpy.and.returnValue(of(selection));
|
||||
|
||||
selection.file.entry.isLocked = true;
|
||||
|
||||
fixture.detectChanges();
|
||||
await component.onClick();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(dispatchSpy.calls.argsFor(0)).toEqual([new EditOfflineAction(selection.file as NodeEntry)]);
|
||||
});
|
||||
|
||||
it('should raise notification on lock error', () => {
|
||||
selectSpy.and.returnValue(of(selection));
|
||||
|
||||
fixture.detectChanges();
|
||||
component.onLockError();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(dispatchSpy.calls.argsFor(0)).toEqual([
|
||||
new SnackbarErrorAction('APP.MESSAGES.ERRORS.LOCK_NODE', {
|
||||
fileName: 'test'
|
||||
})
|
||||
]);
|
||||
});
|
||||
|
||||
it('should raise notification on unlock error', () => {
|
||||
selectSpy.and.returnValue(of(selection));
|
||||
|
||||
fixture.detectChanges();
|
||||
component.onUnlockError();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(dispatchSpy.calls.argsFor(0)).toEqual([
|
||||
new SnackbarErrorAction('APP.MESSAGES.ERRORS.UNLOCK_NODE', {
|
||||
fileName: 'test'
|
||||
})
|
||||
]);
|
||||
});
|
||||
});
|
@@ -0,0 +1,140 @@
|
||||
/*!
|
||||
* @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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { AppStore, DownloadNodesAction, EditOfflineAction, SnackbarErrorAction, getAppSelection } from '@alfresco/aca-shared/store';
|
||||
import { MinimalNodeEntity, NodeEntry, SharedLinkEntry, Node, NodesApi } 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: `
|
||||
<button
|
||||
mat-menu-item
|
||||
[attr.title]="(isNodeLocked ? 'APP.ACTIONS.EDIT_OFFLINE_CANCEL' : 'APP.ACTIONS.EDIT_OFFLINE') | translate"
|
||||
(click)="onClick()"
|
||||
>
|
||||
<ng-container *ngIf="isNodeLocked">
|
||||
<mat-icon>cancel</mat-icon>
|
||||
<span>{{ 'APP.ACTIONS.EDIT_OFFLINE_CANCEL' | translate }}</span>
|
||||
</ng-container>
|
||||
|
||||
<ng-container *ngIf="!isNodeLocked">
|
||||
<mat-icon>edit</mat-icon>
|
||||
<span>{{ 'APP.ACTIONS.EDIT_OFFLINE' | translate }}</span>
|
||||
</ng-container>
|
||||
</button>
|
||||
`,
|
||||
encapsulation: ViewEncapsulation.None,
|
||||
host: { class: 'app-toggle-edit-offline' }
|
||||
})
|
||||
export class ToggleEditOfflineComponent implements OnInit {
|
||||
private nodesApi: NodesApi;
|
||||
selection: MinimalNodeEntity;
|
||||
|
||||
constructor(private store: Store<AppStore>, private alfrescoApiService: AlfrescoApiService) {
|
||||
this.nodesApi = new NodesApi(this.alfrescoApiService.getInstance());
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.store.select(getAppSelection).subscribe(({ file }) => {
|
||||
this.selection = file;
|
||||
});
|
||||
}
|
||||
|
||||
get isNodeLocked(): boolean {
|
||||
return !!(this.selection && isLocked(this.selection));
|
||||
}
|
||||
|
||||
async onClick() {
|
||||
await this.toggleLock(this.selection);
|
||||
}
|
||||
|
||||
private async toggleLock(node: NodeEntry | SharedLinkEntry) {
|
||||
const id = (node as SharedLinkEntry).entry.nodeId || node.entry.id;
|
||||
|
||||
if (isLocked(this.selection)) {
|
||||
try {
|
||||
const response = await this.unlockNode(id);
|
||||
|
||||
this.update(response?.entry);
|
||||
this.store.dispatch(new EditOfflineAction(this.selection));
|
||||
} catch {
|
||||
this.onUnlockError();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
const response = await this.lockNode(id);
|
||||
|
||||
this.update(response?.entry);
|
||||
this.store.dispatch(new DownloadNodesAction([this.selection]));
|
||||
this.store.dispatch(new EditOfflineAction(this.selection));
|
||||
} catch {
|
||||
this.onLockError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onLockError() {
|
||||
this.store.dispatch(
|
||||
new SnackbarErrorAction('APP.MESSAGES.ERRORS.LOCK_NODE', {
|
||||
fileName: this.selection.entry.name
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
onUnlockError() {
|
||||
this.store.dispatch(
|
||||
new SnackbarErrorAction('APP.MESSAGES.ERRORS.UNLOCK_NODE', {
|
||||
fileName: this.selection.entry.name
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
lockNode(nodeId: string) {
|
||||
return this.nodesApi.lockNode(nodeId, {
|
||||
type: 'ALLOW_OWNER_CHANGES',
|
||||
lifetime: 'PERSISTENT'
|
||||
});
|
||||
}
|
||||
|
||||
unlockNode(nodeId: string) {
|
||||
return this.nodesApi.unlockNode(nodeId);
|
||||
}
|
||||
|
||||
private update(data: Node) {
|
||||
if (data && data.properties) {
|
||||
const properties = this.selection.entry.properties || {};
|
||||
|
||||
properties['cm:lockLifetime'] = data.properties['cm:lockLifetime'];
|
||||
properties['cm:lockOwner'] = data.properties['cm:lockOwner'];
|
||||
properties['cm:lockType'] = data.properties['cm:lockType'];
|
||||
|
||||
this.selection.entry.properties = properties;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user