mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
#113 unit tests (wip)
This commit is contained in:
@@ -23,6 +23,14 @@ import {
|
|||||||
} from 'angular2/testing';
|
} from 'angular2/testing';
|
||||||
|
|
||||||
import { DataTableComponent } from './datatable.component';
|
import { DataTableComponent } from './datatable.component';
|
||||||
|
import {
|
||||||
|
DataRow,
|
||||||
|
DataSorting
|
||||||
|
} from './../data/datatable-adapter';
|
||||||
|
import {
|
||||||
|
ObjectDataTableAdapter,
|
||||||
|
ObjectDataColumn
|
||||||
|
} from './../data/object-datatable-adapter';
|
||||||
|
|
||||||
describe('DataTable', () => {
|
describe('DataTable', () => {
|
||||||
|
|
||||||
@@ -33,14 +41,101 @@ describe('DataTable', () => {
|
|||||||
dataTable = new DataTableComponent();
|
dataTable = new DataTableComponent();
|
||||||
|
|
||||||
eventMock = {
|
eventMock = {
|
||||||
preventDefault: function () {
|
preventDefault: function () {}
|
||||||
console.log('mock preventDefault');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pass', () => {
|
it('should initialize default adapter', () => {
|
||||||
expect(true).toBe(true);
|
expect(dataTable.data).toBeUndefined();
|
||||||
|
dataTable.ngOnInit();
|
||||||
|
expect(dataTable.data).toEqual(jasmine.any(ObjectDataTableAdapter));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should initialize with custom data', () => {
|
||||||
|
let data = new ObjectDataTableAdapter([], []);
|
||||||
|
dataTable.data = data;
|
||||||
|
dataTable.ngOnInit();
|
||||||
|
expect(dataTable.data).toBe(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should emit row click event', (done) => {
|
||||||
|
let row = <DataRow> {};
|
||||||
|
|
||||||
|
dataTable.rowClick.subscribe(e => {
|
||||||
|
expect(e.value).toBe(row);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
dataTable.onRowClick(row, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should prevent default event on row click event', () => {
|
||||||
|
let e = jasmine.createSpyObj('event', ['preventDefault']);
|
||||||
|
dataTable.ngOnInit();
|
||||||
|
dataTable.onRowClick(null, e);
|
||||||
|
expect(e.preventDefault).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not sort if column is missing', () => {
|
||||||
|
dataTable.ngOnInit();
|
||||||
|
let adapter = dataTable.data;
|
||||||
|
spyOn(adapter, 'setSorting').and.callThrough();
|
||||||
|
dataTable.onColumnHeaderClick(null);
|
||||||
|
expect(adapter.setSorting).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not sort upon clicking non-sortable column header', () => {
|
||||||
|
dataTable.ngOnInit();
|
||||||
|
let adapter = dataTable.data;
|
||||||
|
spyOn(adapter, 'setSorting').and.callThrough();
|
||||||
|
|
||||||
|
let column = new ObjectDataColumn({
|
||||||
|
key: 'column_1'
|
||||||
|
});
|
||||||
|
|
||||||
|
dataTable.onColumnHeaderClick(column);
|
||||||
|
expect(adapter.setSorting).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set sorting upon column header clicked', () => {
|
||||||
|
dataTable.ngOnInit();
|
||||||
|
let adapter = dataTable.data;
|
||||||
|
spyOn(adapter, 'setSorting').and.callThrough();
|
||||||
|
|
||||||
|
let column = new ObjectDataColumn({
|
||||||
|
key: 'column_1',
|
||||||
|
sortable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
dataTable.onColumnHeaderClick(column);
|
||||||
|
expect(adapter.setSorting).toHaveBeenCalledWith(
|
||||||
|
jasmine.objectContaining({
|
||||||
|
key: 'column_1',
|
||||||
|
direction: 'asc'
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should invert sorting upon column header clicked', () => {
|
||||||
|
dataTable.ngOnInit();
|
||||||
|
|
||||||
|
let adapter = dataTable.data;
|
||||||
|
spyOn(adapter, 'setSorting').and.callThrough();
|
||||||
|
spyOn(adapter, 'getSorting').and.returnValue(new DataSorting('column_1', 'asc'));
|
||||||
|
|
||||||
|
let column = new ObjectDataColumn({
|
||||||
|
key: 'column_1',
|
||||||
|
sortable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
dataTable.onColumnHeaderClick(column);
|
||||||
|
expect(adapter.setSorting).toHaveBeenCalledWith(
|
||||||
|
jasmine.objectContaining({
|
||||||
|
key: 'column_1',
|
||||||
|
direction: 'desc'
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@@ -65,9 +65,7 @@ export class DataTableComponent implements OnInit, AfterViewChecked {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
if (this.data) {
|
if (!this.data) {
|
||||||
console.log(this.data);
|
|
||||||
} else {
|
|
||||||
this.data = new ObjectDataTableAdapter([], []);
|
this.data = new ObjectDataTableAdapter([], []);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,7 +87,7 @@ export class DataTableComponent implements OnInit, AfterViewChecked {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onRowDblClick(row: DataRow, e?) {
|
onRowDblClick(row: DataRow, e?: Event) {
|
||||||
if (e) {
|
if (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
@@ -103,14 +101,14 @@ export class DataTableComponent implements OnInit, AfterViewChecked {
|
|||||||
if (column && column.sortable) {
|
if (column && column.sortable) {
|
||||||
let current = this.data.getSorting();
|
let current = this.data.getSorting();
|
||||||
let newDirection = 'asc';
|
let newDirection = 'asc';
|
||||||
if (column.key === current.key) {
|
if (current && column.key === current.key) {
|
||||||
newDirection = current.direction === 'asc' ? 'desc' : 'asc';
|
newDirection = current.direction === 'asc' ? 'desc' : 'asc';
|
||||||
}
|
}
|
||||||
this.data.setSorting(new DataSorting(column.key, newDirection));
|
this.data.setSorting(new DataSorting(column.key, newDirection));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onSelectAllClick(e?) {
|
onSelectAllClick(e?: Event) {
|
||||||
if (e) {
|
if (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
|
@@ -160,7 +160,7 @@ export class ObjectDataColumn implements DataColumn {
|
|||||||
|
|
||||||
constructor(obj: any) {
|
constructor(obj: any) {
|
||||||
this.key = obj.key;
|
this.key = obj.key;
|
||||||
this.type = obj.type;
|
this.type = obj.type || 'text';
|
||||||
this.sortable = obj.sortable;
|
this.sortable = obj.sortable;
|
||||||
this.title = obj.title;
|
this.title = obj.title;
|
||||||
this.srTitle = obj.srTitle;
|
this.srTitle = obj.srTitle;
|
||||||
|
Reference in New Issue
Block a user