Skip to content

Commit

Permalink
* Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sivakumar414ram committed Aug 31, 2020
1 parent 94aea19 commit 114c65a
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { async, fakeAsync, tick, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NodeEntry, Node, SiteEntry, SitePaging, NodePaging } from '@alfresco/js-api';
import { SearchService, SitesService, setupTestBed, NodesApiService } from '@alfresco/adf-core';
import { SearchService, SitesService, setupTestBed, NodesApiService, UploadService, FileUploadCompleteEvent } from '@alfresco/adf-core';
import { Observable, Observer, of, throwError } from 'rxjs';
import { DropdownBreadcrumbComponent } from '../breadcrumb';
import { ContentNodeSelectorPanelComponent } from './content-node-selector-panel.component';
Expand All @@ -31,6 +31,7 @@ import { DropdownSitesComponent } from '../site-dropdown/sites-dropdown.componen
import { CustomResourcesService } from '../document-list/services/custom-resources.service';
import { NodeEntryEvent, ShareDataRow } from '../document-list';
import { TranslateModule } from '@ngx-translate/core';
import { FileNode } from '../mock';

const ONE_FOLDER_RESULT = {
list: {
Expand All @@ -56,6 +57,7 @@ describe('ContentNodeSelectorComponent', () => {
let contentNodeSelectorService: ContentNodeSelectorService;
let searchService: SearchService;
let nodeService: NodesApiService;
let uploadService: UploadService;
let sitesService: SitesService;
let searchSpy: jasmine.Spy;
let cnSearchSpy: jasmine.Spy;
Expand Down Expand Up @@ -94,6 +96,7 @@ describe('ContentNodeSelectorComponent', () => {
nodeService = TestBed.inject(NodesApiService);
contentNodeSelectorService = TestBed.inject(ContentNodeSelectorService);
sitesService = TestBed.inject(SitesService);
uploadService = TestBed.inject(UploadService);

spyOn(nodeService, 'getNode').and.returnValue(of({ id: 'fake-node', path: { elements: [{ nodeType: 'st:site', name: 'fake-site'}] } }));
cnSearchSpy = spyOn(contentNodeSelectorService, 'search').and.callThrough();
Expand Down Expand Up @@ -1144,6 +1147,40 @@ describe('ContentNodeSelectorComponent', () => {
}));
});

describe('in case preSelectedNodes defined', () => {

it('should update completed uploaded files count', () => {
const completedFiles = 2;
const completeEvent = new FileUploadCompleteEvent(null, completedFiles, { entry: new FileNode('file-name') }, null);
uploadService.fileUploadComplete.next(completeEvent);

expect(component.preSelectedNodes).toEqual([]);
});
it('should NOT be null after selecting node with the necessary permissions', () => {
// const completeEvent = new FileUploadCompleteEvent(null, 1, { entry: {isFile: true} }, null);
spyOn(uploadService, 'uploadFilesInTheQueue').and.callFake(() => {});
spyOn(component.documentList, 'nodeSelected');
// uploadService.fileUploadComplete.next();
// spyOn(uploadService, 'getUploadPromise').and.returnValue(new Promise((success) => {
// success(ss);
// }));

component.preSelectedNodes = [new FileNode()];
fixture.detectChanges();
// await fixture.whenStable();

expect(component.chosenNode.length).toEqual(2);
expect(component.documentList.nodeSelected).toHaveBeenCalled();
// component.select.subscribe((nodes) => {
// expect(nodes).toBeDefined();
// expect(nodes).not.toBeNull();
// expect(component.chosenNode[0]).toBe(entry);
// });

// component.documentList.ready.emit(nodePage);
});
});

});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,15 @@ export class ContentNodeSelectorPanelComponent implements OnInit, OnDestroy {

this.breadcrumbTransform = this.breadcrumbTransform ? this.breadcrumbTransform : null;
this.isSelectionValid = this.isSelectionValid ? this.isSelectionValid : defaultValidation;
this.onFileUploadEvent();
}

ngOnDestroy() {
this.onDestroy$.next(true);
this.onDestroy$.complete();
}

private onFileUploadEvent() {
this.uploadService.fileUploadComplete
.pipe(
debounceTime(300),
Expand All @@ -282,12 +290,6 @@ export class ContentNodeSelectorPanelComponent implements OnInit, OnDestroy {
this.preSelectedNodes = [...uploadedFiles.map((uploadedFile) => uploadedFile.data)];
this.documentList.reload();
});

}

ngOnDestroy() {
this.onDestroy$.next(true);
this.onDestroy$.complete();
}

private getStartSite() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
fakeNodeAnswerWithNOEntries,
fakeNodeWithNoPermission,
fakeGetSitesAnswer,
fakeGetSiteMembership
fakeGetSiteMembership,
mockPreselectedNodes,
mockNodePagingWithPreselectedNodes
} from '../../mock';
import { ContentActionModel } from '../models/content-action.model';
import { NodeMinimal, NodeMinimalEntry, NodePaging } from '../models/document-library.model';
Expand Down Expand Up @@ -1515,6 +1517,58 @@ describe('DocumentList', () => {
where: undefined
}), undefined);
});

describe('Preselected rows', () => {

it('should able to emit preselected nodes', async () => {
const currentFolderNodeIdChange = new SimpleChange('current-node-id', 'next-node-id', true);
const nodeSelectedSpy = spyOn(documentList.nodeSelected, 'emit');

fixture.detectChanges();

documentList.node = mockNodePagingWithPreselectedNodes;
documentList.preSelectedNodes = mockPreselectedNodes;
documentList.ngOnChanges({ currentFolderId: currentFolderNodeIdChange });

documentList.reload();
fixture.detectChanges();
await fixture.whenStable();

expect(documentList.preSelectedNodes.length).toBe(2);
expect(nodeSelectedSpy).toHaveBeenCalled();
});

it('should able to emit preselect nodes on the reload', async () => {
const nodeSelectedSpy = spyOn(documentList.nodeSelected, 'emit');

fixture.detectChanges();

documentList.node = mockNodePagingWithPreselectedNodes;
documentList.preSelectedNodes = mockPreselectedNodes;
documentList.reload();

fixture.detectChanges();
await fixture.whenStable();

expect(documentList.preSelectedNodes.length).toBe(2);
expect(nodeSelectedSpy).toHaveBeenCalled();
});

it('should not call nodeSelected when preselectedNodes undefined/empty', async () => {
const nodeSelectedSpy = spyOn(documentList.nodeSelected, 'emit');

fixture.detectChanges();

documentList.node = mockNodePagingWithPreselectedNodes;
documentList.preSelectedNodes = [];
documentList.reload();

fixture.detectChanges();
await fixture.whenStable();

expect(nodeSelectedSpy).not.toHaveBeenCalled();
});
});
});

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export class DocumentListComponent implements OnInit, OnChanges, OnDestroy, Afte
currentFolderId: string = null;

@Input()
preSelectedNodes: any[] = [];
preSelectedNodes: NodeEntry[] = [];

/** The Document list will show all the nodes contained in the NodePaging entity */
@Input()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { DataColumn, DataRow, DataSorting, ContentService, ThumbnailService, setupTestBed } from '@alfresco/adf-core';
import { FileNode, FolderNode, SmartFolderNode, RuleFolderNode, LinkFolderNode } from './../../mock';
import { FileNode, FolderNode, SmartFolderNode, RuleFolderNode, LinkFolderNode, mockPreselectedNodes, mockNodePagingWithPreselectedNodes, mockNode2, fakeNodePaging } from './../../mock';
import { ShareDataRow } from './share-data-row.model';
import { ShareDataTableAdapter } from './share-datatable-adapter';
import { ContentTestingModule } from '../../testing/content.testing.module';
Expand Down Expand Up @@ -481,4 +481,31 @@ describe('ShareDataTableAdapter', () => {
expect(row.isDropTarget).toBeFalsy();
});
});

describe('Preselected rows', () => {

it('should set isSelected to be true for each preselectedrow if the preselectedNodes are defined', () => {
const adapter = new ShareDataTableAdapter(thumbnailService, contentService, []);
adapter.loadPage(mockNodePagingWithPreselectedNodes, null, null, mockPreselectedNodes);

expect(adapter.getPreSelectedRows().length).toBe(1);
expect(adapter.getPreSelectedRows()[0].isSelected).toBe(true);
});

it('should set preselectedRows empty if preselectedNodes are undefined/empty', () => {
const adapter = new ShareDataTableAdapter(thumbnailService, contentService, []);
adapter.loadPage(mockNodePagingWithPreselectedNodes, null, null, []);

expect(adapter.getPreSelectedRows().length).toBe(0);
});

it('should set preselectedRows empty if preselectedNodes are not found in the list', () => {
const adapter = new ShareDataTableAdapter(thumbnailService, contentService, []);
mockNode2.id = 'mock-file-id';
const preselectedNode = [ { entry: mockNode2 }];
adapter.loadPage(fakeNodePaging, null, null, preselectedNode);

expect(adapter.getPreSelectedRows().length).toBe(0);
});
});
});
116 changes: 115 additions & 1 deletion lib/content-services/src/lib/mock/document-list.component.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { Node, NodePaging } from '@alfresco/js-api';
import { Node, NodePaging, NodeEntry } from '@alfresco/js-api';

export const fakeNodeWithCreatePermission = new Node({
isFile: false,
Expand Down Expand Up @@ -215,3 +215,117 @@ export const fakeNodePaging: NodePaging = {
}]
}
};

export const mockNode1 = new Node({
'isFile': true,
'createdByUser': { 'id': 'admin', 'displayName': 'Administrator' },
'modifiedAt': '2017-05-24T15:08:55.640Z',
'nodeType': 'cm:content',
'content': {
'mimeType': 'application/rtf',
'mimeTypeName': 'Rich Text Format',
'sizeInBytes': 14530,
'encoding': 'UTF-8'
},
'parentId': 'd124de26-6ba0-4f40-8d98-4907da2d337a',
'createdAt': '2017-05-24T15:08:55.640Z',
'path': {
'name': '/Company Home/Guest Home',
'isComplete': true,
'elements': [{
'id': '94acfc73-7014-4475-9bd9-93a2162f0f8c',
'name': 'Company Home'
}, { 'id': 'd124de26-6ba0-4f40-8d98-4907da2d337a', 'name': 'Guest Home' }]
},
'isFolder': false,
'modifiedByUser': { 'id': 'admin', 'displayName': 'Administrator' },
'name': 'b_txt_file.rtf',
'id': '70e1cc6a-6918-468a-b84a-1048093b06fd',
'properties': { 'cm:versionLabel': '1.0', 'cm:versionType': 'MAJOR' },
'allowableOperations': ['delete', 'update']
});

export const mockNode2 = new Node({
'isFile': true,
'createdByUser': { 'id': 'admin', 'displayName': 'Administrator' },
'modifiedAt': '2017-05-24T15:08:55.640Z',
'nodeType': 'cm:content',
'content': {
'mimeType': 'application/rtf',
'mimeTypeName': 'Rich Text Format',
'sizeInBytes': 14530,
'encoding': 'UTF-8'
},
'parentId': 'd124de26-6ba0-4f40-8d98-4907da2d337a',
'createdAt': '2017-05-24T15:08:55.640Z',
'path': {
'name': '/Company Home/Guest Home',
'isComplete': true,
'elements': [{
'id': '94acfc73-7014-4475-9bd9-93a2162f0f8c',
'name': 'Company Home'
}, { 'id': 'd124de26-6ba0-4f40-8d98-4907da2d337a', 'name': 'Guest Home' }]
},
'isFolder': false,
'modifiedByUser': { 'id': 'admin', 'displayName': 'Administrator' },
'name': 'b_txt_file.rtf',
'id': '67b80f77-dbca-4f58-be6c-71b9dd61e554',
'properties': { 'cm:versionLabel': '1.0', 'cm:versionType': 'MAJOR' },
'allowableOperations': ['delete', 'update']
});

export const mockNode3 = new Node({
'isFile': true,
'createdByUser': { 'id': 'admin', 'displayName': 'Administrator' },
'modifiedAt': '2017-05-24T15:08:55.640Z',
'nodeType': 'cm:content',
'content': {
'mimeType': 'application/rtf',
'mimeTypeName': 'Rich Text Format',
'sizeInBytes': 14530,
'encoding': 'UTF-8'
},
'parentId': 'd124de26-6ba0-4f40-8d98-4907da2d337a',
'createdAt': '2017-05-24T15:08:55.640Z',
'path': {
'name': '/Company Home/Guest Home',
'isComplete': true,
'elements': [{
'id': '94acfc73-7014-4475-9bd9-93a2162f0f8c',
'name': 'Company Home'
}, { 'id': 'd124de26-6ba0-4f40-8d98-4907da2d337a', 'name': 'Guest Home' }]
},
'isFolder': false,
'modifiedByUser': { 'id': 'admin', 'displayName': 'Administrator' },
'name': 'c_txt_file.rtf',
'id': '67b80f77-dbca-4f58-be6c-71b9dd61e555',
'properties': { 'cm:versionLabel': '1.0', 'cm:versionType': 'MAJOR' },
'allowableOperations': ['delete', 'update']
});

export const mockPreselectedNodes: NodeEntry[] = [
{
entry: mockNode1
},
{
entry: mockNode1
}
];

export const mockNodePagingWithPreselectedNodes: NodePaging = {
list: {
pagination: {
count: 5,
hasMoreItems: false,
totalItems: 5,
skipCount: 0,
maxItems: 100
}, entries: [{
entry: mockNode1
}, {
entry: mockNode2
}, {
entry: mockNode3
}]
}
};

0 comments on commit 114c65a

Please sign in to comment.