import './index.js';
import template from './index.html';
describe('Component vnTh', () => {
let controller;
let $element;
beforeEach(ngModule('vnCore'));
beforeEach(inject($componentController => {
$element = angular.element(`
${template}
`);
controller = $componentController('vnTh', {$element: $element});
controller.table = {
setOrder: () => {},
applyOrder: () => {}
};
controller.column.setAttribute('field', 'MyField');
}));
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`, () => {
jest.spyOn(controller.table, 'setOrder');
controller.order = 'Change me!';
expect(controller.table.setOrder).toHaveBeenCalledWith('MyField', 'Change me!');
});
});
describe('onToggleOrder()', () => {
it(`should not call updateArrow() method if field property isn't defined`, () => {
controller.column.setAttribute('field', '');
jest.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';
jest.spyOn(controller, 'toggleOrder');
jest.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';
jest.spyOn(controller.table, 'setOrder');
jest.spyOn(controller, 'updateArrow');
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');
});
});
});