added treeview unit tests
gitea/salix/1757-drag_and_drop_department This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-10-17 11:08:40 +02:00
parent 6d159c50a0
commit 79f9e616d2
4 changed files with 264 additions and 5 deletions

View File

@ -7,10 +7,6 @@ class Controller extends Component {
event.preventDefault();
this.treeview.onToggle(item);
}
onDrop(item, dragged, dropped) {
this.treeview.onDrop(item, dragged, dropped);
}
}
ngModule.component('vnTreeviewChilds', {

View File

@ -103,6 +103,7 @@ export default class Treeview extends Component {
const $dropped = this.dropping.$ctrl.item;
const $dragged = this.dragging.$ctrl.item;
if ($dropped != $dragged.parent)
this.emit('drop', {$dropped, $dragged});
}

View File

@ -0,0 +1,260 @@
describe('Component vnTreeview', () => {
let controller;
let $element;
beforeEach(angular.mock.module('vnCore', $translateProvider => {
$translateProvider.translations('en', {});
}));
beforeEach(inject(($compile, $rootScope) => {
$element = $compile(`<vn-treeview></vn-treeview>`)($rootScope);
controller = $element.controller('vnTreeview');
const promise = new Promise(() => {
return {name: 'My item'};
});
controller.fetchFunc = () => {
return promise;
};
controller.createFunc = () => {
return promise;
};
controller.removeFunc = () => {
return promise;
};
controller.sortFunc = () => {
return promise;
};
}));
afterEach(() => {
$element.remove();
});
// See how to test DOM element in Jest
xdescribe('undrop()', () => {
it(`should reset all drop events and properties`, () => {
controller.dropping = angular.element(`<vn-treeview-child class="dropping"></vn-treeview-child>`);
spyOn(controller.dropping.classList, 'remove');
controller.undrop();
expect(controller.dropping).toBeNull();
});
});
describe('dragOver()', () => {
it(`should set the dragClientY property`, () => {
const event = new Event('dragover');
event.clientY = 100;
controller.dragOver(event);
expect(controller.dragClientY).toEqual(100);
});
});
describe('data() setter', () => {
it(`should set the items property nested into a root element`, () => {
const items = [{name: 'Item1'}, {name: 'Item2'}];
controller.data = items;
const rootItem = controller.items[0];
expect(rootItem.childs).toEqual(items);
});
});
describe('fetch()', () => {
it(`should call the fetchFunc() method`, () => {
spyOn(controller, 'fetchFunc').and.returnValue(
new Promise(resolve => resolve())
);
controller.fetch().then(() => {
expect(controller.data).toBeDefined();
});
expect(controller.fetchFunc).toHaveBeenCalledWith();
});
});
describe('setParent()', () => {
it(`should set the parent property recursively to each element of an item list`, () => {
spyOn(controller, 'setParent').and.callThrough();
const items = [{name: 'Item1'}, {name: 'Item2', childs: [
{name: 'Item3'}
]}];
const rootItem = {name: 'Nested tree', sons: items};
controller.setParent(rootItem, items);
expect(items[0].parent).toEqual(rootItem);
expect(items[1].parent).toEqual(rootItem);
expect(controller.setParent).toHaveBeenCalledWith(rootItem, items[1].childs);
});
});
describe('onToggle()', () => {
it(`should call the fold() or unfold() methods`, () => {
spyOn(controller, 'fold');
spyOn(controller, 'unfold');
const item = {name: 'My item'};
controller.onToggle(item);
item.active = true;
controller.onToggle(item);
expect(controller.unfold).toHaveBeenCalledWith(item);
expect(controller.fold).toHaveBeenCalledWith(item);
});
});
describe('fold()', () => {
it(`should remove the childs and set the active property to false`, () => {
const item = {name: 'My item', childs: [{name: 'Item 1'}], active: true};
controller.fold(item);
expect(item.childs).toBeUndefined();
expect(item.active).toBeFalsy();
});
});
describe('unfold()', () => {
it(`should unfold a parent item`, () => {
const expectedResponse = [{name: 'Item 1'}, {name: 'Item 2'}];
spyOn(controller, 'fetchFunc').and.returnValue(
new Promise(resolve => resolve(expectedResponse))
);
spyOn(controller, 'setParent');
spyOn(controller, 'sortFunc');
const parent = {name: 'My item', sons: 1};
const child = {name: 'Item 1'};
child.parent = parent;
parent.childs = [child];
controller.unfold(parent).then(() => {
expect(controller.fetchFunc).toHaveBeenCalledWith({$item: parent});
expect(controller.setParent).toHaveBeenCalledWith(parent, expectedResponse);
expect(controller.sortFunc).toHaveBeenCalledWith(jasmine.any(Object));
expect(parent.active).toBeTruthy();
});
});
});
describe('onRemove()', () => {
it(`should call the removeFunc() method`, () => {
spyOn(controller, 'removeFunc');
const item = {name: 'My item'};
controller.onRemove(item);
expect(controller.removeFunc).toHaveBeenCalledWith({$item: item});
});
});
describe('remove()', () => {
it(`should remove a child element`, () => {
const parent = {name: 'My item', sons: 1};
const child = {name: 'Item 1'};
child.parent = parent;
parent.childs = [child];
controller.remove(child);
expect(parent.childs).toEqual([]);
expect(parent.sons).toEqual(0);
});
});
describe('onCreate()', () => {
it(`should call the createFunc() method`, () => {
spyOn(controller, 'createFunc');
const parent = {name: 'My item'};
controller.onCreate(parent);
expect(controller.createFunc).toHaveBeenCalledWith({$parent: parent});
});
});
describe('create()', () => {
it(`should unfold an inactive parent and then create a child`, () => {
spyOn(controller, 'unfold');
spyOn(controller, 'sortFunc');
const parent = {name: 'My item', sons: 2, childs: [
{name: 'Item 1'},
{name: 'Item 2'}
]};
const child = {name: 'Item 3'};
child.parent = parent;
parent.childs.push(child);
controller.create(child);
expect(parent.sons).toEqual(3);
expect(controller.unfold).toHaveBeenCalledWith(parent);
expect(controller.sortFunc).toHaveBeenCalledWith(jasmine.any(Object));
expect(controller.sortFunc).toHaveBeenCalledTimes(2);
});
it(`should create a child on an active parent`, () => {
spyOn(controller, 'unfold');
spyOn(controller, 'sortFunc');
const parent = {name: 'My item', sons: 2, childs: [
{name: 'Item 1'},
{name: 'Item 2'}
], active: true};
const child = {name: 'Item 3'};
child.parent = parent;
controller.create(child);
expect(parent.sons).toEqual(3);
expect(controller.unfold).not.toHaveBeenCalledWith(parent);
expect(controller.sortFunc).toHaveBeenCalledWith(jasmine.any(Object));
expect(controller.sortFunc).toHaveBeenCalledTimes(2);
});
});
describe('move()', () => {
it(`should move an item to anocher parent and then unfold the parent`, () => {
spyOn(controller, 'unfold').and.returnValue(
new Promise(resolve => resolve())
);
const newParent = {name: 'My item 2', sons: 0};
const parent = {name: 'My item', sons: 3, childs: [
{name: 'Item 1'},
{name: 'Item 2'}
]};
const child = {name: 'Item 3'};
child.parent = parent;
parent.childs.push(child);
controller.move(child, newParent);
expect(parent.sons).toEqual(2);
expect(controller.unfold).toHaveBeenCalledWith(newParent);
});
it(`should move an item to anocher parent`, () => {
spyOn(controller, 'unfold');
spyOn(controller, 'create');
const newParent = {name: 'My item 2', sons: 0, active: true};
const parent = {name: 'My item', sons: 3, childs: [
{name: 'Item 1'},
{name: 'Item 2'}
]};
const child = {name: 'Item 3'};
child.parent = parent;
parent.childs.push(child);
controller.move(child, newParent);
expect(parent.sons).toEqual(2);
expect(controller.unfold).not.toHaveBeenCalledWith(newParent);
});
});
});

View File

@ -50,6 +50,7 @@
</vn-check>
</vn-one>
</vn-horizontal>
<vn-data-viewer model="model">
<vn-table model="model">
<vn-thead>
<vn-tr>
@ -114,6 +115,7 @@
</vn-tr>
</vn-tbody>
</vn-table>
</vn-data-viewer>
</vn-vertical>
</vn-card>
<vn-button-bar>