mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-26 17:24:56 +00:00
Merge pull request #389 from Alfresco/dev-denys-tests
Unit tests and Travis config for document list
This commit is contained in:
commit
8314fcb557
@ -17,8 +17,7 @@ before_install:
|
||||
- export CHROME_BIN=/usr/bin/google-chrome
|
||||
- export DISPLAY=:99.0
|
||||
- sh -e /etc/init.d/xvfb start
|
||||
- (cd ng2-components/ng2-alfresco-core; npm install; npm link)
|
||||
- (cd ng2-components/ng2-alfresco-datatable; npm install; npm link)
|
||||
- (cd ng2-components/ng2-alfresco-core; npm install; npm link);(cd ng2-components/ng2-alfresco-datatable; npm link ng2-alfresco-core; npm install; npm link)
|
||||
|
||||
env:
|
||||
matrix:
|
||||
@ -31,7 +30,7 @@ env:
|
||||
- MODULE=ng2-alfresco-viewer
|
||||
|
||||
before_script:
|
||||
- cd ng2-components/$MODULE; npm install; npm link ng2-alfresco-core; npm run travis
|
||||
- cd ng2-components/$MODULE; npm install; npm run travis
|
||||
- ls -ltrh ./node_modules/
|
||||
script: npm run test
|
||||
# Send coverage data to Coveralls
|
||||
|
@ -37,6 +37,8 @@ export * from './src/services/context-menu.service';
|
||||
export * from './src/components/context-menu-holder.component';
|
||||
export * from './src/components/context-menu.directive';
|
||||
|
||||
export * from './src/utils/object-utils';
|
||||
|
||||
export const ALFRESCO_CORE_PROVIDERS: [any] = [
|
||||
AlfrescoAuthenticationService,
|
||||
AlfrescoContentService,
|
||||
|
@ -0,0 +1,59 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
it,
|
||||
describe,
|
||||
expect
|
||||
} from '@angular/core/testing';
|
||||
import { ObjectUtils } from './object-utils';
|
||||
|
||||
describe('ObjectUtils', () => {
|
||||
|
||||
it('should get top level property value', () => {
|
||||
let obj = {
|
||||
id: 1
|
||||
};
|
||||
expect(ObjectUtils.getValue(obj, 'id')).toBe(1);
|
||||
});
|
||||
|
||||
it('should not get top level property value', () => {
|
||||
let obj = {};
|
||||
expect(ObjectUtils.getValue(obj, 'missing')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should get nested property value', () => {
|
||||
let obj = {
|
||||
name: {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe'
|
||||
}
|
||||
};
|
||||
|
||||
expect(ObjectUtils.getValue(obj, 'name.lastName')).toBe('Doe');
|
||||
});
|
||||
|
||||
it('should not get nested property value', () => {
|
||||
let obj = {};
|
||||
expect(ObjectUtils.getValue(obj, 'some.missing.property')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined when getting value for missing target', () => {
|
||||
expect(ObjectUtils.getValue(null, 'id')).toBeUndefined();
|
||||
});
|
||||
|
||||
});
|
50
ng2-components/ng2-alfresco-core/src/utils/object-utils.ts
Normal file
50
ng2-components/ng2-alfresco-core/src/utils/object-utils.ts
Normal file
@ -0,0 +1,50 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export class ObjectUtils {
|
||||
/**
|
||||
* Gets a value from an object by composed key
|
||||
* ObjectUtils.getValue({ item: { nodeType: 'cm:folder' }}, 'item.nodeType') ==> 'cm:folder'
|
||||
* @param target
|
||||
* @param key
|
||||
* @returns {string}
|
||||
*/
|
||||
static getValue(target: any, key: string): any {
|
||||
|
||||
if (!target) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let keys = key.split('.');
|
||||
key = '';
|
||||
|
||||
do {
|
||||
key += keys.shift();
|
||||
let value = target[key];
|
||||
if (value !== undefined && (typeof value === 'object' || !keys.length)) {
|
||||
target = value;
|
||||
key = '';
|
||||
} else if (!keys.length) {
|
||||
target = undefined;
|
||||
} else {
|
||||
key += '.';
|
||||
}
|
||||
} while (keys.length);
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
@ -347,11 +347,6 @@ describe('ObjectDataRow', () => {
|
||||
expect(row.getValue('some.missing.property')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined when getting value for missing target', () => {
|
||||
let row = new ObjectDataRow({});
|
||||
expect(row.getObjectValue(null, 'id')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should check top level value exists', () => {
|
||||
let row = new ObjectDataRow({ id: 1 });
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
import { DatePipe } from '@angular/common';
|
||||
import { ObjectUtils } from 'ng2-alfresco-core';
|
||||
|
||||
import {
|
||||
DataTableAdapter,
|
||||
@ -147,40 +148,8 @@ export class ObjectDataRow implements DataRow {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value from an object by composed key
|
||||
* documentList.getObjectValue({ item: { nodeType: 'cm:folder' }}, 'item.nodeType') ==> 'cm:folder'
|
||||
* @param target
|
||||
* @param key
|
||||
* @returns {string}
|
||||
*/
|
||||
getObjectValue(target: any, key: string): any {
|
||||
|
||||
if (!target) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let keys = key.split('.');
|
||||
key = '';
|
||||
|
||||
do {
|
||||
key += keys.shift();
|
||||
let value = target[key];
|
||||
if (value !== undefined && (typeof value === 'object' || !keys.length)) {
|
||||
target = value;
|
||||
key = '';
|
||||
} else if (!keys.length) {
|
||||
target = undefined;
|
||||
} else {
|
||||
key += '.';
|
||||
}
|
||||
} while (keys.length);
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
getValue(key: string): any {
|
||||
return this.getObjectValue(this.obj, key);
|
||||
return ObjectUtils.getValue(this.obj, key);
|
||||
}
|
||||
|
||||
hasValue(key: string): boolean {
|
||||
|
@ -33,7 +33,7 @@ export class PageNode extends NodePaging {
|
||||
}
|
||||
|
||||
export class FileNode extends MinimalNodeEntity {
|
||||
constructor(name?: string) {
|
||||
constructor(name?: string, mimeType?: string) {
|
||||
super();
|
||||
this.entry = new MinimalNodeEntryEntity();
|
||||
this.entry.id = 'file-id';
|
||||
@ -42,6 +42,7 @@ export class FileNode extends MinimalNodeEntity {
|
||||
this.entry.name = name;
|
||||
this.entry.path = new PathInfoEntity();
|
||||
this.entry.content = new ContentInfo();
|
||||
this.entry.content.mimeType = mimeType || 'text/plain';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,8 @@
|
||||
*/
|
||||
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { NodePaging } from './../models/document-library.model';
|
||||
import { PageNode } from './document-library.model.mock';
|
||||
import { DocumentListService } from './../services/document-list.service';
|
||||
import {
|
||||
AlfrescoSettingsService,
|
||||
@ -25,7 +27,7 @@ import {
|
||||
|
||||
export class DocumentListServiceMock extends DocumentListService {
|
||||
|
||||
folderToReturn: any = {};
|
||||
getFolderResult: NodePaging = new PageNode();
|
||||
getFolderReject: boolean = false;
|
||||
getFolderRejectError: string = 'Error';
|
||||
|
||||
@ -42,7 +44,7 @@ export class DocumentListServiceMock extends DocumentListService {
|
||||
return Observable.throw(this.getFolderRejectError);
|
||||
}
|
||||
return Observable.create(observer => {
|
||||
observer.next(this.folderToReturn);
|
||||
observer.next(this.getFolderResult);
|
||||
observer.complete();
|
||||
});
|
||||
}
|
||||
|
@ -75,47 +75,6 @@ describe('DocumentList', () => {
|
||||
expect(columns[0]).toBe(column);
|
||||
});
|
||||
|
||||
// TODO: move to data adapter
|
||||
/*
|
||||
it('should fetch folder', () => {
|
||||
let folder = {
|
||||
'nodeRef': 'workspace://SpacesStore/8bb36efb-c26d-4d2b-9199-ab6922f53c28'
|
||||
};
|
||||
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(documentListService, 'getDocumentThumbnailUrl').and.returnValue(url);
|
||||
|
||||
let node = new FileNode();
|
||||
documentList.thumbnails = true;
|
||||
let result = documentList.getThumbnailUrl(node);
|
||||
|
||||
expect(result).toBe(url);
|
||||
expect(documentListService.getDocumentThumbnailUrl).toHaveBeenCalled();
|
||||
});
|
||||
*/
|
||||
|
||||
// TODO: move to data adapter
|
||||
/*
|
||||
it('should return a null thumbnail url for a null item', () => {
|
||||
let url = 'URL';
|
||||
spyOn(documentListService, 'getDocumentThumbnailUrl').and.returnValue(url);
|
||||
|
||||
let result = documentList.getThumbnailUrl(null);
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(documentListService.getDocumentThumbnailUrl).not.toHaveBeenCalled();
|
||||
});
|
||||
*/
|
||||
|
||||
it('should execute action with node', () => {
|
||||
let node = new FileNode();
|
||||
let action = new ContentActionModel();
|
||||
@ -457,144 +416,6 @@ 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();
|
||||
node.entry.isFile = false;
|
||||
|
||||
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);
|
||||
documentList.baseComponentPath = '/root';
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.content.mimeType = 'text/plain';
|
||||
|
||||
expect(documentList.getThumbnailUrl(file)).toBe(`/root/img/${fileName}`);
|
||||
});
|
||||
*/
|
||||
|
||||
// TODO: move to data adapter
|
||||
/*
|
||||
it('should fallback to default icon for missing mime type', () => {
|
||||
spyOn(documentListService, 'getMimeTypeIcon').and.returnValue(null);
|
||||
documentList.baseComponentPath = '/root';
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.content.mimeType = null;
|
||||
|
||||
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(documentListService, 'getMimeTypeIcon').and.returnValue(null);
|
||||
documentList.baseComponentPath = '/root';
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.content.mimeType = 'text/plain';
|
||||
|
||||
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(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;
|
||||
|
||||
let file = new FileNode();
|
||||
expect(list.getThumbnailUrl(file)).toBeNull();
|
||||
});
|
||||
*/
|
||||
|
||||
// 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);
|
||||
let dateValue = 'Jul 15, 2015, 9:43:11 PM';
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.createdAt = rawValue;
|
||||
|
||||
let col = new ContentColumnModel();
|
||||
col.source = 'createdAt';
|
||||
col.type = 'date';
|
||||
col.format = 'medium'; // Jul 15, 2015, 9:43:11 PM
|
||||
|
||||
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);
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.createdAt = rawValue;
|
||||
|
||||
let col = new ContentColumnModel();
|
||||
col.source = 'createdAt';
|
||||
col.type = 'string';
|
||||
|
||||
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);
|
||||
|
||||
let file = new FileNode();
|
||||
|
||||
let col = new ContentColumnModel();
|
||||
col.source = '$thumbnail';
|
||||
col.type = 'image';
|
||||
|
||||
let value = documentList.getCellValue(file, col);
|
||||
expect(value).toBe(url);
|
||||
});
|
||||
*/
|
||||
|
||||
it('should require path to display folder content', () => {
|
||||
spyOn(documentListService, 'getFolder').and.callThrough();
|
||||
|
||||
@ -662,53 +483,6 @@ describe('DocumentList', () => {
|
||||
expect(documentList.getNodeActions).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
/*
|
||||
it('should update error message when folder content display fails', () => {
|
||||
let error = 'My Error';
|
||||
documentListService.getFolderReject = true;
|
||||
documentListService.getFolderRejectError = error;
|
||||
|
||||
documentList.displayFolderContent('/some/path');
|
||||
expect(documentList.errorMessage).toBe(error);
|
||||
});
|
||||
*/
|
||||
|
||||
// TODO: move to data adapter
|
||||
/*
|
||||
it('should log error when having date conversion issues', () => {
|
||||
|
||||
let value = '<wrong-date>';
|
||||
let file = new FileNode();
|
||||
file.entry.createdAt = value;
|
||||
|
||||
let col = new ContentColumnModel({
|
||||
source: 'createdAt',
|
||||
type: 'date',
|
||||
format: 'medium'
|
||||
});
|
||||
|
||||
spyOn(console, 'error').and.stub();
|
||||
|
||||
let result = documentList.getCellValue(file, col);
|
||||
|
||||
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({
|
||||
source: 'name',
|
||||
type: 'image'
|
||||
});
|
||||
|
||||
expect(documentList.getCellValue(file, col)).toBe(file.entry.name);
|
||||
});
|
||||
*/
|
||||
|
||||
it('should require current folder path to reload', () => {
|
||||
|
||||
// Redefine 'currentFolderPath' to disable native setter validation
|
||||
@ -724,47 +498,6 @@ 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();
|
||||
|
||||
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');
|
||||
let file2 = new FileNode('file2');
|
||||
let page = new PageNode([file1, file2, folder]);
|
||||
|
||||
// asc
|
||||
documentList.sort(page, new ColumnSortingModel({
|
||||
key: 'name',
|
||||
direction: 'asc'
|
||||
}));
|
||||
|
||||
expect(page.list.entries[0]).toBe(folder);
|
||||
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);
|
||||
});
|
||||
*/
|
||||
|
||||
// TODO: move to data adapter
|
||||
/*
|
||||
it('should sort by dates up to ms', () => {
|
||||
|
@ -0,0 +1,431 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { it, describe, expect, beforeEach } from '@angular/core/testing';
|
||||
import { DataColumn, DataRow, DataSorting } from 'ng2-alfresco-datatable';
|
||||
import { DocumentListServiceMock } from './../assets/document-list.service.mock';
|
||||
import { ShareDataTableAdapter, ShareDataRow } from './share-datatable-adapter';
|
||||
import { FileNode, FolderNode, PageNode } from './../assets/document-library.model.mock';
|
||||
|
||||
describe('ShareDataTableAdapter', () => {
|
||||
|
||||
let basePath: string;
|
||||
let documentListService: DocumentListServiceMock;
|
||||
|
||||
beforeEach(() => {
|
||||
basePath = '/root';
|
||||
documentListService = new DocumentListServiceMock();
|
||||
});
|
||||
|
||||
it('should setup rows and columns with constructor', () => {
|
||||
let schema = [<DataColumn> {}];
|
||||
let adapter = new ShareDataTableAdapter(null, null, schema);
|
||||
|
||||
expect(adapter.getRows()).toEqual([]);
|
||||
expect(adapter.getColumns()).toEqual(schema);
|
||||
});
|
||||
|
||||
it('should setup columns when constructor is missing schema', () => {
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
|
||||
expect(adapter.getColumns()).toEqual([]);
|
||||
});
|
||||
|
||||
it('should set new columns', () => {
|
||||
let columns = [<DataColumn> {}, <DataColumn> {}];
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
adapter.setColumns(columns);
|
||||
expect(adapter.getColumns()).toEqual(columns);
|
||||
});
|
||||
|
||||
it('should reset columns', () => {
|
||||
let columns = [<DataColumn> {}, <DataColumn> {}];
|
||||
let adapter = new ShareDataTableAdapter(null, null, columns);
|
||||
|
||||
expect(adapter.getColumns()).toEqual(columns);
|
||||
adapter.setColumns(null);
|
||||
expect(adapter.getColumns()).toEqual([]);
|
||||
});
|
||||
|
||||
it('should set new rows', () => {
|
||||
let rows = [<DataRow> {}, <DataRow> {}];
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
|
||||
expect(adapter.getRows()).toEqual([]);
|
||||
adapter.setRows(rows);
|
||||
expect(adapter.getRows()).toEqual(rows);
|
||||
});
|
||||
|
||||
it('should reset rows', () => {
|
||||
let rows = [<DataRow> {}, <DataRow> {}];
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
|
||||
adapter.setRows(rows);
|
||||
expect(adapter.getRows()).toEqual(rows);
|
||||
|
||||
adapter.setRows(null);
|
||||
expect(adapter.getRows()).toEqual([]);
|
||||
});
|
||||
|
||||
it('should sort new rows', () => {
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
spyOn(adapter, 'sort').and.callThrough();
|
||||
|
||||
let rows = [<DataRow> {}];
|
||||
adapter.setRows(rows);
|
||||
|
||||
expect(adapter.sort).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail when getting value for missing row', () => {
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
let check = () => { return adapter.getValue(null, <DataColumn>{}); };
|
||||
expect(check).toThrowError(ShareDataTableAdapter.ERR_ROW_NOT_FOUND);
|
||||
});
|
||||
|
||||
it('should fail when getting value for missing column', () => {
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
let check = () => { return adapter.getValue(<DataRow>{}, null); };
|
||||
expect(check).toThrowError(ShareDataTableAdapter.ERR_COL_NOT_FOUND);
|
||||
});
|
||||
|
||||
it('should require path to load data', () => {
|
||||
spyOn(documentListService, 'getFolder').and.callThrough();
|
||||
|
||||
let adapter = new ShareDataTableAdapter(documentListService, null, null);
|
||||
adapter.loadPath(null);
|
||||
|
||||
expect(documentListService.getFolder).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should load data for path', () => {
|
||||
let folder = new FolderNode();
|
||||
let path = '/some/path';
|
||||
let page = new PageNode([folder]);
|
||||
|
||||
spyOn(documentListService, 'getFolder').and.callThrough();
|
||||
documentListService.getFolderResult = page;
|
||||
|
||||
let adapter = new ShareDataTableAdapter(documentListService, null, null);
|
||||
adapter.loadPath(path);
|
||||
|
||||
expect(documentListService.getFolder).toHaveBeenCalledWith(path);
|
||||
|
||||
let rows = adapter.getRows();
|
||||
expect(rows.length).toBe(1);
|
||||
expect((<ShareDataRow>rows[0]).node).toBe(folder);
|
||||
});
|
||||
|
||||
it('should covert 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);
|
||||
let dateValue = 'Jul 15, 2015, 9:43:11 PM';
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.createdAt = rawValue;
|
||||
|
||||
let col = <DataColumn> {
|
||||
key: 'createdAt',
|
||||
type: 'date',
|
||||
format: 'medium' // Jul 15, 2015, 9:43:11 PM
|
||||
};
|
||||
|
||||
let row = new ShareDataRow(file);
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(dateValue);
|
||||
});
|
||||
|
||||
it('should use default date format as fallback', () => {
|
||||
let rawValue = new Date(2015, 6, 15, 21, 43, 11).toString(); // Wed Jul 15 2015 21:43:11 GMT+0100 (BST);
|
||||
let dateValue = 'Jul 15, 2015, 9:43:11 PM';
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.createdAt = rawValue;
|
||||
|
||||
let col = <DataColumn> {
|
||||
key: 'createdAt',
|
||||
type: 'date',
|
||||
format: null
|
||||
};
|
||||
|
||||
let row = new ShareDataRow(file);
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(dateValue);
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.createdAt = rawValue;
|
||||
|
||||
let col = <DataColumn> {
|
||||
key: 'createdAt',
|
||||
type: 'string'
|
||||
};
|
||||
|
||||
let row = new ShareDataRow(file);
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(rawValue);
|
||||
});
|
||||
|
||||
it('should log error when having date conversion issues', () => {
|
||||
let dateValue = '[wrong-date]';
|
||||
let file = new FileNode();
|
||||
file.entry.createdAt = dateValue;
|
||||
|
||||
let col = <DataColumn> {
|
||||
key: 'createdAt',
|
||||
type: 'date',
|
||||
format: 'medium'
|
||||
};
|
||||
|
||||
let row = new ShareDataRow(file);
|
||||
let adapter = new ShareDataTableAdapter(null, null, null);
|
||||
spyOn(console, 'error').and.stub();
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(dateValue);
|
||||
expect(console.error).toHaveBeenCalledWith(`Error parsing date ${value} to format ${col.format}`);
|
||||
});
|
||||
|
||||
it('should generate fallback icon for a file thumbnail with unknown mime type', () => {
|
||||
let adapter = new ShareDataTableAdapter(documentListService, basePath, null);
|
||||
|
||||
let file = new FileNode('file', 'wrong-mime');
|
||||
let row = new ShareDataRow(file);
|
||||
let col = <DataColumn> { type: 'image', key: '$thumbnail' };
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(`${basePath}/img/ft_ic_miscellaneous.svg`);
|
||||
});
|
||||
|
||||
it('should generate fallback icon for a file thumbnail with missing mime type', () => {
|
||||
let adapter = new ShareDataTableAdapter(documentListService, basePath, null);
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.content.mimeType = null;
|
||||
|
||||
let row = new ShareDataRow(file);
|
||||
let col = <DataColumn> { type: 'image', key: '$thumbnail' };
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(`${basePath}/img/ft_ic_miscellaneous.svg`);
|
||||
});
|
||||
|
||||
it('should generate fallback icon for a file with no content entry', () => {
|
||||
let adapter = new ShareDataTableAdapter(documentListService, basePath, null);
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.content = null;
|
||||
|
||||
let row = new ShareDataRow(file);
|
||||
let col = <DataColumn> { type: 'image', key: '$thumbnail' };
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(`${basePath}/img/ft_ic_miscellaneous.svg`);
|
||||
});
|
||||
|
||||
it('should generate fallback icon when document service fails to find one', () => {
|
||||
spyOn(documentListService, 'getMimeTypeIcon').and.returnValue(null);
|
||||
let adapter = new ShareDataTableAdapter(documentListService, basePath, null);
|
||||
|
||||
let file = new FileNode();
|
||||
let row = new ShareDataRow(file);
|
||||
let col = <DataColumn> { type: 'image', key: '$thumbnail' };
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(`${basePath}/img/ft_ic_miscellaneous.svg`);
|
||||
});
|
||||
|
||||
it('should return image value unmodified', () => {
|
||||
let imageUrl = 'http://<address>';
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry['icon'] = imageUrl;
|
||||
|
||||
|
||||
let adapter = new ShareDataTableAdapter(null, basePath, null);
|
||||
let row = new ShareDataRow(file);
|
||||
let col = <DataColumn> { type: 'image', key: 'icon' };
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(imageUrl);
|
||||
});
|
||||
|
||||
it('should resolve folder icon', () => {
|
||||
let adapter = new ShareDataTableAdapter(null, basePath, null);
|
||||
|
||||
let row = new ShareDataRow(new FolderNode());
|
||||
let col = <DataColumn> { type: 'image', key: '$thumbnail' };
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(`${basePath}/img/ft_ic_folder.svg`);
|
||||
});
|
||||
|
||||
it('should resolve file thumbnail', () => {
|
||||
let imageUrl: 'http://<addresss>';
|
||||
spyOn(documentListService, 'getDocumentThumbnailUrl').and.returnValue(imageUrl);
|
||||
|
||||
let adapter = new ShareDataTableAdapter(documentListService, basePath, null);
|
||||
adapter.thumbnails = true;
|
||||
|
||||
let file = new FileNode();
|
||||
let row = new ShareDataRow(file);
|
||||
let col = <DataColumn> { type: 'image', key: '$thumbnail' };
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(imageUrl);
|
||||
expect(documentListService.getDocumentThumbnailUrl).toHaveBeenCalledWith(file);
|
||||
});
|
||||
|
||||
it('should resolve fallback file icon for unknown node', () => {
|
||||
let adapter = new ShareDataTableAdapter(null, basePath, null);
|
||||
|
||||
let file = new FileNode();
|
||||
file.entry.isFile = false;
|
||||
file.entry.isFolder = false;
|
||||
|
||||
let row = new ShareDataRow(file);
|
||||
let col = <DataColumn> { type: 'image', key: '$thumbnail' };
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBe(`${basePath}/img/ft_ic_miscellaneous.svg`);
|
||||
});
|
||||
|
||||
it('should require document service to resolve thumbnail', () => {
|
||||
let adapter = new ShareDataTableAdapter(null, basePath, null);
|
||||
adapter.thumbnails = true;
|
||||
|
||||
let file = new FileNode();
|
||||
let row = new ShareDataRow(file);
|
||||
let col = <DataColumn> { type: 'image', key: '$thumbnail' };
|
||||
|
||||
let value = adapter.getValue(row, col);
|
||||
expect(value).toBeNull();
|
||||
});
|
||||
|
||||
it('should log load error', () => {
|
||||
let error = 'My Error';
|
||||
documentListService.getFolderReject = true;
|
||||
documentListService.getFolderRejectError = error;
|
||||
|
||||
spyOn(console, 'error').and.stub();
|
||||
spyOn(documentListService, 'getFolder').and.callThrough();
|
||||
|
||||
let adapter = new ShareDataTableAdapter(documentListService, null, null);
|
||||
adapter.loadPath('/some/path');
|
||||
|
||||
expect(documentListService.getFolder).toHaveBeenCalled();
|
||||
expect(console.error).toHaveBeenCalledWith(error);
|
||||
});
|
||||
|
||||
it('should generate file icon path based on mime type', () => {
|
||||
let fileName = 'custom-icon.svg';
|
||||
spyOn(documentListService, 'getMimeTypeIcon').and.returnValue(fileName);
|
||||
|
||||
let file = new FileNode('file1', 'text/plain');
|
||||
let row = new ShareDataRow(file);
|
||||
let col = <DataColumn> {type: 'image', key: '$thumbnail'};
|
||||
|
||||
let adapter = new ShareDataTableAdapter(documentListService, '/root', null);
|
||||
let value = adapter.getValue(row, col);
|
||||
|
||||
expect(value).toBe(`/root/img/${fileName}`);
|
||||
expect(documentListService.getMimeTypeIcon).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should put folders on top upon sort', () => {
|
||||
let file1 = new FileNode('file1');
|
||||
let file2 = new FileNode('file2');
|
||||
let folder = new FolderNode();
|
||||
|
||||
let col = <DataColumn> { key: 'name' };
|
||||
let adapter = new ShareDataTableAdapter(null, null, [col]);
|
||||
adapter.setSorting(new DataSorting('name', 'asc'));
|
||||
|
||||
adapter.setRows([
|
||||
new ShareDataRow(file2),
|
||||
new ShareDataRow(file1),
|
||||
new ShareDataRow(folder)
|
||||
]);
|
||||
|
||||
let sorted = adapter.getRows();
|
||||
expect((<ShareDataRow> sorted[0]).node).toBe(folder);
|
||||
expect((<ShareDataRow> sorted[1]).node).toBe(file1);
|
||||
expect((<ShareDataRow> sorted[2]).node).toBe(file2);
|
||||
});
|
||||
|
||||
it('should sort by dates up to ms', () => {
|
||||
let file1 = new FileNode('file1');
|
||||
file1.entry['dateProp'] = new Date(2016, 6, 30, 13, 14, 1);
|
||||
|
||||
let file2 = new FileNode('file2');
|
||||
file2.entry['dateProp'] = new Date(2016, 6, 30, 13, 14, 2);
|
||||
|
||||
let col = <DataColumn> { key: 'dateProp' };
|
||||
let adapter = new ShareDataTableAdapter(null, null, [col]);
|
||||
|
||||
adapter.setRows([
|
||||
new ShareDataRow(file2),
|
||||
new ShareDataRow(file1)
|
||||
]);
|
||||
|
||||
adapter.sort('dateProp', 'asc');
|
||||
|
||||
let rows = adapter.getRows();
|
||||
expect((<ShareDataRow> rows[0]).node).toBe(file1);
|
||||
expect((<ShareDataRow> rows[1]).node).toBe(file2);
|
||||
|
||||
adapter.sort('dateProp', 'desc');
|
||||
expect((<ShareDataRow> rows[0]).node).toBe(file2);
|
||||
expect((<ShareDataRow> rows[1]).node).toBe(file1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ShareDataRow', () => {
|
||||
|
||||
it('should wrap node', () => {
|
||||
let file = new FileNode();
|
||||
let row = new ShareDataRow(file);
|
||||
expect(row.node).toBe(file);
|
||||
});
|
||||
|
||||
it('should require object source', () => {
|
||||
expect(() => { return new ShareDataRow(null); }).toThrowError(ShareDataRow.ERR_OBJECT_NOT_FOUND);
|
||||
});
|
||||
|
||||
it('should resolve value from node entry', () => {
|
||||
let file = new FileNode('test');
|
||||
let row = new ShareDataRow(file);
|
||||
expect(row.getValue('name')).toBe('test');
|
||||
});
|
||||
|
||||
it('should check value', () => {
|
||||
let file = new FileNode('test');
|
||||
let row = new ShareDataRow(file);
|
||||
|
||||
expect(row.hasValue('name')).toBeTruthy();
|
||||
expect(row.hasValue('missing')).toBeFalsy();
|
||||
});
|
||||
|
||||
});
|
@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
import { DatePipe } from '@angular/common';
|
||||
import { ObjectUtils } from 'ng2-alfresco-core';
|
||||
import {
|
||||
DataTableAdapter,
|
||||
DataRow, DataColumn, DataSorting
|
||||
@ -26,6 +27,11 @@ import { DocumentListService } from './../services/document-list.service';
|
||||
|
||||
export class ShareDataTableAdapter implements DataTableAdapter {
|
||||
|
||||
static ERR_ROW_NOT_FOUND: string = 'Row not found';
|
||||
static ERR_COL_NOT_FOUND: string = 'Column not found';
|
||||
|
||||
static DEFAULT_DATE_FORMAT: string = 'medium';
|
||||
|
||||
private sorting: DataSorting;
|
||||
private rows: DataRow[];
|
||||
private columns: DataColumn[];
|
||||
@ -58,20 +64,20 @@ export class ShareDataTableAdapter implements DataTableAdapter {
|
||||
|
||||
getValue(row: DataRow, col: DataColumn): any {
|
||||
if (!row) {
|
||||
throw new Error('Row not found');
|
||||
throw new Error(ShareDataTableAdapter.ERR_ROW_NOT_FOUND);
|
||||
}
|
||||
if (!col) {
|
||||
throw new Error('Column not found');
|
||||
throw new Error(ShareDataTableAdapter.ERR_COL_NOT_FOUND);
|
||||
}
|
||||
let value = row.getValue(col.key);
|
||||
|
||||
if (col.type === 'date') {
|
||||
let datePipe = new DatePipe();
|
||||
let format = col.format || 'medium';
|
||||
let format = col.format || ShareDataTableAdapter.DEFAULT_DATE_FORMAT;
|
||||
try {
|
||||
return datePipe.transform(value, format);
|
||||
} catch (err) {
|
||||
console.error(`DocumentList: error parsing date ${value} to format ${format}`);
|
||||
console.error(`Error parsing date ${value} to format ${format}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,7 +99,7 @@ export class ShareDataTableAdapter implements DataTableAdapter {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (node.entry.content && node.entry.content.mimeType) {
|
||||
if (node.entry.content) {
|
||||
let mimeType = node.entry.content.mimeType;
|
||||
if (mimeType) {
|
||||
let icon = this.documentListService.getMimeTypeIcon(mimeType);
|
||||
@ -119,7 +125,7 @@ export class ShareDataTableAdapter implements DataTableAdapter {
|
||||
setSorting(sorting: DataSorting): void {
|
||||
this.sorting = sorting;
|
||||
|
||||
if (sorting && sorting.key) {
|
||||
if (sorting && sorting.key && this.rows && this.rows.length > 0) {
|
||||
this.rows.sort((a: ShareDataRow, b: ShareDataRow) => {
|
||||
if (a.node.entry.isFolder !== b.node.entry.isFolder) {
|
||||
return a.node.entry.isFolder ? -1 : 1;
|
||||
@ -167,7 +173,9 @@ export class ShareDataTableAdapter implements DataTableAdapter {
|
||||
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
|
||||
if (this.columns && this.columns.length > 0) {
|
||||
let sortable = this.columns.filter(c => c.sortable);
|
||||
if (sortable.length > 0) {
|
||||
this.sort(sortable[0].key, 'asc');
|
||||
@ -176,16 +184,20 @@ export class ShareDataTableAdapter implements DataTableAdapter {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.rows = rows;
|
||||
},
|
||||
error => console.log(error));
|
||||
error => console.error(error));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class ShareDataRow implements DataRow {
|
||||
|
||||
static ERR_OBJECT_NOT_FOUND: string = 'Object source not found';
|
||||
|
||||
isSelected: boolean = false;
|
||||
|
||||
get node(): MinimalNodeEntity {
|
||||
@ -194,44 +206,12 @@ export class ShareDataRow implements DataRow {
|
||||
|
||||
constructor(private obj: MinimalNodeEntity) {
|
||||
if (!obj) {
|
||||
throw new Error('Object source not found');
|
||||
throw new Error(ShareDataRow.ERR_OBJECT_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value from an object by composed key
|
||||
* documentList.getObjectValue({ item: { nodeType: 'cm:folder' }}, 'item.nodeType') ==> 'cm:folder'
|
||||
* @param target
|
||||
* @param key
|
||||
* @returns {string}
|
||||
*/
|
||||
getObjectValue(target: any, key: string): any {
|
||||
|
||||
if (!target) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let keys = key.split('.');
|
||||
key = '';
|
||||
|
||||
do {
|
||||
key += keys.shift();
|
||||
let value = target[key];
|
||||
if (value !== undefined && (typeof value === 'object' || !keys.length)) {
|
||||
target = value;
|
||||
key = '';
|
||||
} else if (!keys.length) {
|
||||
target = undefined;
|
||||
} else {
|
||||
key += '.';
|
||||
}
|
||||
} while (keys.length);
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
getValue(key: string): any {
|
||||
return this.getObjectValue(this.obj.entry, key);
|
||||
return ObjectUtils.getValue(this.obj.entry, key);
|
||||
}
|
||||
|
||||
hasValue(key: string): boolean {
|
||||
|
Loading…
x
Reference in New Issue
Block a user