From 0ccf192f35ff31b951caab42818a5e9d287e802b Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Wed, 6 Jul 2016 12:28:38 +0100 Subject: [PATCH] Repair unit tests --- .../src/data/datatable-adapter.ts | 2 +- .../src/components/content-action.spec.ts | 26 +- .../components/content-column-list.spec.ts | 36 +- .../src/components/content-column.spec.ts | 71 +--- .../src/components/content-column.ts | 12 +- .../src/components/document-list.spec.ts | 390 ++++++------------ .../src/components/document-list.ts | 102 ++--- .../src/data/share-datatable-adapter.ts | 26 +- 8 files changed, 249 insertions(+), 416 deletions(-) diff --git a/ng2-components/ng2-alfresco-datatable/src/data/datatable-adapter.ts b/ng2-components/ng2-alfresco-datatable/src/data/datatable-adapter.ts index e6b2cb07b2..41ebaada41 100644 --- a/ng2-components/ng2-alfresco-datatable/src/data/datatable-adapter.ts +++ b/ng2-components/ng2-alfresco-datatable/src/data/datatable-adapter.ts @@ -51,5 +51,5 @@ export class DataSorting { export interface DataRowEvent { value?: DataRow; - event: Event; + event?: Event; } 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 0a43a2699d..59b473841f 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 @@ -15,17 +15,6 @@ * limitations under the License. */ -import { describe, it, expect } from '@angular/core/testing'; - -describe('ContentAction', () => { - - it('should be upgraded', () => { - expect(false).toBeTruthy(); - }); - -}); - -/* import { it, describe, @@ -35,7 +24,7 @@ import { import { EventEmitter } from '@angular/core'; import { DocumentList } from './document-list'; -import { AlfrescoServiceMock } from '../assets/alfresco.service.mock'; +import { DocumentListServiceMock } from '../assets/document-list.service.mock'; import { ContentActionList } from './content-action-list'; import { ContentAction } from './content-action'; import { DocumentActionsService } from '../services/document-actions.service'; @@ -51,11 +40,11 @@ describe('ContentAction', () => { let folderActions: FolderActionsService; beforeEach(() => { - let alfrescoServiceMock = new AlfrescoServiceMock(); + let documentServiceMock = new DocumentListServiceMock(); documentActions = new DocumentActionsService(null, null); folderActions = new FolderActionsService(null); - documentList = new DocumentList(alfrescoServiceMock, null); + documentList = new DocumentList(documentServiceMock, null); actionList = new ContentActionList(documentList); }); @@ -70,7 +59,6 @@ describe('ContentAction', () => { it('should setup and register model', () => { let action = new ContentAction(actionList, null, null); - action.type = 'button'; action.target = 'document'; action.title = ''; action.icon = '<icon>'; @@ -81,7 +69,6 @@ describe('ContentAction', () => { expect(documentList.actions.length).toBe(1); let model = documentList.actions[0]; - expect(model.type).toBe(action.type); expect(model.target).toBe(action.target); expect(model.title).toBe(action.title); expect(model.icon).toBe(action.icon); @@ -93,7 +80,6 @@ describe('ContentAction', () => { spyOn(documentActions, 'getHandler').and.returnValue(handler); let action = new ContentAction(actionList, documentActions, null); - action.type = 'button'; action.target = 'document'; action.handler = '<handler>'; action.ngOnInit(); @@ -110,7 +96,6 @@ describe('ContentAction', () => { spyOn(folderActions, 'getHandler').and.returnValue(handler); let action = new ContentAction(actionList, null, folderActions); - action.type = 'button'; action.target = 'folder'; action.handler = '<handler>'; action.ngOnInit(); @@ -127,7 +112,6 @@ describe('ContentAction', () => { spyOn(documentActions, 'getHandler').and.stub(); let action = new ContentAction(actionList, documentActions, folderActions); - action.type = 'button'; action.handler = '<handler>'; action.ngOnInit(); @@ -149,7 +133,6 @@ describe('ContentAction', () => { let action = new ContentAction(actionList, documentActions, null); action.target = 'DoCuMeNt'; - action.type = 'button'; action.handler = '<handler>'; action.ngOnInit(); @@ -161,7 +144,6 @@ describe('ContentAction', () => { let action = new ContentAction(actionList, null, folderActions); action.target = 'FoLdEr'; - action.type = 'button'; action.handler = '<handler>'; action.ngOnInit(); @@ -178,7 +160,6 @@ describe('ContentAction', () => { let action = new ContentAction(actionList, null, null); action.target = 'document'; - action.type = 'button'; action.execute = emitter; action.ngOnInit(); @@ -281,4 +262,3 @@ describe('ContentAction', () => { expect(action.register()).toBeFalsy(); }); }); -*/ diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-column-list.spec.ts b/ng2-components/ng2-alfresco-documentlist/src/components/content-column-list.spec.ts index 210c87fe93..084f7c0ca3 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-column-list.spec.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-column-list.spec.ts @@ -15,17 +15,6 @@ * limitations under the License. */ -import { describe, it, expect } from '@angular/core/testing'; - -describe('ContentColumnList', () => { - - it('should be upgraded', () => { - expect(false).toBeTruthy(); - }); - -}); - -/* import { it, describe, @@ -33,10 +22,11 @@ import { beforeEach } from '@angular/core/testing'; -import {DocumentList} from './document-list'; -import {AlfrescoServiceMock} from '../assets/alfresco.service.mock'; -import {ContentColumnList} from './content-column-list'; -import {ContentColumnModel} from '../models/content-column.model'; +import { DataColumn } from 'ng2-alfresco-datatable'; + +import { DocumentList } from './document-list'; +import { DocumentListServiceMock } from '../assets/document-list.service.mock'; +import { ContentColumnList } from './content-column-list'; describe('ContentColumnList', () => { @@ -44,23 +34,26 @@ describe('ContentColumnList', () => { let columnList: ContentColumnList; beforeEach(() => { - let alfrescoServiceMock = new AlfrescoServiceMock(); - documentList = new DocumentList(alfrescoServiceMock, null); + let service = new DocumentListServiceMock(); + documentList = new DocumentList(service, null); columnList = new ContentColumnList(documentList); }); it('should register column within parent document list', () => { - expect(documentList.columns.length).toBe(0); + let columns = documentList.data.getColumns(); + expect(columns.length).toBe(0); - let result = columnList.registerColumn(new ContentColumnModel()); + let column = <DataColumn> {}; + let result = columnList.registerColumn(column); expect(result).toBeTruthy(); - expect(documentList.columns.length).toBe(1); + expect(columns.length).toBe(1); + expect(columns[0]).toBe(column); }); it('should require document list instance to register action', () => { columnList = new ContentColumnList(null); - let col = new ContentColumnModel(); + let col = <DataColumn> {}; expect(columnList.registerColumn(col)).toBeFalsy(); }); @@ -73,4 +66,3 @@ describe('ContentColumnList', () => { }); }); -*/ 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 f5eaf814a6..1966d9b6fe 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 @@ -15,17 +15,6 @@ * limitations under the License. */ -import { describe, it, expect } from '@angular/core/testing'; - -describe('ContentColumn', () => { - - it('should be upgraded', () => { - expect(false).toBeTruthy(); - }); - -}); - -/* import { it, describe, @@ -33,10 +22,10 @@ import { beforeEach } from '@angular/core/testing'; -import {DocumentList} from './document-list'; -import {ContentColumn} from './content-column'; -import {AlfrescoServiceMock} from '../assets/alfresco.service.mock'; -import {ContentColumnList} from './content-column-list'; +import { DocumentList } from './document-list'; +import { ContentColumn } from './content-column'; +import { DocumentListServiceMock } from '../assets/document-list.service.mock'; +import { ContentColumnList } from './content-column-list'; describe('ContentColumn', () => { @@ -44,8 +33,8 @@ describe('ContentColumn', () => { let columnList: ContentColumnList; beforeEach(() => { - let alfrescoServiceMock = new AlfrescoServiceMock(); - documentList = new DocumentList(alfrescoServiceMock, null); + let service = new DocumentListServiceMock(); + documentList = new DocumentList(service, null); columnList = new ContentColumnList(documentList); }); @@ -56,52 +45,19 @@ describe('ContentColumn', () => { column.ngOnInit(); expect(columnList.registerColumn).toHaveBeenCalled(); - }); - it('should setup model properties during registration', () => { - - let column = new ContentColumn(columnList); - column.title = '<title>'; - column.srTitle = '<sr-title>'; - column.source = '<source>'; - column.cssClass = '<css-class>'; - column.ngOnInit(); - - expect(documentList.columns.length).toBe(1); - - let model = documentList.columns[0]; - expect(model.title).toBe(column.title); - expect(model.srTitle).toBe(column.srTitle); - expect(model.source).toBe(column.source); - expect(model.cssClass).toBe(column.cssClass); + let columns = documentList.data.getColumns(); + expect(columns.length).toBe(1); + expect(columns[0]).toBe(column); }); it('should setup screen reader title for thumbnail column', () => { - - let column = new ContentColumn(columnList); - column.source = '$thumbnail'; + let column = new ContentColumn(columnList, { + key: '$thumbnail' + }); column.ngOnInit(); - expect(documentList.columns.length).toBe(1); - - let model = documentList.columns[0]; - expect(model.srTitle).toBe('Thumbnail'); - }); - - it('should sync localizable fields with model', () => { - - let column = new ContentColumn(columnList); - column.title = 'title1'; - column.srTitle = 'srTitle1'; - column.ngOnInit(); - - expect(column.model.title).toBe(column.title); - expect(column.model.srTitle).toBe(column.srTitle); - - column.title = 'title2'; - column.ngOnChanges(null); - - expect(column.model.title).toBe('title2'); + expect(column.srTitle).toBe('Thumbnail'); }); it('should register on init', () => { @@ -121,4 +77,3 @@ describe('ContentColumn', () => { }); }); -*/ 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 3f5f715f6d..7c5808898a 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-column.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-column.ts @@ -49,7 +49,17 @@ export class ContentColumn implements OnInit, DataColumn { @Input('class') cssClass: string; - constructor(private list: ContentColumnList) {} + constructor(private list: ContentColumnList, opts?: any) { + if (opts) { + this.key = opts.key; + this.type = opts.type || 'text'; + this.format = opts.format; + this.sortable = opts.sortable ? true : false; + this.title = opts.title || ''; + this.srTitle = opts.srTitle; + this.cssClass = opts.cssClass; + } + } ngOnInit() { if (!this.srTitle && this.key === '$thumbnail') { 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 89c8ed34f6..c9e373536b 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 @@ -15,46 +15,26 @@ * limitations under the License. */ -import { describe, it, expect } from '@angular/core/testing'; - -describe('DocumentList', () => { - - it('should be upgraded', () => { - expect(false).toBeTruthy(); - }); - -}); - -/* -import { - it, - describe, - expect, - beforeEach -} from '@angular/core/testing'; +import { it, describe, expect, beforeEach } from '@angular/core/testing'; import { NgZone } from '@angular/core'; +import { DataColumn } from 'ng2-alfresco-datatable'; import { DocumentList } from './document-list'; -import { ContentColumnModel } from '../models/content-column.model'; -import { AlfrescoServiceMock } from '../assets/alfresco.service.mock'; +import { DocumentListServiceMock } from './../assets/document-list.service.mock'; import { ContentActionModel } from '../models/content-action.model'; -import { - PageNode, - FileNode, - FolderNode -} from '../assets/document-library.model.mock'; -import { ColumnSortingModel } from '../models/column-sorting.model'; +import { FileNode, FolderNode } from '../assets/document-library.model.mock'; +import { MinimalNodeEntity } from '../models/document-library.model'; describe('DocumentList', () => { - let alfrescoServiceMock: AlfrescoServiceMock; + let documentListService: DocumentListServiceMock; let documentList: DocumentList; let eventMock: any; let componentHandler; beforeEach(() => { - alfrescoServiceMock = new AlfrescoServiceMock(); + documentListService = new DocumentListServiceMock(); let zone = new NgZone(false); - documentList = new DocumentList(alfrescoServiceMock, zone); + documentList = new DocumentList(documentListService, zone); eventMock = { preventDefault: function () { @@ -74,56 +54,67 @@ describe('DocumentList', () => { documentList.ngAfterContentInit(); expect(documentList.setupDefaultColumns).toHaveBeenCalled(); - expect(documentList.columns.length).not.toBe(0); + expect(documentList.data.getColumns().length).not.toBe(0); }); it('should use custom columns instead of default ones', () => { - let column: ContentColumnModel = { + let column = <DataColumn> { title: 'title', - source: 'source', + key: 'source', cssClass: 'css', srTitle: '', type: 'text', format: '' }; - documentList.columns.push(column); + + let columns = documentList.data.getColumns(); + columns.push(column); documentList.ngAfterContentInit(); - expect(documentList.columns.length).toBe(1); - expect(documentList.columns[0]).toBe(column); + expect(columns.length).toBe(1); + expect(columns[0]).toBe(column); }); + // TODO: move to data adapter + /* it('should fetch folder', () => { let folder = { 'nodeRef': 'workspace://SpacesStore/8bb36efb-c26d-4d2b-9199-ab6922f53c28' }; - alfrescoServiceMock.folderToReturn = folder; + documentListService.folderToReturn = folder; documentList.ngOnInit(); expect(documentList.folder).toBe(folder); }); + */ + // TODO: move to data adapter + /* it('should return thumbnail url for a file when thumbnails turned on', () => { let url = 'URL'; - spyOn(alfrescoServiceMock, 'getDocumentThumbnailUrl').and.returnValue(url); + spyOn(documentListService, 'getDocumentThumbnailUrl').and.returnValue(url); let node = new FileNode(); documentList.thumbnails = true; let result = documentList.getThumbnailUrl(node); expect(result).toBe(url); - expect(alfrescoServiceMock.getDocumentThumbnailUrl).toHaveBeenCalled(); + expect(documentListService.getDocumentThumbnailUrl).toHaveBeenCalled(); }); + */ + // TODO: move to data adapter + /* it('should return a null thumbnail url for a null item', () => { let url = 'URL'; - spyOn(alfrescoServiceMock, 'getDocumentThumbnailUrl').and.returnValue(url); + spyOn(documentListService, 'getDocumentThumbnailUrl').and.returnValue(url); let result = documentList.getThumbnailUrl(null); expect(result).toBeNull(); - expect(alfrescoServiceMock.getDocumentThumbnailUrl).not.toHaveBeenCalled(); + expect(documentListService.getDocumentThumbnailUrl).not.toHaveBeenCalled(); }); + */ it('should execute action with node', () => { let node = new FileNode(); @@ -150,100 +141,54 @@ describe('DocumentList', () => { expect(action.handler).not.toHaveBeenCalled(); }); - it('should give no content actions for empty target', () => { - let actions = documentList.getContentActions(null, 'button'); + it('should not give node actions for empty target', () => { + let actions = documentList.getNodeActions(null); expect(actions.length).toBe(0); }); - it('should give no content actions for empty type', () => { - let actions = documentList.getContentActions('folder', null); - expect(actions.length).toBe(0); - }); - - it('should filter content actions for various types and targets', () => { - let folderButton = new ContentActionModel(); - folderButton.target = 'folder'; - folderButton.type = 'button'; - + it('should filter content actions for various targets', () => { let folderMenu = new ContentActionModel(); folderMenu.target = 'folder'; - folderMenu.type = 'menu'; - - let documentButton = new ContentActionModel(); - documentButton.target = 'document'; - documentButton.type = 'button'; let documentMenu = new ContentActionModel(); documentMenu.target = 'document'; - documentMenu.type = 'menu'; documentList.actions = [ - folderButton, folderMenu, - documentButton, documentMenu ]; - let actions = documentList.getContentActions('folder', 'button'); - expect(actions.length).toBe(1); - expect(actions[0]).toBe(folderButton); - actions = documentList.getContentActions('folder', 'menu'); + let actions = documentList.getNodeActions(new FolderNode()); expect(actions.length).toBe(1); expect(actions[0]).toBe(folderMenu); - actions = documentList.getContentActions('document', 'button'); - expect(actions.length).toBe(1); - expect(actions[0]).toBe(documentButton); - - actions = documentList.getContentActions('document', 'menu'); + actions = documentList.getNodeActions(new FileNode()); expect(actions.length).toBe(1); expect(actions[0]).toBe(documentMenu); }); - it('should be case insensitive when filtering content actions', () => { - let documentButton = new ContentActionModel(); - documentButton.target = 'document'; - documentButton.type = 'button'; - - documentList.actions = [documentButton]; - - let actions = documentList.getContentActions('DoCuMeNt', 'BUTTON'); - expect(actions.length).toBe(1); - expect(actions[0]).toBe(documentButton); - }); - it('should find no content actions', () => { let documentButton = new ContentActionModel(); documentButton.target = 'document'; - documentButton.type = 'button'; - documentList.actions = [documentButton]; - let actions = documentList.getContentActions('unknown', 'value'); - expect(actions.length).toBe(0); + let node = new MinimalNodeEntity(); + expect(documentList.getNodeActions(node)).toEqual([]); + + node = new FileNode(); + node.entry.isFile = false; + node.entry.isFolder = false; + expect(documentList.getNodeActions(node)).toEqual([]); }); - it('should emit itemClick event', (done) => { + it('should emit nodeClick event', (done) => { let node = new FileNode(); - documentList.itemClick.subscribe(e => { + documentList.nodeClick.subscribe(e => { expect(e.value).toBe(node); done(); }); - documentList.onItemClick(node); - }); - - it('should prevent default item single click event', () => { - spyOn(eventMock, 'preventDefault').and.stub(); - - documentList.onItemClick(null, eventMock); - expect(eventMock.preventDefault).toHaveBeenCalled(); - }); - - it('should prevent default item double click event', () => { - spyOn(eventMock, 'preventDefault').and.stub(); - documentList.onItemDblClick(null, eventMock); - expect(eventMock.preventDefault).toHaveBeenCalled(); + documentList.onNodeClick(node); }); it('should display folder content on click', () => { @@ -255,7 +200,7 @@ describe('DocumentList', () => { spyOn(documentList, 'displayFolderContent').and.stub(); documentList.navigationMode = DocumentList.SINGLE_CLICK_NAVIGATION; - documentList.onItemClick(node); + documentList.onNodeClick(node); expect(documentList.currentFolderPath).toBe(path); }); @@ -264,7 +209,7 @@ describe('DocumentList', () => { expect(documentList.navigate).toBe(true); spyOn(documentList, 'displayFolderContent').and.stub(); - documentList.onItemClick(null); + documentList.onNodeClick(null); expect(documentList.displayFolderContent).not.toHaveBeenCalled(); }); @@ -274,7 +219,7 @@ describe('DocumentList', () => { spyOn(documentList, 'displayFolderContent').and.stub(); let node = new FileNode(); - documentList.onItemClick(node); + documentList.onNodeClick(node); expect(documentList.displayFolderContent).not.toHaveBeenCalled(); }); @@ -284,7 +229,7 @@ describe('DocumentList', () => { let node = new FolderNode('<display name>'); documentList.navigate = false; - documentList.onItemClick(node); + documentList.onNodeClick(node); expect(documentList.displayFolderContent).not.toHaveBeenCalled(); }); @@ -293,33 +238,6 @@ describe('DocumentList', () => { expect(documentList.getNodePath(null)).toBe(null); }); - it('should return root object value', () => { - let target = { - key1: 'value1' - }; - - expect(documentList.getObjectValue(target, 'key1')).toBe('value1'); - }); - - it('should return no object value when key is missing', () => { - let target = { - key1: 'value1' - }; - expect(documentList.getObjectValue(target, 'missing')).toBeUndefined(); - }); - - it('should return nested object value', () => { - let target = { - key1: { - key2: { - key3: 'value1' - } - } - }; - - expect(documentList.getObjectValue(target, 'key1.key2.key3')).toBe('value1'); - }); - it('should display folder content for new folder path', () => { spyOn(documentList, 'displayFolderContent').and.stub(); let newPath = '/some/new/path'; @@ -345,42 +263,44 @@ describe('DocumentList', () => { }); it('should emit folder changed event', (done) => { + spyOn(documentList, 'displayFolderContent').and.stub(); documentList.folderChange.subscribe(e => { done(); }); - documentList.folder = new PageNode(); + + documentList.currentFolderPath = '/some/new/path'; }); it('should emit folder changed event with folder details', (done) => { - let folder = new PageNode(); + spyOn(documentList, 'displayFolderContent').and.stub(); + let path = '/path'; documentList.folderChange.subscribe(e => { - expect(e.value).toBe(folder); expect(e.path).toBe(path); done(); }); - spyOn(documentList, 'displayFolderContent').and.stub(); documentList.currentFolderPath = path; - documentList.folder = folder; }); - it('should not emit folder changed event', () => { - let folder = new PageNode(); + it('should emit folder changed event only once', () => { + spyOn(documentList, 'displayFolderContent').and.stub(); + let path = '/new/path'; let calls = 0; documentList.folderChange.subscribe(e => { calls++; }); - documentList.folder = folder; - documentList.folder = folder; + documentList.currentFolderPath = path; + documentList.currentFolderPath = path; + documentList.currentFolderPath = path; expect(calls).toBe(1); }); it('should reload on binding changes', () => { spyOn(documentList, 'reload').and.stub(); - documentList.ngOnChanges(null); + documentList.ngOnChanges(); expect(documentList.reload).toHaveBeenCalled(); }); @@ -407,8 +327,9 @@ describe('DocumentList', () => { }); it('should subscribe to context action handler', () => { - let value = {}; + spyOn(documentList, 'displayFolderContent').and.stub(); spyOn(documentList, 'contextActionCallback').and.stub(); + let value = {}; documentList.ngOnInit(); documentList.contextActionHandler.next(value); expect(documentList.contextActionCallback).toHaveBeenCalledWith(value); @@ -427,7 +348,7 @@ describe('DocumentList', () => { done(); }); documentList.navigationMode = DocumentList.SINGLE_CLICK_NAVIGATION; - documentList.onItemClick(file, null); + documentList.onNodeClick(file); }); it('should emit file preview event on double click', (done) => { @@ -437,7 +358,7 @@ describe('DocumentList', () => { done(); }); documentList.navigationMode = DocumentList.DOUBLE_CLICK_NAVIGATION; - documentList.onItemDblClick(file, null); + documentList.onNodeDblClick(file); }); it('should perform folder navigation on single click', () => { @@ -445,7 +366,7 @@ describe('DocumentList', () => { spyOn(documentList, 'performNavigation').and.stub(); documentList.navigationMode = DocumentList.SINGLE_CLICK_NAVIGATION; - documentList.onItemClick(folder, null); + documentList.onNodeClick(folder); expect(documentList.performNavigation).toHaveBeenCalled(); }); @@ -454,7 +375,7 @@ describe('DocumentList', () => { spyOn(documentList, 'performNavigation').and.stub(); documentList.navigationMode = DocumentList.DOUBLE_CLICK_NAVIGATION; - documentList.onItemDblClick(folder, null); + documentList.onNodeDblClick(folder); expect(documentList.performNavigation).toHaveBeenCalled(); }); @@ -463,7 +384,7 @@ describe('DocumentList', () => { spyOn(documentList, 'performNavigation').and.stub(); documentList.navigationMode = DocumentList.SINGLE_CLICK_NAVIGATION; - documentList.onItemDblClick(folder, null); + documentList.onNodeDblClick(folder); expect(documentList.performNavigation).not.toHaveBeenCalled(); }); @@ -474,15 +395,16 @@ describe('DocumentList', () => { documentList.navigate = false; documentList.navigationMode = DocumentList.DOUBLE_CLICK_NAVIGATION; - documentList.onItemDblClick(folder, null); + documentList.onNodeDblClick(folder); expect(documentList.performNavigation).not.toHaveBeenCalled(); }); it('should perform navigation for folder node only', () => { + spyOn(documentList, 'getNodePath').and.returnValue('/path'); + let folder = new FolderNode(); let file = new FileNode(); - spyOn(documentList, 'getNodePath').and.returnValue('/path'); expect(documentList.performNavigation(folder)).toBeTruthy(); expect(documentList.performNavigation(file)).toBeFalsy(); @@ -499,7 +421,6 @@ describe('DocumentList', () => { expect(documentList.getNodePath(file)).toBe('/folder1/file.txt'); }); - it('should require valid node for file preview', () => { let file = new FileNode(); file.entry = null; @@ -508,11 +429,11 @@ describe('DocumentList', () => { documentList.navigationMode = DocumentList.SINGLE_CLICK_NAVIGATION; documentList.preview.subscribe(val => called = true); - documentList.onItemClick(file, null); + documentList.onNodeClick(file); expect(called).toBeFalsy(); documentList.navigationMode = DocumentList.DOUBLE_CLICK_NAVIGATION; - documentList.onItemDblClick(file, null); + documentList.onNodeDblClick(file); expect(called).toBeFalsy(); }); @@ -522,10 +443,10 @@ describe('DocumentList', () => { spyOn(documentList, 'performNavigation').and.stub(); documentList.navigationMode = DocumentList.SINGLE_CLICK_NAVIGATION; - documentList.onItemClick(folder, null); + documentList.onNodeClick(folder); documentList.navigationMode = DocumentList.DOUBLE_CLICK_NAVIGATION; - documentList.onItemDblClick(folder, null); + documentList.onNodeDblClick(folder); expect(documentList.performNavigation).not.toHaveBeenCalled(); }); @@ -536,6 +457,8 @@ describe('DocumentList', () => { expect(documentList.displayFolderContent).toHaveBeenCalled(); }); + // TODO: move to data adapter + /* it('should generate thumbnail for unknown content', () => { documentList.baseComponentPath = '/root'; let node = new FileNode(); @@ -543,13 +466,19 @@ describe('DocumentList', () => { expect(documentList.getThumbnailUrl(node)).toBe('/root/img/ft_ic_miscellaneous.svg'); }); + */ + // TODO: move to data adapter + /* it('should generate folder icon path', () => { documentList.baseComponentPath = '/root'; let folder = new FolderNode(); expect(documentList.getThumbnailUrl(folder)).toBe('/root/img/ft_ic_folder.svg'); }); + */ + // TODO: move to data adapter + /* it('should generate file icon path based on mime type', () => { let fileName = 'custom-icon.svg'; spyOn(alfrescoServiceMock, 'getMimeTypeIcon').and.returnValue(fileName); @@ -560,9 +489,12 @@ describe('DocumentList', () => { expect(documentList.getThumbnailUrl(file)).toBe(`/root/img/${fileName}`); }); + */ + // TODO: move to data adapter + /* it('should fallback to default icon for missing mime type', () => { - spyOn(alfrescoServiceMock, 'getMimeTypeIcon').and.returnValue(null); + spyOn(documentListService, 'getMimeTypeIcon').and.returnValue(null); documentList.baseComponentPath = '/root'; let file = new FileNode(); @@ -570,9 +502,12 @@ describe('DocumentList', () => { expect(documentList.getThumbnailUrl(file)).toBe('/root/img/ft_ic_miscellaneous.svg'); }); + */ + // TODO: move to data adapter + /* it('should fallback to default icon for unknown mime type', () => { - spyOn(alfrescoServiceMock, 'getMimeTypeIcon').and.returnValue(null); + spyOn(documentListService, 'getMimeTypeIcon').and.returnValue(null); documentList.baseComponentPath = '/root'; let file = new FileNode(); @@ -580,17 +515,23 @@ describe('DocumentList', () => { expect(documentList.getThumbnailUrl(file)).toBe('/root/img/ft_ic_miscellaneous.svg'); }); + */ + // TODO: move to data adapter + /* it('should resolve thumbnail url for a file', () => { let url = 'http://<some url>'; - spyOn(alfrescoServiceMock, 'getDocumentThumbnailUrl').and.returnValue(url); + spyOn(documentListService, 'getDocumentThumbnailUrl').and.returnValue(url); documentList.thumbnails = true; let file = new FileNode(); expect(documentList.getThumbnailUrl(file)).toBe(url); }); + */ + // TODO: move to data adapter + /* it('should return no thumbnail url with missing service', () => { let list = new DocumentList(null, null); list.thumbnails = true; @@ -598,76 +539,10 @@ describe('DocumentList', () => { let file = new FileNode(); expect(list.getThumbnailUrl(file)).toBeNull(); }); + */ - it('should sort on column header click', () => { - let col = new ContentColumnModel(); - col.source = 'id'; - - spyOn(documentList, 'sort').and.callThrough(); - - documentList.onColumnHeaderClick(col); - - expect(documentList.sorting).toEqual( - jasmine.objectContaining({ - key: 'id', - direction: 'asc' - }) - ); - expect(documentList.sort).toHaveBeenCalled(); - }); - - it('should invert sorting on column header click', () => { - let col = new ContentColumnModel(); - col.source = 'id'; - - spyOn(documentList, 'sort').and.callThrough(); - - documentList.sorting = <ColumnSortingModel> { key: 'id', direction: 'asc' }; - documentList.onColumnHeaderClick(col); - - expect(documentList.sorting).toEqual( - jasmine.objectContaining({ - key: 'id', - direction: 'desc' - }) - ); - - documentList.onColumnHeaderClick(col); - expect(documentList.sorting).toEqual( - jasmine.objectContaining({ - key: 'id', - direction: 'asc' - }) - ); - - expect(documentList.sort).toHaveBeenCalledTimes(2); - }); - - it('should use ascending direction for different column header click', () => { - let col = new ContentColumnModel(); - col.source = 'id'; - - spyOn(documentList, 'sort').and.callThrough(); - - documentList.sorting = <ColumnSortingModel> { key: 'col1', direction: 'desc' }; - documentList.onColumnHeaderClick(col); - - expect(documentList.sorting).toEqual( - jasmine.objectContaining({ - key: 'id', - direction: 'asc' - }) - ); - - expect(documentList.sort).toHaveBeenCalled(); - }); - - it('should not sort by column header when instance is missing', () => { - spyOn(documentList, 'sort').and.callThrough(); - documentList.onColumnHeaderClick(null); - expect(documentList.sort).not.toHaveBeenCalled(); - }); - + // TODO: move to DataTable + /* it('should convert cell value to formatted date', () => { let rawValue = new Date(2015, 6, 15, 21, 43, 11).toString(); // Wed Jul 15 2015 21:43:11 GMT+0100 (BST); @@ -684,7 +559,10 @@ describe('DocumentList', () => { let value = documentList.getCellValue(file, col); expect(value).toBe(dateValue); }); + */ + // TODO: move to DataTable + /* it('should return date value as string', () => { let rawValue = new Date(2015, 6, 15, 21, 43, 11).toString(); // Wed Jul 15 2015 21:43:11 GMT+0100 (BST); @@ -698,7 +576,10 @@ describe('DocumentList', () => { let value = documentList.getCellValue(file, col); expect(value).toBe(rawValue); }); + */ + // TODO: move to data adapter + /* it('should convert cell value to thumbnail', () => { let url = 'http://<address>'; spyOn(documentList, 'getThumbnailUrl').and.returnValue(url); @@ -712,14 +593,15 @@ describe('DocumentList', () => { let value = documentList.getCellValue(file, col); expect(value).toBe(url); }); + */ it('should require path to display folder content', () => { - spyOn(alfrescoServiceMock, 'getFolder').and.callThrough(); + spyOn(documentListService, 'getFolder').and.callThrough(); documentList.displayFolderContent(null); documentList.displayFolderContent(''); - expect(alfrescoServiceMock.getFolder).not.toHaveBeenCalled(); + expect(documentListService.getFolder).not.toHaveBeenCalled(); }); it('should require node to resolve context menu actions', () => { @@ -733,12 +615,12 @@ describe('DocumentList', () => { it('should fetch context menu actions for a file node', () => { let actionModel = {}; - spyOn(documentList, 'getContentActions').and.returnValue([actionModel]); + spyOn(documentList, 'getNodeActions').and.returnValue([actionModel]); let file = new FileNode(); let actions = documentList.getContextActions(file); - expect(documentList.getContentActions).toHaveBeenCalledWith('document', 'menu'); + expect(documentList.getNodeActions).toHaveBeenCalledWith(file); expect(actions.length).toBe(1); expect(actions[0].model).toBe(actionModel); expect(actions[0].node).toBe(file); @@ -747,12 +629,12 @@ describe('DocumentList', () => { it('should fetch context menu actions for a folder node', () => { let actionModel = {}; - spyOn(documentList, 'getContentActions').and.returnValue([actionModel]); + spyOn(documentList, 'getNodeActions').and.returnValue([actionModel]); let folder = new FolderNode(); let actions = documentList.getContextActions(folder); - expect(documentList.getContentActions).toHaveBeenCalledWith('folder', 'menu'); + expect(documentList.getNodeActions).toHaveBeenCalledWith(folder); expect(actions.length).toBe(1); expect(actions[0].model).toBe(actionModel); expect(actions[0].node).toBe(folder); @@ -760,51 +642,39 @@ describe('DocumentList', () => { }); it('should fetch no context menu actions for unknown type', () => { - spyOn(documentList, 'getContentActions').and.stub(); + spyOn(documentList, 'getNodeActions').and.stub(); let node = new FileNode(); node.entry.isFile = false; node.entry.isFolder = false; let actions = documentList.getContextActions(node); - - expect(documentList.getContentActions).not.toHaveBeenCalled(); expect(actions).toBeNull(); }); it('should return null value when no content actions found', () => { - spyOn(documentList, 'getContentActions').and.returnValue([]); + spyOn(documentList, 'getNodeActions').and.returnValue([]); let file = new FileNode(); let actions = documentList.getContextActions(file); expect(actions).toBeNull(); - expect(documentList.getContentActions).toHaveBeenCalled(); + expect(documentList.getNodeActions).toHaveBeenCalled(); }); + /* it('should update error message when folder content display fails', () => { let error = 'My Error'; - alfrescoServiceMock.getFolderReject = true; - alfrescoServiceMock.getFolderRejectError = error; + documentListService.getFolderReject = true; + documentListService.getFolderRejectError = error; documentList.displayFolderContent('/some/path'); expect(documentList.errorMessage).toBe(error); }); + */ - it('should get object value via property path', () => { - let obj = { - name: { - firstName: '<name>' - } - }; - - expect(documentList.getObjectValue(obj, 'name.firstName')).toBe('<name>'); - }); - - it('should not get object value via invalid path', () => { - expect(documentList.getObjectValue({}, 'some.missing.path')).toBeUndefined(); - }); - + // TODO: move to data adapter + /* it('should log error when having date conversion issues', () => { let value = '<wrong-date>'; @@ -824,7 +694,10 @@ describe('DocumentList', () => { expect(result).toBe(value); expect(console.error).toHaveBeenCalledWith(`DocumentList: error parsing date ${value} to format ${col.format}`); }); + */ + // TODO: move to data adapter + /* it('should convert thumbnail if column source defined', () => { let file = new FileNode(); let col = new ContentColumnModel({ @@ -834,6 +707,7 @@ describe('DocumentList', () => { expect(documentList.getCellValue(file, col)).toBe(file.entry.name); }); + */ it('should require current folder path to reload', () => { @@ -850,6 +724,8 @@ describe('DocumentList', () => { expect(documentList.displayFolderContent).not.toHaveBeenCalled(); }); + // TODO: move to data adapter + /* it('should not sort empty page', () => { let page = new PageNode(); spyOn(page.list.entries, 'sort').and.stub(); @@ -857,7 +733,10 @@ describe('DocumentList', () => { documentList.sort(page, null); expect(page.list.entries.sort).not.toHaveBeenCalled(); }); + */ + // TODO: move to data adapter + /* it('should put folders to top on sort', () => { let folder = new FolderNode(); let file1 = new FileNode('file1'); @@ -884,7 +763,10 @@ describe('DocumentList', () => { expect(page.list.entries[1]).toBe(file2); expect(page.list.entries[2]).toBe(file1); }); + */ + // TODO: move to data adapter + /* it('should sort by dates up to ms', () => { let file1 = new FileNode(); file1.entry['dateProp'] = new Date(2016, 6, 30, 13, 14, 1); @@ -912,6 +794,6 @@ describe('DocumentList', () => { expect(page.list.entries[0]).toBe(file1); expect(page.list.entries[1]).toBe(file2); }); + */ }); -*/ diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/document-list.ts b/ng2-components/ng2-alfresco-documentlist/src/components/document-list.ts index ca3741ba5b..26bb14d6b9 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/document-list.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/document-list.ts @@ -158,7 +158,7 @@ export class DocumentList implements OnInit, AfterViewInit, AfterViewChecked, Af this.contextActionHandler.subscribe(val => this.contextActionCallback(val)); } - ngOnChanges(/*change*/) { + ngOnChanges() { this.reload(); } @@ -189,22 +189,25 @@ export class DocumentList implements OnInit, AfterViewInit, AfterViewChecked, Af getNodeActions(node: MinimalNodeEntity): ContentActionModel[] { let target = null; - if (node.entry.isFile) { - target = 'document'; + if (node && node.entry) { + if (node.entry.isFile) { + target = 'document'; + } + + if (node.entry.isFolder) { + target = 'folder'; + } + + if (target) { + + let ltarget = target.toLowerCase(); + + return this.actions.filter(entry => { + return entry.target.toLowerCase() === ltarget; + }); + } } - if (node.entry.isFolder) { - target = 'folder'; - } - - if (target) { - - let ltarget = target.toLowerCase(); - - return this.actions.filter(entry => { - return entry.target.toLowerCase() === ltarget; - }); - } return []; } @@ -234,9 +237,7 @@ export class DocumentList implements OnInit, AfterViewInit, AfterViewChecked, Af } displayFolderContent(path: string) { - if (path) { - this.data.loadPath(path); - } + this.data.loadPath(path); } reload() { @@ -282,49 +283,58 @@ export class DocumentList implements OnInit, AfterViewInit, AfterViewChecked, Af this.data.setColumns([colThumbnail, colName]); } - onRowClick(event: DataRowEvent) { - let item = (<ShareDataRow> event.value).node; + onPreviewFile(node: MinimalNodeEntity) { + if (node) { + this.preview.emit({ + value: node + }); + } + } + onNodeClick(node: MinimalNodeEntity) { this.nodeClick.emit({ - value: item + value: node }); if (this.navigate && this.navigationMode === DocumentList.SINGLE_CLICK_NAVIGATION) { - if (item && item.entry) { - if (item.entry.isFile) { - this.preview.emit({ - value: item - }); + if (node && node.entry) { + if (node.entry.isFile) { + this.onPreviewFile(node); } - if (item.entry.isFolder) { - this.performNavigation(item); + if (node.entry.isFolder) { + this.performNavigation(node); } } } + } + onRowClick(event: DataRowEvent) { + let item = (<ShareDataRow> event.value).node; + this.onNodeClick(item); + } + + onNodeDblClick(node: MinimalNodeEntity) { + this.nodeDblClick.emit({ + value: node + }); + + if (this.navigate && this.navigationMode === DocumentList.DOUBLE_CLICK_NAVIGATION) { + if (node && node.entry) { + if (node.entry.isFile) { + this.onPreviewFile(node); + } + + if (node.entry.isFolder) { + this.performNavigation(node); + } + } + } } onRowDblClick(event?: DataRowEvent) { let item = (<ShareDataRow> event.value).node; - - this.nodeDblClick.emit({ - value: item - }); - - if (this.navigate && this.navigationMode === DocumentList.DOUBLE_CLICK_NAVIGATION) { - if (item && item.entry) { - if (item.entry.isFile) { - this.preview.emit({ - value: item - }); - } - - if (item.entry.isFolder) { - this.performNavigation(item); - } - } - } + this.onNodeDblClick(item); } onShowRowContextMenu(event) { diff --git a/ng2-components/ng2-alfresco-documentlist/src/data/share-datatable-adapter.ts b/ng2-components/ng2-alfresco-documentlist/src/data/share-datatable-adapter.ts index 5ff5b8f2c2..95b1cc5ff8 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/data/share-datatable-adapter.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/data/share-datatable-adapter.ts @@ -168,19 +168,23 @@ export class ShareDataTableAdapter implements DataTableAdapter { .getFolder(path) .subscribe(val => { let page = <NodePaging>val; - let data = page.list.entries; - if (data && data.length > 0) { - this.rows = data.map(item => new ShareDataRow(item)); - // Sort by first sortable or just first column - let sortable = this.columns.filter(c => c.sortable); - if (sortable.length > 0) { - this.sort(sortable[0].key, 'asc'); - } else { - this.sort(this.columns[0].key, 'asc'); + let rows = []; + + if (page && page.list) { + let data = page.list.entries; + if (data && data.length > 0) { + rows = data.map(item => new ShareDataRow(item)); + // Sort by first sortable or just first column + let sortable = this.columns.filter(c => c.sortable); + if (sortable.length > 0) { + this.sort(sortable[0].key, 'asc'); + } else { + this.sort(this.columns[0].key, 'asc'); + } } - } else { - this.rows = []; } + + this.rows = []; }, error => console.log(error)); }