Unit tests for document list

- unit tests
- component code improvements

refs #9
This commit is contained in:
Denys Vuika
2016-05-06 14:49:59 +01:00
parent f37c6aca08
commit d4ddda084f
5 changed files with 157 additions and 31 deletions

View File

@@ -42,12 +42,12 @@ export declare class DocumentList implements OnInit, AfterViewChecked, AfterCont
ngAfterViewChecked(): void; ngAfterViewChecked(): void;
getContentActions(target: string, type: string): ContentActionModel[]; getContentActions(target: string, type: string): ContentActionModel[];
onNavigateParentClick($event: any): void; onNavigateParentClick($event: any): void;
onItemClick(item: DocumentEntity, $event: any): void; onItemClick(item: DocumentEntity, $event?: any): void;
goToRoute(r: any, $event: any): void; goToRoute(r: any, $event: any): void;
getContentUrl(document: DocumentEntity): string; getContentUrl(node: DocumentEntity): string;
getDocumentThumbnailUrl(document: DocumentEntity): string; getDocumentThumbnailUrl(node: DocumentEntity): string;
executeContentAction(node: DocumentEntity, action: ContentActionModel): void; executeContentAction(node: DocumentEntity, action: ContentActionModel): void;
displayFolderContent(path: any): void; displayFolderContent(path: any): void;
private _getItemPath(item); getNodePath(node: DocumentEntity): string;
private _setupDefaultColumns(); setupDefaultColumns(): void;
} }

View File

@@ -65,7 +65,7 @@ System.register(['angular2/core', './../services/alfresco.service', './../models
}; };
DocumentList.prototype.ngAfterContentInit = function () { DocumentList.prototype.ngAfterContentInit = function () {
if (!this.columns || this.columns.length === 0) { if (!this.columns || this.columns.length === 0) {
this._setupDefaultColumns(); this.setupDefaultColumns();
} }
}; };
DocumentList.prototype.ngAfterViewChecked = function () { DocumentList.prototype.ngAfterViewChecked = function () {
@@ -98,6 +98,7 @@ System.register(['angular2/core', './../services/alfresco.service', './../models
} }
}; };
DocumentList.prototype.onItemClick = function (item, $event) { DocumentList.prototype.onItemClick = function (item, $event) {
if ($event === void 0) { $event = null; }
if ($event) { if ($event) {
$event.preventDefault(); $event.preventDefault();
} }
@@ -106,7 +107,7 @@ System.register(['angular2/core', './../services/alfresco.service', './../models
}); });
if (this.navigate && item) { if (this.navigate && item) {
if (item.isFolder) { if (item.isFolder) {
var path = this._getItemPath(item); var path = this.getNodePath(item);
this.route.push({ this.route.push({
name: item.displayName, name: item.displayName,
path: path path: path
@@ -127,11 +128,17 @@ System.register(['angular2/core', './../services/alfresco.service', './../models
} }
} }
}; };
DocumentList.prototype.getContentUrl = function (document) { DocumentList.prototype.getContentUrl = function (node) {
return this._alfrescoService.getContentUrl(document); if (this._alfrescoService) {
return this._alfrescoService.getContentUrl(node);
}
return null;
}; };
DocumentList.prototype.getDocumentThumbnailUrl = function (document) { DocumentList.prototype.getDocumentThumbnailUrl = function (node) {
return this._alfrescoService.getDocumentThumbnailUrl(document); if (this._alfrescoService) {
return this._alfrescoService.getDocumentThumbnailUrl(node);
}
return null;
}; };
DocumentList.prototype.executeContentAction = function (node, action) { DocumentList.prototype.executeContentAction = function (node, action) {
if (action) { if (action) {
@@ -147,13 +154,16 @@ System.register(['angular2/core', './../services/alfresco.service', './../models
.subscribe(function (folder) { return _this.folder = folder; }, function (error) { return _this.errorMessage = error; }); .subscribe(function (folder) { return _this.folder = folder; }, function (error) { return _this.errorMessage = error; });
} }
}; };
DocumentList.prototype._getItemPath = function (item) { DocumentList.prototype.getNodePath = function (node) {
var container = item.location.container; if (node) {
var path = item.location.path !== '/' ? (item.location.path + '/') : '/'; var container = node.location.container;
var relativePath = container + path + item.fileName; var path = node.location.path !== '/' ? (node.location.path + '/') : '/';
return item.location.site + '/' + relativePath; var relativePath = container + path + node.fileName;
return node.location.site + '/' + relativePath;
}
return null;
}; };
DocumentList.prototype._setupDefaultColumns = function () { DocumentList.prototype.setupDefaultColumns = function () {
var thumbnailCol = new content_column_model_1.ContentColumnModel(); var thumbnailCol = new content_column_model_1.ContentColumnModel();
thumbnailCol.source = '$thumbnail'; thumbnailCol.source = '$thumbnail';
var nameCol = new content_column_model_1.ContentColumnModel(); var nameCol = new content_column_model_1.ContentColumnModel();

File diff suppressed because one or more lines are too long

View File

@@ -81,7 +81,7 @@ export class DocumentList implements OnInit, AfterViewChecked, AfterContentInit
ngAfterContentInit() { ngAfterContentInit() {
if (!this.columns || this.columns.length === 0) { if (!this.columns || this.columns.length === 0) {
this._setupDefaultColumns(); this.setupDefaultColumns();
} }
} }
@@ -120,7 +120,7 @@ export class DocumentList implements OnInit, AfterViewChecked, AfterContentInit
} }
} }
onItemClick(item: DocumentEntity, $event) { onItemClick(item: DocumentEntity, $event = null) {
if ($event) { if ($event) {
$event.preventDefault(); $event.preventDefault();
} }
@@ -131,7 +131,7 @@ export class DocumentList implements OnInit, AfterViewChecked, AfterContentInit
if (this.navigate && item) { if (this.navigate && item) {
if (item.isFolder) { if (item.isFolder) {
let path = this._getItemPath(item); let path = this.getNodePath(item);
this.route.push({ this.route.push({
name: item.displayName, name: item.displayName,
path: path path: path
@@ -155,12 +155,18 @@ export class DocumentList implements OnInit, AfterViewChecked, AfterContentInit
} }
} }
getContentUrl(document: DocumentEntity) { getContentUrl(node: DocumentEntity) {
return this._alfrescoService.getContentUrl(document); if (this._alfrescoService) {
return this._alfrescoService.getContentUrl(node);
}
return null;
} }
getDocumentThumbnailUrl(document: DocumentEntity) { getDocumentThumbnailUrl(node: DocumentEntity) {
return this._alfrescoService.getDocumentThumbnailUrl(document); if (this._alfrescoService) {
return this._alfrescoService.getDocumentThumbnailUrl(node);
}
return null;
} }
executeContentAction(node: DocumentEntity, action: ContentActionModel) { executeContentAction(node: DocumentEntity, action: ContentActionModel) {
@@ -181,14 +187,17 @@ export class DocumentList implements OnInit, AfterViewChecked, AfterContentInit
} }
} }
private _getItemPath(item: DocumentEntity): string { getNodePath(node: DocumentEntity): string {
let container = item.location.container; if (node) {
let path = item.location.path !== '/' ? (item.location.path + '/' ) : '/'; let container = node.location.container;
let relativePath = container + path + item.fileName; let path = node.location.path !== '/' ? (node.location.path + '/' ) : '/';
return item.location.site + '/' + relativePath; let relativePath = container + path + node.fileName;
return node.location.site + '/' + relativePath;
}
return null;
} }
private _setupDefaultColumns() { setupDefaultColumns(): void {
let thumbnailCol = new ContentColumnModel(); let thumbnailCol = new ContentColumnModel();
thumbnailCol.source = '$thumbnail'; thumbnailCol.source = '$thumbnail';

View File

@@ -30,14 +30,23 @@ describe('document-list', () => {
let alfrescoServiceMock: AlfrescoServiceMock; let alfrescoServiceMock: AlfrescoServiceMock;
let documentList: DocumentList; let documentList: DocumentList;
let eventMock: any;
beforeEach(() => { beforeEach(() => {
alfrescoServiceMock = new AlfrescoServiceMock(); alfrescoServiceMock = new AlfrescoServiceMock();
documentList = new DocumentList(alfrescoServiceMock); documentList = new DocumentList(alfrescoServiceMock);
eventMock = {
preventDefault: function() {}
};
}); });
it('should setup default columns', () => { it('should setup default columns', () => {
spyOn(documentList, 'setupDefaultColumns').and.callThrough();
documentList.ngAfterContentInit(); documentList.ngAfterContentInit();
expect(documentList.setupDefaultColumns).toHaveBeenCalled();
expect(documentList.columns.length).not.toBe(0); expect(documentList.columns.length).not.toBe(0);
}); });
@@ -93,6 +102,12 @@ describe('document-list', () => {
expect(alfrescoServiceMock.getContentUrl).toHaveBeenCalled(); expect(alfrescoServiceMock.getContentUrl).toHaveBeenCalled();
}); });
it('should return no content url without service', () => {
let list = new DocumentList(null);
let node = new DocumentEntity();
expect(list.getContentUrl(node)).toBeNull();
});
it('should get thumbnail url', () => { it('should get thumbnail url', () => {
let url = 'URL'; let url = 'URL';
spyOn(alfrescoServiceMock, 'getDocumentThumbnailUrl').and.returnValue(url); spyOn(alfrescoServiceMock, 'getDocumentThumbnailUrl').and.returnValue(url);
@@ -102,6 +117,12 @@ describe('document-list', () => {
expect(result).toBe(url); expect(result).toBe(url);
expect(alfrescoServiceMock.getDocumentThumbnailUrl).toHaveBeenCalled(); expect(alfrescoServiceMock.getDocumentThumbnailUrl).toHaveBeenCalled();
}); });
it('should get no thumbnail url without service', () => {
let list = new DocumentList(null);
let node = new DocumentEntity();
expect(list.getDocumentThumbnailUrl(node)).toBeNull();
});
it('should execute action with node', () => { it('should execute action with node', () => {
let node = new DocumentEntity(); let node = new DocumentEntity();
@@ -207,4 +228,90 @@ describe('document-list', () => {
expect(actions.length).toBe(0); expect(actions.length).toBe(0);
}); });
it('should emit itemClick event', (done) => {
let node: DocumentEntity = new DocumentEntity();
documentList.itemClick.subscribe(e => {
expect(e.value).toBe(node);
done();
});
documentList.onItemClick(node);
});
it('should prevent default events for item click', () => {
spyOn(eventMock, 'preventDefault').and.stub();
documentList.onItemClick(null, eventMock);
expect(eventMock.preventDefault).toHaveBeenCalled();
});
it('should display folder content on click', () => {
let path = '/';
let node = new DocumentEntity();
node.isFolder = true;
node.displayName = '<display name>';
spyOn(documentList, 'getNodePath').and.returnValue(path);
spyOn(documentList, 'displayFolderContent').and.stub();
documentList.onItemClick(node);
expect(documentList.displayFolderContent).toHaveBeenCalledWith(path);
var routeEntry = documentList.route.pop();
expect(routeEntry.name).toBe(node.displayName);
expect(routeEntry.path).toBe(path);
});
it('should not display folder content when no target node provided', () => {
expect(documentList.navigate).toBe(true);
spyOn(documentList, 'displayFolderContent').and.stub();
documentList.onItemClick(null);
expect(documentList.displayFolderContent).not.toHaveBeenCalled();
});
it('should display folder content only on folder node click', () => {
expect(documentList.navigate).toBe(true);
spyOn(documentList, 'displayFolderContent').and.stub();
let node = new DocumentEntity();
node.isFolder = false;
documentList.onItemClick(node);
expect(documentList.displayFolderContent).not.toHaveBeenCalled();
});
it('should not display folder content on click when navigation is off', () => {
spyOn(documentList, 'displayFolderContent').and.stub();
let node = new DocumentEntity();
node.isFolder = true;
node.displayName = '<display name>';
documentList.navigate = false;
documentList.onItemClick(node);
expect(documentList.displayFolderContent).not.toHaveBeenCalled();
});
it('should require node to get path', () => {
expect(documentList.getNodePath(null)).toBe(null);
});
it('should get node path', () => {
let node = {
fileName: 'fileName',
location: {
site: 'swsdp',
container: 'documentLibrary',
path: '\/'
}
};
expect(documentList.getNodePath(node)).toBe('swsdp/documentLibrary/fileName');
});
}); });