New 'core:/utils/ObjectUtils', additional unit tests

This commit is contained in:
Denys Vuika 2016-07-07 14:30:15 +01:00
parent 3881faec42
commit acfcd19af0
7 changed files with 203 additions and 71 deletions

View File

@ -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,

View File

@ -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();
});
});

View 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;
}
}

View File

@ -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 });

View File

@ -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 {

View File

@ -0,0 +1,88 @@
/*!
* @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 { DataColumn, DataRow } from 'ng2-alfresco-datatable';
import { ShareDataTableAdapter } from './share-datatable-adapter';
describe('ShareDataTableAdapter', () => {
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();
});
});

View File

@ -16,6 +16,7 @@
*/
import { DatePipe } from '@angular/common';
import { ObjectUtils } from 'ng2-alfresco-core';
import {
DataTableAdapter,
DataRow, DataColumn, DataSorting
@ -198,40 +199,8 @@ export class ShareDataRow 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.entry, key);
return ObjectUtils.getValue(this.obj.entry, key);
}
hasValue(key: string): boolean {