From b1553a9bed8a66b8e2f011aed43c35c778c90afa Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Thu, 30 Jun 2016 15:44:26 +0100 Subject: [PATCH] Unit tests and api improvements for document list --- .../src/components/content-action.spec.ts | 44 +++++++++++++++++++ .../src/components/content-action.ts | 9 +++- .../src/components/content-column.spec.ts | 16 +++++++ .../src/components/content-column.ts | 7 ++- ...document-list-breadcrumb.component.spec.ts | 23 ++++++++++ .../document-list-breadcrumb.component.ts | 7 +++ .../src/components/document-list.spec.ts | 33 ++++++++++++-- 7 files changed, 133 insertions(+), 6 deletions(-) diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.spec.ts b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.spec.ts index 08d260980c..0f42649e01 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.spec.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.spec.ts @@ -30,6 +30,7 @@ import { ContentAction } from './content-action'; import { DocumentActionsService } from '../services/document-actions.service'; import { FolderActionsService } from '../services/folder-actions.service'; import { ContentActionHandler } from '../models/content-action.model'; +import { FileNode } from '../assets/document-library.model.mock'; describe('ContentAction', () => { @@ -225,4 +226,47 @@ describe('ContentAction', () => { expect(documentActions.getHandler).not.toHaveBeenCalled(); }); + + it('should wire model with custom event handler', (done) => { + let action = new ContentAction(actionList, documentActions, folderActions); + let file = new FileNode(); + + let handler = new EventEmitter(); + handler.subscribe((e) => { + expect(e.value).toBe(file); + done(); + }); + + action.execute = handler; + + action.ngOnInit(); + action.model.handler(file); + }); + + it('should allow registering model without handler', () => { + let action = new ContentAction(actionList, documentActions, folderActions); + + spyOn(actionList, 'registerAction').and.callThrough(); + action.execute = null; + action.ngOnInit(); + + expect(action.model.handler).toBeUndefined(); + expect(actionList.registerAction).toHaveBeenCalledWith(action.model); + }); + + it('should register on init', () => { + let action = new ContentAction(actionList, null, null); + spyOn(action, 'register').and.callThrough(); + + action.ngOnInit(); + expect(action.register).toHaveBeenCalled(); + }); + + it('should require action list to register action with', () => { + let action = new ContentAction(actionList, null, null); + expect(action.register()).toBeTruthy(); + + action = new ContentAction(null, null, null); + expect(action.register()).toBeFalsy(); + }); }); diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.ts b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.ts index 33164fdea5..2c0f2859a6 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.ts @@ -73,7 +73,14 @@ export class ContentAction implements OnInit, OnChanges { }; } - this.list.registerAction(this.model); + this.register(); + } + + register(): boolean { + if (this.list) { + return this.list.registerAction(this.model); + } + return false; } ngOnChanges(changes) { diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-column.spec.ts b/ng2-components/ng2-alfresco-documentlist/src/components/content-column.spec.ts index 9f335ec6a3..f44502c9f8 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-column.spec.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-column.spec.ts @@ -93,4 +93,20 @@ describe('ContentColumn', () => { expect(column.model.title).toBe('title2'); }); + it('should register on init', () => { + let column = new ContentColumn(columnList); + spyOn(column, 'register').and.callThrough(); + + column.ngOnInit(); + expect(column.register).toHaveBeenCalled(); + }); + + it('should require action list to register action with', () => { + let column = new ContentColumn(columnList); + expect(column.register()).toBeTruthy(); + + column = new ContentColumn(null); + expect(column.register()).toBeFalsy(); + }); + }); diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-column.ts b/ng2-components/ng2-alfresco-documentlist/src/components/content-column.ts index 1599d765f6..3b0c9d40ff 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-column.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-column.ts @@ -66,9 +66,14 @@ export class ContentColumn implements OnInit, OnChanges { this.model.srTitle = 'Thumbnail'; } + this.register(); + } + + register(): boolean { if (this.list) { - this.list.registerColumn(this.model); + return this.list.registerColumn(this.model); } + return false; } ngOnChanges(change) { diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/document-list-breadcrumb.component.spec.ts b/ng2-components/ng2-alfresco-documentlist/src/components/document-list-breadcrumb.component.spec.ts index 9b1ba0fde1..47a688a106 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/document-list-breadcrumb.component.spec.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/document-list-breadcrumb.component.spec.ts @@ -106,4 +106,27 @@ describe('DocumentListBreadcrumb', () => { expect(documentList.currentFolderPath).toBe(node.path); }); + it('should do nothing for same path', () => { + let called = 0; + + component.pathChanged.subscribe(() => called++); + + component.currentFolderPath = '/'; + component.currentFolderPath = '/'; + + expect(called).toBe(0); + }); + + it('should emit path changed event', (done) => { + let path = '/some/path'; + + component.pathChanged.subscribe(e => { + expect(e.value).toBe(path); + expect(e.route).toBe(component.route); + done(); + }); + + component.currentFolderPath = path; + }); + }); diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/document-list-breadcrumb.component.ts b/ng2-components/ng2-alfresco-documentlist/src/components/document-list-breadcrumb.component.ts index 5bc4990259..40e1c13308 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/document-list-breadcrumb.component.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/document-list-breadcrumb.component.ts @@ -49,6 +49,10 @@ export class DocumentListBreadcrumb { this._currentFolderPath = this.rootFolder.path; this.route = [ this.rootFolder ]; } + this.pathChanged.emit({ + value: this._currentFolderPath, + route: this.route + }); } } @@ -65,6 +69,9 @@ export class DocumentListBreadcrumb { @Output() navigate: EventEmitter = new EventEmitter(); + @Output() + pathChanged: EventEmitter = new EventEmitter(); + onRoutePathClick(route: PathNode, e?: Event) { if (e) { e.preventDefault(); diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/document-list.spec.ts b/ng2-components/ng2-alfresco-documentlist/src/components/document-list.spec.ts index 2010435b79..67b9d6cc42 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/document-list.spec.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/document-list.spec.ts @@ -849,15 +849,29 @@ describe('DocumentList', () => { it('should put folders to top on sort', () => { let folder = new FolderNode(); - let file = new FileNode(); - let page = new PageNode([file, folder]); + let file1 = new FileNode('file1'); + let file2 = new FileNode('file2'); + let page = new PageNode([file1, file2, folder]); + // asc documentList.sort(page, new ColumnSortingModel({ - key: 'name' + key: 'name', + direction: 'asc' })); expect(page.list.entries[0]).toBe(folder); - expect(page.list.entries[1]).toBe(file); + expect(page.list.entries[1]).toBe(file1); + expect(page.list.entries[2]).toBe(file2); + + // desc + documentList.sort(page, new ColumnSortingModel({ + key: 'name', + direction: 'desc' + })); + + expect(page.list.entries[0]).toBe(folder); + expect(page.list.entries[1]).toBe(file2); + expect(page.list.entries[2]).toBe(file1); }); it('should sort by dates up to ms', () => { @@ -868,6 +882,8 @@ describe('DocumentList', () => { file2.entry['dateProp'] = new Date(2016, 6, 30, 13, 14, 2); let page = new PageNode([file1, file2]); + + // desc documentList.sort(page, new ColumnSortingModel({ key: 'dateProp', direction: 'desc' @@ -875,6 +891,15 @@ describe('DocumentList', () => { expect(page.list.entries[0]).toBe(file2); expect(page.list.entries[1]).toBe(file1); + + // asc + documentList.sort(page, new ColumnSortingModel({ + key: 'dateProp', + direction: 'asc' + })); + + expect(page.list.entries[0]).toBe(file1); + expect(page.list.entries[1]).toBe(file2); }); });