Skip to content

Commit

Permalink
improve object datatable adapter unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgny committed May 7, 2024
1 parent 7c96c18 commit cbb3767
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
* limitations under the License.
*/

import { DataColumn } from '../data-column.model';

export const mockPersonData = [
{
person: {
Expand Down Expand Up @@ -62,26 +60,4 @@ export const mockPersonData = [
}
];

export const mockPersonSchema: DataColumn[] = [
{
type: 'json',
key: 'person.personData.[address.[data]test]',
title: 'Address',
sortable: true,
draggable: true
},
{
type: 'text',
key: 'person.name',
title: 'Name',
sortable: true,
draggable: true
},
{
type: 'json',
key: 'person.phoneNumbers',
title: 'Phone numbers',
sortable: true,
draggable: true
}
];
export const mockPersonDataFirstRow = mockPersonData[0].person;
158 changes: 123 additions & 35 deletions lib/core/src/lib/datatable/data/object-datatable-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { DataSorting } from './data-sorting.model';
import { ObjectDataTableAdapter } from './object-datatable-adapter';
import { ObjectDataRow } from './object-datarow.model';
import { ObjectDataColumn } from './object-datacolumn.model';
import { mockPersonData, mockPersonSchema } from './mocks/object-datatable-adapter.mock';
import { mockPersonData, mockPersonDataFirstRow } from './mocks/object-datatable-adapter.mock';

describe('ObjectDataTableAdapter', () => {
it('should init with empty row collection', () => {
Expand Down Expand Up @@ -291,42 +291,130 @@ describe('ObjectDataTableAdapter', () => {
);
});

it('should create proper rows and columns from schema and data with nested properties', () => {
const adapter = new ObjectDataTableAdapter(mockPersonData, mockPersonSchema);
const rows = adapter.getRows();
const columns = adapter.getColumns();
describe('should create proper rows and columns from schema and data with nested properties for', () => {
it('one column', () => {
const mockPersonSchema: DataColumn[] = [
{
type: 'text',
key: 'person.name',
title: 'Name'
}
];

const adapter = new ObjectDataTableAdapter(mockPersonData, mockPersonSchema);
const rows = adapter.getRows();
const columns = adapter.getColumns();

const expectedFirstRow = new ObjectDataRow({
name: 'John Doe'
});
const expectedSecondRow = new ObjectDataRow({
name: 'Sam Smith'
});
const expectedColumns = [new ObjectDataColumn({ key: 'name', type: 'text', title: 'Name' })];

expect(rows.length).toBe(2);
expect(rows[0]).toEqual(expectedFirstRow);
expect(rows[1]).toEqual(expectedSecondRow);

expect(columns.length).toBe(1);
expect(columns).toEqual(expectedColumns);
});

expect(rows.length).toBe(2);
expect(rows[0].getValue('address.[data]test')).toEqual({ street: '1234 Main St', city: 'Springfield' });
expect(rows[0].getValue('name')).toBe('John Doe');
expect(rows[0].getValue('phoneNumbers')).toEqual([
{
type: 'home',
phoneNumber: '123-456-7890'
},
{
type: 'work',
phoneNumber: '098-765-4321'
}
]);

expect(rows[1].getValue('address.[data]test')).toEqual({ street: '731 Second St', city: 'Westlake' });
expect(rows[1].getValue('name')).toBe('Sam Smith');
expect(rows[1].getValue('phoneNumbers')).toEqual([
{
type: 'home',
phoneNumber: '123-456-7891'
},
{
type: 'work',
phoneNumber: '321-654-1987'
}
]);
it('one row', () => {
const mockPersonSchema: DataColumn[] = [
{
type: 'text',
key: 'name',
title: 'Name'
},
{
type: 'text',
key: 'personData.[address.[data]test].city',
title: 'City'
}
];

const adapter = new ObjectDataTableAdapter([mockPersonDataFirstRow], mockPersonSchema);
const rows = adapter.getRows();
const columns = adapter.getColumns();

const expectedFirstRow = new ObjectDataRow({
name: 'John Doe',
city: 'Springfield'
});
const expectedColumns = [
new ObjectDataColumn({ key: 'name', type: 'text', title: 'Name' }),
new ObjectDataColumn({ key: 'city', type: 'text', title: 'City' })
];

expect(rows.length).toBe(1);
expect(rows[0]).toEqual(expectedFirstRow);

expect(columns.length).toBe(2);
expect(columns).toEqual(expectedColumns);
});

expect(columns.length).toBe(3);
expect(columns[0].key).toBe('address.[data]test');
expect(columns[1].key).toBe('name');
expect(columns[2].key).toBe('phoneNumbers');
it('complex schema', () => {
const mockPersonSchema: DataColumn[] = [
{
type: 'text',
key: 'person.name',
title: 'Name'
},
{
type: 'text',
key: 'person.personData.[address.[data]test].city',
title: 'City'
},
{
type: 'text',
key: 'person.personData.[address.[data]test].street',
title: 'Street'
},
{
type: 'json',
key: 'person.phoneNumbers',
title: 'Phone numbers'
}
];

const adapter = new ObjectDataTableAdapter(mockPersonData, mockPersonSchema);
const rows = adapter.getRows();
const columns = adapter.getColumns();

const expectedFirstRow = new ObjectDataRow({
city: 'Springfield',
street: '1234 Main St',
name: 'John Doe',
phoneNumbers: [
{ type: 'home', phoneNumber: '123-456-7890' },
{ type: 'work', phoneNumber: '098-765-4321' }
]
});
const expectedSecondRow = new ObjectDataRow({
city: 'Westlake',
street: '731 Second St',
name: 'Sam Smith',
phoneNumbers: [
{ type: 'home', phoneNumber: '123-456-7891' },
{ type: 'work', phoneNumber: '321-654-1987' }
]
});
const expectedColumns = [
new ObjectDataColumn({ key: 'name', type: 'text', title: 'Name' }),
new ObjectDataColumn({ key: 'city', type: 'text', title: 'City' }),
new ObjectDataColumn({ key: 'street', type: 'text', title: 'Street' }),
new ObjectDataColumn({ key: 'phoneNumbers', type: 'json', title: 'Phone numbers' })
];

expect(rows.length).toBe(2);
expect(rows[0]).toEqual(expectedFirstRow);
expect(rows[1]).toEqual(expectedSecondRow);

expect(columns.length).toBe(4);
expect(columns).toEqual(expectedColumns);
});
});
});

Expand Down

0 comments on commit cbb3767

Please sign in to comment.