Client side unit test for drop-down
This commit is contained in:
parent
09e7e7f4ce
commit
311d80d902
|
@ -12,7 +12,7 @@ describe('Component vnDropDown', () => {
|
|||
|
||||
beforeEach(angular.mock.inject((_$componentController_, _$timeout_, _$filter_) => {
|
||||
$componentController = _$componentController_;
|
||||
$element = angular.element('<div></div>');
|
||||
$element = angular.element('<div><ul><li></li></ul></div>');
|
||||
$timeout = _$timeout_;
|
||||
$filter = _$filter_;
|
||||
}));
|
||||
|
@ -268,7 +268,7 @@ describe('Component vnDropDown', () => {
|
|||
expect(controller._activeOption).toEqual(0);
|
||||
});
|
||||
|
||||
it(`should call clearSearch() Esc key is pressed and take off 1 from _activeOption`, () => {
|
||||
it(`should call clearSearch() Esc key is pressed and add up 1 to _activeOption`, () => {
|
||||
let controller = $componentController('vnDropDown', {$element, $timeout, $filter});
|
||||
controller.items = [{id: 1, name: 'Batman'}, {id: 2, name: 'Bruce'}, {id: 3, name: 'Logan'}, {id: 4, name: 'Wolverine'}];
|
||||
spyOn(controller, 'setScrollPosition');
|
||||
|
@ -284,4 +284,77 @@ describe('Component vnDropDown', () => {
|
|||
expect(controller._activeOption).toEqual(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setScrollPosition()', () => {
|
||||
it(`should call child.scrollIntoView if defined `, () => {
|
||||
$element[0].firstChild.setAttribute('class', 'dropdown');
|
||||
let child = $element[0].firstChild.firstChild;
|
||||
child.scrollIntoView = () => {};
|
||||
spyOn(child, 'scrollIntoView');
|
||||
let controller = $componentController('vnDropDown', {$element, $timeout, $filter});
|
||||
controller._activeOption = 0;
|
||||
controller.setScrollPosition();
|
||||
|
||||
expect(child.scrollIntoView).toHaveBeenCalledWith();
|
||||
});
|
||||
});
|
||||
|
||||
describe('selectItem()', () => {
|
||||
it(`should pass item to selected and set controller._show to false`, () => {
|
||||
let controller = $componentController('vnDropDown', {$element, $timeout, $filter});
|
||||
let item = {id: 1, name: 'Batman'};
|
||||
controller.selectItem(item);
|
||||
|
||||
expect(controller.selected).toEqual(item);
|
||||
expect(controller._show).toEqual(false);
|
||||
});
|
||||
|
||||
it(`should pass item to selected and set controller._show to true if the controller.multiple is defined`, () => {
|
||||
let controller = $componentController('vnDropDown', {$element, $timeout, $filter});
|
||||
let item = {id: 1, name: 'Batman'};
|
||||
controller.multiple = true;
|
||||
controller.selectItem(item);
|
||||
|
||||
expect(controller.selected).toEqual(item);
|
||||
expect(controller._show).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadItems()', () => {
|
||||
it(`should set controller._show to true`, () => {
|
||||
let controller = $componentController('vnDropDown', {$element, $timeout, $filter});
|
||||
controller.loadItems();
|
||||
|
||||
expect(controller._show).toEqual(true);
|
||||
});
|
||||
|
||||
it(`should call loadMore() and then set controller._show to true`, () => {
|
||||
let controller = $componentController('vnDropDown', {$element, $timeout, $filter}, {showLoadMore: () => {}, loadMore: () => {}});
|
||||
spyOn(controller, 'loadMore');
|
||||
controller.loadItems();
|
||||
|
||||
expect(controller._show).toEqual(true);
|
||||
expect(controller.loadMore).toHaveBeenCalledWith();
|
||||
});
|
||||
});
|
||||
|
||||
describe('$onInit()', () => {
|
||||
it(`should add an event listener to the parent element`, () => {
|
||||
let controller = $componentController('vnDropDown', {$element, $timeout, $filter});
|
||||
spyOn(controller.parent, 'addEventListener');
|
||||
controller.$onInit();
|
||||
|
||||
expect(controller.parent.addEventListener).toHaveBeenCalledWith('keydown', jasmine.any(Function));
|
||||
});
|
||||
});
|
||||
|
||||
describe('$onDestroy()', () => {
|
||||
it(`should remove an event listener from the parent element`, () => {
|
||||
let controller = $componentController('vnDropDown', {$element, $timeout, $filter});
|
||||
spyOn(controller.parent, 'removeEventListener');
|
||||
controller.$onDestroy();
|
||||
|
||||
expect(controller.parent.removeEventListener).toHaveBeenCalledWith('keydown', jasmine.any(Function));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue