2018-06-28 12:43:59 +00:00
|
|
|
import './index.js';
|
|
|
|
import template from './index.html';
|
|
|
|
|
2018-06-28 13:47:14 +00:00
|
|
|
describe('Component vnTh', () => {
|
2018-06-28 12:43:59 +00:00
|
|
|
let controller;
|
|
|
|
let $element;
|
|
|
|
|
2019-10-24 22:53:53 +00:00
|
|
|
beforeEach(ngModule('vnCore'));
|
2018-06-28 12:43:59 +00:00
|
|
|
|
2018-12-22 10:59:26 +00:00
|
|
|
beforeEach(angular.mock.inject($componentController => {
|
2018-06-28 12:43:59 +00:00
|
|
|
$element = angular.element(`<div>${template}</div>`);
|
|
|
|
controller = $componentController('vnTh', {$element: $element});
|
2018-09-03 07:54:31 +00:00
|
|
|
controller.table = {
|
|
|
|
setOrder: () => {},
|
|
|
|
applyOrder: () => {}
|
2018-06-28 12:43:59 +00:00
|
|
|
};
|
2018-09-03 07:54:31 +00:00
|
|
|
controller.column.setAttribute('field', 'MyField');
|
2018-06-28 12:43:59 +00:00
|
|
|
}));
|
|
|
|
|
2018-09-03 07:54:31 +00:00
|
|
|
describe('onInit()', () => {
|
|
|
|
it(`should define controllers order as per defaultOrder then call setOrder()`, () => {
|
|
|
|
controller.defaultOrder = 'DESC';
|
2020-02-26 12:22:52 +00:00
|
|
|
jest.spyOn(controller.table, 'setOrder');
|
2018-09-03 07:54:31 +00:00
|
|
|
controller.$onInit();
|
|
|
|
|
|
|
|
expect(controller.order).toEqual('DESC');
|
|
|
|
expect(controller.table.setOrder).toHaveBeenCalledWith('MyField', 'DESC');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-28 12:43:59 +00:00
|
|
|
describe('toggleOrder()', () => {
|
|
|
|
it(`should change the ordenation to DESC (descendant) if it was ASC (ascendant)`, () => {
|
|
|
|
controller.order = 'ASC';
|
|
|
|
controller.toggleOrder();
|
|
|
|
|
|
|
|
expect(controller.order).toEqual('DESC');
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should change the ordenation to ASC (ascendant) if it wasnt ASC`, () => {
|
|
|
|
controller.order = 'DESC or any other value that might occur';
|
|
|
|
controller.toggleOrder();
|
|
|
|
|
|
|
|
expect(controller.order).toEqual('ASC');
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should call the setOrder() function after changing a value`, () => {
|
2020-02-26 12:22:52 +00:00
|
|
|
jest.spyOn(controller.table, 'setOrder');
|
2018-06-28 12:43:59 +00:00
|
|
|
controller.order = 'Change me!';
|
|
|
|
|
|
|
|
expect(controller.table.setOrder).toHaveBeenCalledWith('MyField', 'Change me!');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-03 07:54:31 +00:00
|
|
|
describe('onToggleOrder()', () => {
|
|
|
|
it(`should not call updateArrow() method if field property isn't defined`, () => {
|
|
|
|
controller.column.setAttribute('field', '');
|
2020-02-26 12:22:52 +00:00
|
|
|
jest.spyOn(controller, 'updateArrow');
|
2018-09-03 07:54:31 +00:00
|
|
|
|
|
|
|
controller.onToggleOrder();
|
|
|
|
|
|
|
|
expect(controller.updateArrow).not.toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should call toggleOrder() method if field property and
|
|
|
|
table field property equals and then call updateArrow()`, () => {
|
|
|
|
controller.table.field = 'MyField';
|
2020-02-26 12:22:52 +00:00
|
|
|
jest.spyOn(controller, 'toggleOrder');
|
|
|
|
jest.spyOn(controller, 'updateArrow');
|
2018-09-03 07:54:31 +00:00
|
|
|
|
|
|
|
controller.onToggleOrder();
|
|
|
|
|
|
|
|
expect(controller.toggleOrder).toHaveBeenCalledWith();
|
|
|
|
expect(controller.updateArrow).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should call setOrder() method if field property and
|
|
|
|
table field property doesn't equals and then call updateArrow()`, () => {
|
|
|
|
controller.table.field = 'MyField2';
|
2020-02-26 12:22:52 +00:00
|
|
|
jest.spyOn(controller.table, 'setOrder');
|
|
|
|
jest.spyOn(controller, 'updateArrow');
|
2018-06-28 12:43:59 +00:00
|
|
|
|
2018-09-03 07:54:31 +00:00
|
|
|
controller.onToggleOrder();
|
|
|
|
|
|
|
|
expect(controller.table.setOrder).toHaveBeenCalledWith('MyField', 'ASC');
|
|
|
|
expect(controller.updateArrow).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('updateArrow()', () => {
|
|
|
|
it(`should remove 'asc' class and add 'desc' class if order property is descendant`, () => {
|
|
|
|
controller.column.classList.add('asc');
|
|
|
|
controller.order = 'DESC';
|
|
|
|
controller.updateArrow();
|
|
|
|
|
|
|
|
expect(controller.column.classList[0]).toEqual('desc');
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should remove 'asc' class and add it again if order property is ascendant`, () => {
|
|
|
|
controller.column.classList.add('asc');
|
|
|
|
controller.order = 'ASC';
|
|
|
|
controller.updateArrow();
|
|
|
|
|
|
|
|
expect(controller.column.classList[0]).toEqual('asc');
|
2018-06-28 12:43:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|