2020-11-20 14:05:57 +00:00
|
|
|
import './index.js';
|
|
|
|
|
|
|
|
describe('Travel Component vnTravelExtraCommunity', () => {
|
|
|
|
let controller;
|
|
|
|
let $httpBackend;
|
|
|
|
|
|
|
|
beforeEach(ngModule('travel'));
|
|
|
|
|
|
|
|
beforeEach(inject(($componentController, _$httpBackend_) => {
|
|
|
|
$httpBackend = _$httpBackend_;
|
2020-11-23 07:12:33 +00:00
|
|
|
const $element = angular.element('<vn-travel-extra-community><div class="travel-list"></div></vn-travel-extra-community>');
|
2020-11-20 14:05:57 +00:00
|
|
|
controller = $componentController('vnTravelExtraCommunity', {$element});
|
2020-11-23 07:12:33 +00:00
|
|
|
controller.$.model = {};
|
|
|
|
controller.$.model.refresh = jest.fn();
|
2020-11-20 14:05:57 +00:00
|
|
|
}));
|
|
|
|
|
2020-11-23 07:12:33 +00:00
|
|
|
describe('findDraggable()', () => {
|
|
|
|
it('should find the draggable element', () => {
|
2022-09-13 13:11:14 +00:00
|
|
|
const draggable = document.createElement('tr');
|
2020-11-23 07:12:33 +00:00
|
|
|
draggable.setAttribute('draggable', true);
|
|
|
|
|
|
|
|
const $event = new Event('dragstart');
|
|
|
|
const target = document.createElement('div');
|
|
|
|
draggable.appendChild(target);
|
|
|
|
target.dispatchEvent($event);
|
|
|
|
|
|
|
|
const result = controller.findDraggable($event);
|
|
|
|
|
|
|
|
expect(result).toEqual(draggable);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('findDroppable()', () => {
|
|
|
|
it('should find the droppable element', () => {
|
2022-09-13 13:11:14 +00:00
|
|
|
const droppable = document.createElement('tbody');
|
2020-11-23 07:12:33 +00:00
|
|
|
droppable.setAttribute('vn-droppable', true);
|
|
|
|
|
|
|
|
const $event = new Event('drop');
|
|
|
|
const target = document.createElement('div');
|
|
|
|
droppable.appendChild(target);
|
|
|
|
target.dispatchEvent($event);
|
|
|
|
|
|
|
|
const result = controller.findDroppable($event);
|
|
|
|
|
|
|
|
expect(result).toEqual(droppable);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('dragStart()', () => {
|
2022-09-13 13:11:14 +00:00
|
|
|
it(`should add the class "dragging" to the draggable element
|
2020-11-23 07:12:33 +00:00
|
|
|
and then set the entryId controller property`, () => {
|
2022-09-13 13:11:14 +00:00
|
|
|
const draggable = document.createElement('tr');
|
2020-11-23 07:12:33 +00:00
|
|
|
draggable.setAttribute('draggable', true);
|
|
|
|
draggable.setAttribute('id', 3);
|
|
|
|
|
|
|
|
jest.spyOn(controller, 'findDraggable').mockReturnValue(draggable);
|
|
|
|
|
|
|
|
const $event = new Event('dragStart');
|
|
|
|
controller.dragStart($event);
|
|
|
|
|
|
|
|
const firstClass = draggable.classList[0];
|
2020-11-20 14:05:57 +00:00
|
|
|
|
2020-11-23 07:12:33 +00:00
|
|
|
expect(firstClass).toEqual('dragging');
|
|
|
|
expect(controller.entryId).toEqual(3);
|
|
|
|
expect(controller.entry).toEqual(draggable);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-23 17:42:11 +00:00
|
|
|
describe('dragEnd()', () => {
|
2022-09-13 13:11:14 +00:00
|
|
|
it(`should remove the class "dragging" from the draggable element
|
2020-11-23 07:12:33 +00:00
|
|
|
and then set the entryId controller property to null`, () => {
|
2022-09-13 13:11:14 +00:00
|
|
|
const draggable = document.createElement('tr');
|
2020-11-23 07:12:33 +00:00
|
|
|
draggable.setAttribute('draggable', true);
|
|
|
|
draggable.setAttribute('id', 3);
|
|
|
|
draggable.classList.add('dragging');
|
|
|
|
|
|
|
|
jest.spyOn(controller, 'findDraggable').mockReturnValue(draggable);
|
|
|
|
|
|
|
|
const $event = new Event('dragStart');
|
|
|
|
controller.dragEnd($event);
|
|
|
|
|
|
|
|
const classList = draggable.classList;
|
|
|
|
|
|
|
|
expect(classList.length).toEqual(0);
|
|
|
|
expect(controller.entryId).toBeNull();
|
|
|
|
expect(controller.entry).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('onDrop()', () => {
|
|
|
|
it('should make an HTTP patch query', () => {
|
2022-09-13 13:11:14 +00:00
|
|
|
const droppable = document.createElement('tbody');
|
2020-11-23 07:12:33 +00:00
|
|
|
droppable.setAttribute('vn-droppable', true);
|
|
|
|
droppable.setAttribute('id', 1);
|
|
|
|
|
|
|
|
jest.spyOn(controller, 'findDroppable').mockReturnValue(droppable);
|
|
|
|
|
2022-09-13 13:11:14 +00:00
|
|
|
const oldDroppable = document.createElement('tbody');
|
2020-11-23 07:12:33 +00:00
|
|
|
oldDroppable.setAttribute('vn-droppable', true);
|
|
|
|
const entry = document.createElement('div');
|
|
|
|
oldDroppable.appendChild(entry);
|
|
|
|
|
|
|
|
controller.entryId = 3;
|
|
|
|
controller.entry = entry;
|
|
|
|
|
|
|
|
const $event = new Event('drop');
|
|
|
|
const expectedData = {travelFk: 1};
|
|
|
|
$httpBackend.expect('PATCH', `Entries/3`, expectedData).respond(200);
|
|
|
|
controller.onDrop($event);
|
|
|
|
$httpBackend.flush();
|
2020-11-20 14:05:57 +00:00
|
|
|
});
|
|
|
|
});
|
2021-01-18 13:22:27 +00:00
|
|
|
|
|
|
|
describe('save()', () => {
|
|
|
|
it('should make an HTTP query', () => {
|
|
|
|
jest.spyOn(controller.vnApp, 'showSuccess');
|
|
|
|
|
|
|
|
const travelId = 1;
|
|
|
|
const data = {ref: 'New reference'};
|
|
|
|
const expectedData = {ref: 'New reference'};
|
|
|
|
$httpBackend.expect('PATCH', `Travels/${travelId}`, expectedData).respond(200);
|
|
|
|
controller.save(travelId, data);
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
|
|
|
|
});
|
|
|
|
});
|
2020-11-20 14:05:57 +00:00
|
|
|
});
|