diff --git a/client/core/src/components/th/index.spec.js b/client/core/src/components/th/index.spec.js index fd863b46e..780ffe8d7 100644 --- a/client/core/src/components/th/index.spec.js +++ b/client/core/src/components/th/index.spec.js @@ -14,16 +14,24 @@ describe('Component vnTh', () => { $componentController = _$componentController_; $element = angular.element(`
${template}
`); controller = $componentController('vnTh', {$element: $element}); - controller.table = {setOrder: () => {}}; - controller.column = { - getAttribute: () => 'MyField', - classList: { - add: () => {}, - remove: () => {} - } + controller.table = { + setOrder: () => {}, + applyOrder: () => {} }; + controller.column.setAttribute('field', 'MyField'); })); + 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'; @@ -47,14 +55,56 @@ describe('Component vnTh', () => { }); }); - describe('onInit()', () => { - it(`should define controllers order as per defaultOrder then call setOrder()`, () => { - controller.defaultOrder = 'DESC'; - spyOn(controller.table, 'setOrder'); - controller.$onInit(); + describe('onToggleOrder()', () => { + it(`should not call updateArrow() method if field property isn't defined`, () => { + controller.column.setAttribute('field', ''); + spyOn(controller, 'updateArrow'); - expect(controller.order).toEqual('DESC'); - expect(controller.table.setOrder).toHaveBeenCalledWith('MyField', 'DESC'); + 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'); + 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'); }); }); });