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

278 lines
9.1 KiB
JavaScript

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([{name: 'My item'}]))
);
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');
let event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
const item = {name: 'My item', sons: 1};
controller.onToggle(event, item);
item.active = true;
controller.onToggle(event, 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`, () => {
let event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
spyOn(controller, 'removeFunc');
const item = {name: 'My item'};
controller.onRemove(event, 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`, () => {
let event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
spyOn(controller, 'createFunc');
const parent = {name: 'My item'};
controller.onCreate(event, 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);
});
});
});