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

107 lines
3.7 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;
beforeEach(ngModule('vnCore'));
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';
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');
});
});
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');
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-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');
});
});
});