salix/front/core/components/th/index.spec.js

109 lines
3.8 KiB
JavaScript
Raw Normal View History

import './index.js';
import template from './index.html';
2018-06-28 13:47:14 +00:00
describe('Component vnTh', () => {
let controller;
let $element;
2019-09-13 14:09:14 +00:00
beforeEach(angular.mock.module('vnCore', $translateProvider => {
$translateProvider.translations('en', {});
}));
beforeEach(angular.mock.inject($componentController => {
$element = angular.element(`<div>${template}</div>`);
controller = $componentController('vnTh', {$element: $element});
2018-09-03 07:54:31 +00:00
controller.table = {
setOrder: () => {},
applyOrder: () => {}
};
2018-09-03 07:54:31 +00:00
controller.column.setAttribute('field', 'MyField');
}));
2018-09-03 07:54:31 +00:00
describe('onInit()', () => {
it(`should define controllers order as per defaultOrder then call setOrder()`, () => {
controller.defaultOrder = 'DESC';
spyOn(controller.table, 'setOrder');
controller.$onInit();
expect(controller.order).toEqual('DESC');
expect(controller.table.setOrder).toHaveBeenCalledWith('MyField', 'DESC');
});
});
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`, () => {
spyOn(controller.table, 'setOrder');
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', '');
spyOn(controller, 'updateArrow');
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';
spyOn(controller, 'toggleOrder');
spyOn(controller, 'updateArrow');
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';
spyOn(controller.table, 'setOrder');
2018-09-03 07:54:31 +00:00
spyOn(controller, 'updateArrow');
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');
});
});
});