[ADF-2310] Error is thrown if isSelectionValid optional property is not defined (#2957)

* [ADF-2310] Error is thrown if isSelectionValid optional property is not defined

* [ADF-2310] added unit tests for the change and removed unneeded import

* fix tests

* [ADF-2310] add missing async

* [ADF-2310] add more async function to unit tests
This commit is contained in:
suzanadirla
2018-02-20 20:22:21 +02:00
committed by Eugenio Romano
parent 4513cb0e09
commit ed9289d6df
2 changed files with 194 additions and 74 deletions

View File

@@ -21,7 +21,6 @@ import { By } from '@angular/platform-browser';
import { MinimalNodeEntryEntity, SiteEntry, SitePaging } from 'alfresco-js-api'; import { MinimalNodeEntryEntity, SiteEntry, SitePaging } from 'alfresco-js-api';
import { import {
AlfrescoApiService, AlfrescoApiService,
ContentService,
TranslationService, TranslationService,
SearchService, SearchService,
SitesService, SitesService,
@@ -76,10 +75,6 @@ describe('ContentNodeSelectorComponent', () => {
_observer.next(result); _observer.next(result);
} }
function returnAlwaysTrue(entry: MinimalNodeEntryEntity) {
return true;
}
function setupTestbed(plusProviders) { function setupTestbed(plusProviders) {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
@@ -95,7 +90,6 @@ describe('ContentNodeSelectorComponent', () => {
], ],
providers: [ providers: [
AlfrescoApiService, AlfrescoApiService,
ContentService,
SearchService, SearchService,
TranslationService, TranslationService,
DocumentListService, DocumentListService,
@@ -215,9 +209,6 @@ describe('ContentNodeSelectorComponent', () => {
}); });
it('should show the breadcrumb for the selected node when search results are displayed', (done) => { it('should show the breadcrumb for the selected node when search results are displayed', (done) => {
const alfrescoContentService = TestBed.get(ContentService);
spyOn(alfrescoContentService, 'hasPermission').and.returnValue(true);
typeToSearchBox(); typeToSearchBox();
setTimeout(() => { setTimeout(() => {
@@ -238,9 +229,6 @@ describe('ContentNodeSelectorComponent', () => {
}); });
it('should NOT show the breadcrumb for the selected node when not on search results list', (done) => { it('should NOT show the breadcrumb for the selected node when not on search results list', (done) => {
const alfrescoContentService = TestBed.get(ContentService);
spyOn(alfrescoContentService, 'hasPermission').and.returnValue(true);
typeToSearchBox(); typeToSearchBox();
setTimeout(() => { setTimeout(() => {
@@ -327,6 +315,10 @@ describe('ContentNodeSelectorComponent', () => {
spyOn(documentListService, 'getFolderNode').and.returnValue(Promise.resolve(expectedDefaultFolderNode)); spyOn(documentListService, 'getFolderNode').and.returnValue(Promise.resolve(expectedDefaultFolderNode));
spyOn(component.documentList, 'loadFolderNodesByFolderNodeId').and.returnValue(Promise.resolve()); spyOn(component.documentList, 'loadFolderNodesByFolderNodeId').and.returnValue(Promise.resolve());
const sitesService = TestBed.get(SitesService);
spyOn(sitesService, 'getSites').and.returnValue(Observable.of({ list: { entries: [] } }));
getCorrespondingNodeIdsSpy = spyOn(component.documentList, 'getCorrespondingNodeIds').and getCorrespondingNodeIdsSpy = spyOn(component.documentList, 'getCorrespondingNodeIds').and
.callFake(id => { .callFake(id => {
if (id === '-sites-') { if (id === '-sites-') {
@@ -652,7 +644,7 @@ describe('ContentNodeSelectorComponent', () => {
}, 300); }, 300);
}); });
it('button\'s callback should load the next batch of results by calling the search api', () => { it('button\'s callback should load the next batch of results by calling the search api', async(() => {
const skipCount = 8; const skipCount = 8;
component.searchTerm = 'kakarot'; component.searchTerm = 'kakarot';
@@ -661,7 +653,7 @@ describe('ContentNodeSelectorComponent', () => {
fixture.whenStable().then(() => { fixture.whenStable().then(() => {
expect(searchSpy).toHaveBeenCalledWith(defaultSearchOptions('kakarot', undefined, skipCount)); expect(searchSpy).toHaveBeenCalledWith(defaultSearchOptions('kakarot', undefined, skipCount));
}); });
}); }));
it('should be shown when pagination\'s hasMoreItems is true', () => { it('should be shown when pagination\'s hasMoreItems is true', () => {
component.pagination = { component.pagination = {
@@ -729,95 +721,222 @@ describe('ContentNodeSelectorComponent', () => {
}); });
}); });
describe('Action button for the chosen node', () => { describe('Chosen node', () => {
const entry: MinimalNodeEntryEntity = <MinimalNodeEntryEntity> { list: {entries: [{}]}}; const entry: MinimalNodeEntryEntity = <MinimalNodeEntryEntity> {};
const nodePage: NodePaging = <NodePaging> {list: {}, pagination: {}}; const nodePage: NodePaging = <NodePaging> {list: {}, pagination: {}};
let hasPermission; let hasPermission;
function returnHasPermission(): boolean {
return hasPermission;
}
beforeEach(() => { beforeEach(() => {
const alfrescoContentService = TestBed.get(ContentService); const sitesService = TestBed.get(SitesService);
spyOn(alfrescoContentService, 'hasPermission').and.callFake(() => hasPermission); spyOn(sitesService, 'getSites').and.returnValue(Observable.of({ list: { entries: [] } }));
component.isSelectionValid = returnAlwaysTrue.bind(this);
}); });
it('should become enabled after loading node with the necessary permissions', async(() => { describe('in the case when isSelectionValid is a custom function for checking permissions,', () => {
hasPermission = true;
component.documentList.folderNode = entry;
component.select.subscribe((nodes) => { beforeEach(() => {
expect(nodes).toBeDefined(); component.isSelectionValid = returnHasPermission;
expect(nodes).not.toBeNull();
}); });
component.documentList.ready.emit(nodePage); it('should NOT be null after selecting node with the necessary permissions', async(() => {
fixture.detectChanges(); hasPermission = true;
})); component.documentList.folderNode = entry;
it('should remain disabled after loading node without the necessary permissions', () => { component.select.subscribe((nodes) => {
hasPermission = false; expect(nodes).toBeDefined();
component.documentList.folderNode = entry; expect(nodes).not.toBeNull();
expect(component.chosenNode).toBe(entry);
});
component.select.subscribe((nodes) => { component.documentList.ready.emit(nodePage);
expect(nodes).toBeDefined(); fixture.detectChanges();
expect(nodes).not.toBeNull(); }));
});
component.documentList.ready.emit(nodePage); it('should be null after selecting node without the necessary permissions', async(() => {
fixture.detectChanges(); hasPermission = false;
component.documentList.folderNode = entry;
component.select.subscribe((nodes) => {
expect(nodes).toBeDefined();
expect(nodes).toBeNull();
expect(component.chosenNode).toBeNull();
});
component.documentList.ready.emit(nodePage);
fixture.detectChanges();
}));
it('should NOT be null after clicking on a node (with the right permissions) in the list (onNodeSelect)', async(() => {
hasPermission = true;
component.select.subscribe((nodes) => {
expect(nodes).toBeDefined();
expect(nodes).not.toBeNull();
expect(component.chosenNode).toBe(entry);
});
component.onNodeSelect({ detail: { node: { entry } } });
fixture.detectChanges();
}));
it('should remain null when clicking on a node (with the WRONG permissions) in the list (onNodeSelect)', async(() => {
hasPermission = false;
component.select.subscribe((nodes) => {
expect(nodes).toBeDefined();
expect(nodes).toBeNull();
expect(component.chosenNode).toBeNull();
});
component.onNodeSelect({ detail: { node: { entry } } });
fixture.detectChanges();
}));
it('should become null when clicking on a node (with the WRONG permissions) after previously selecting a right node', async(() => {
component.select.subscribe((nodes) => {
if (hasPermission) {
expect(nodes).toBeDefined();
expect(nodes).not.toBeNull();
expect(component.chosenNode).not.toBeNull();
} else {
expect(nodes).toBeDefined();
expect(nodes).toBeNull();
expect(component.chosenNode).toBeNull();
}
});
hasPermission = true;
component.onNodeSelect({ detail: { node: { entry } } });
fixture.detectChanges();
hasPermission = false;
component.onNodeSelect({ detail: { node: { entry } } });
fixture.detectChanges();
}));
it('should be null when the chosenNode is reset', async(() => {
hasPermission = true;
component.onNodeSelect({ detail: { node: { entry: <MinimalNodeEntryEntity> {} } } });
fixture.detectChanges();
component.select.subscribe((nodes) => {
expect(nodes).toBeDefined();
expect(nodes).toBeNull();
expect(component.chosenNode).toBeNull();
});
component.resetChosenNode();
fixture.detectChanges();
}));
}); });
it('should be enabled when clicking on a node (with the right permissions) in the list (onNodeSelect)', () => { describe('in the case when isSelectionValid is null', () => {
hasPermission = true;
component.select.subscribe((nodes) => { beforeEach(() => {
expect(nodes).toBeDefined(); component.isSelectionValid = null;
expect(nodes).not.toBeNull();
}); });
component.onNodeSelect({ detail: { node: { entry } } }); it('should NOT be null after selecting node because isSelectionValid would be reset to defaultValidation function', async(() => {
fixture.detectChanges(); component.documentList.folderNode = entry;
fixture.detectChanges();
component.select.subscribe((nodes) => {
expect(nodes).toBeDefined();
expect(nodes).not.toBeNull();
expect(component.chosenNode).not.toBeNull();
expect(component.isSelectionValid).not.toBeNull();
});
component.documentList.ready.emit(nodePage);
fixture.detectChanges();
}));
it('should NOT be null after clicking on a node in the list (onNodeSelect)', async(() => {
fixture.detectChanges();
component.select.subscribe((nodes) => {
expect(nodes).toBeDefined();
expect(nodes).not.toBeNull();
expect(component.chosenNode).not.toBeNull();
expect(component.isSelectionValid).not.toBeNull();
});
component.onNodeSelect({ detail: { node: { entry } } });
fixture.detectChanges();
}));
it('should be null when the chosenNode is reset', async(() => {
fixture.detectChanges();
component.onNodeSelect({ detail: { node: { entry: <MinimalNodeEntryEntity> {} } } });
fixture.detectChanges();
component.select.subscribe((nodes) => {
expect(nodes).toBeDefined();
expect(nodes).toBeNull();
expect(component.chosenNode).toBeNull();
expect(component.isSelectionValid).not.toBeNull();
});
component.resetChosenNode();
fixture.detectChanges();
}));
}); });
it('should remain disabled when clicking on a node (with the WRONG permissions) in the list (onNodeSelect)', () => { describe('in the case when isSelectionValid is not defined', () => {
hasPermission = false;
component.select.subscribe((nodes) => { beforeEach(() => {
expect(nodes).toBeDefined(); component.isSelectionValid = undefined;
expect(nodes).not.toBeNull();
}); });
component.onNodeSelect({ detail: { node: { entry } } }); it('should NOT be null after selecting node because isSelectionValid would be the defaultValidation function', async(() => {
fixture.detectChanges(); component.documentList.folderNode = entry;
}); fixture.detectChanges();
it('should become disabled when clicking on a node (with the WRONG permissions) after previously selecting a right node', () => { component.select.subscribe((nodes) => {
component.select.subscribe((nodes) => { expect(nodes).toBeDefined();
expect(nodes).toBeDefined(); expect(nodes).not.toBeNull();
expect(nodes).not.toBeNull(); expect(component.chosenNode).not.toBeNull();
}); expect(component.isSelectionValid).not.toBeNull();
});
hasPermission = true; component.documentList.ready.emit(nodePage);
component.onNodeSelect({ detail: { node: { entry } } }); fixture.detectChanges();
fixture.detectChanges(); }));
hasPermission = false; it('should NOT be null after clicking on a node in the list (onNodeSelect)', async(() => {
component.onNodeSelect({ detail: { node: { entry } } }); component.select.subscribe((nodes) => {
fixture.detectChanges(); expect(nodes).toBeDefined();
}); expect(nodes).not.toBeNull();
expect(component.chosenNode).not.toBeNull();
expect(component.isSelectionValid).not.toBeNull();
});
fixture.detectChanges();
it('should emit null when the chosenNode is reset', () => { component.onNodeSelect({ detail: { node: { entry } } });
hasPermission = true; fixture.detectChanges();
component.onNodeSelect({ detail: { node: { entry: <MinimalNodeEntryEntity> {} } } }); }));
fixture.detectChanges();
component.select.subscribe((nodes) => { it('should be null when the chosenNode is reset', async(() => {
expect(nodes).toBeDefined(); fixture.detectChanges();
expect(nodes).toBeNull();
});
component.resetChosenNode(); component.onNodeSelect({ detail: { node: { entry: <MinimalNodeEntryEntity> {} } } });
fixture.detectChanges(); fixture.detectChanges();
component.select.subscribe((nodes) => {
expect(nodes).toBeDefined();
expect(nodes).toBeNull();
expect(component.chosenNode).toBeNull();
expect(component.isSelectionValid).not.toBeNull();
});
component.resetChosenNode();
fixture.detectChanges();
}));
}); });
}); });
}); });

View File

@@ -128,6 +128,7 @@ export class ContentNodeSelectorPanelComponent implements OnInit {
this.paginationStrategy = PaginationStrategy.Infinite; this.paginationStrategy = PaginationStrategy.Infinite;
this.breadcrumbTransform = this.breadcrumbTransform ? this.breadcrumbTransform : null ; this.breadcrumbTransform = this.breadcrumbTransform ? this.breadcrumbTransform : null ;
this.isSelectionValid = this.isSelectionValid ? this.isSelectionValid : defaultValidation;
} }
/** /**