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

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