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

258 lines
8.4 KiB
JavaScript
Raw Permalink Normal View History

2019-10-17 09:08:40 +00:00
describe('Component vnTreeview', () => {
let controller;
let $element;
beforeEach(ngModule('vnCore'));
2019-10-17 09:08:40 +00:00
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();
});
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`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'fetchFunc');
2019-10-17 09:08:40 +00:00
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`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'setParent');
2019-10-17 09:08:40 +00:00
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`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'fold');
jest.spyOn(controller, 'unfold');
2019-10-17 09:08:40 +00:00
2019-10-24 08:17:32 +00:00
let event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
const item = {name: 'My item', sons: 1};
2019-10-17 09:08:40 +00:00
2019-10-24 08:17:32 +00:00
controller.onToggle(event, item);
2019-10-17 09:08:40 +00:00
item.active = true;
2019-10-24 08:17:32 +00:00
controller.onToggle(event, item);
2019-10-17 09:08:40 +00:00
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'}];
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'fetchFunc');
jest.spyOn(controller, 'setParent');
jest.spyOn(controller, 'sortFunc');
2019-10-17 09:08:40 +00:00
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`, () => {
2019-10-24 08:17:32 +00:00
let event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'removeFunc');
2019-10-17 09:08:40 +00:00
const item = {name: 'My item'};
2019-10-24 08:17:32 +00:00
controller.onRemove(event, item);
2019-10-17 09:08:40 +00:00
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`, () => {
2019-10-24 08:17:32 +00:00
let event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'createFunc');
2019-10-17 09:08:40 +00:00
const parent = {name: 'My item'};
2019-10-24 08:17:32 +00:00
controller.onCreate(event, parent);
2019-10-17 09:08:40 +00:00
expect(controller.createFunc).toHaveBeenCalledWith({$parent: parent});
});
});
describe('create()', () => {
it(`should unfold an inactive parent and then create a child`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'unfold');
jest.spyOn(controller, 'sortFunc');
2019-10-17 09:08:40 +00:00
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`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'unfold');
jest.spyOn(controller, 'sortFunc');
2019-10-17 09:08:40 +00:00
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`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'unfold');
2019-10-17 09:08:40 +00:00
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`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'unfold');
jest.spyOn(controller, 'create');
2019-10-17 09:08:40 +00:00
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);
});
});
});