client side unit test for vnFocus directive tests

This commit is contained in:
Carlos 2017-10-23 16:25:47 +02:00
parent 5e3d61d1f1
commit c88023182a
1 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,55 @@
describe('Directive focus', () => {
let $scope;
let $element;
let compile;
beforeEach(() => {
angular.mock.module('client');
});
compile = (_element, _childElement) => {
inject(($compile, $rootScope) => {
$scope = $rootScope.$new();
$element = angular.element(_element);
if (_childElement) {
let childElement = angular.element(_childElement);
$element[0] < childElement;
$element[0].firstChild.focus = jasmine.createSpy(focus);
}
$element[0].focus = jasmine.createSpy('focus');
$element[0].select = jasmine.createSpy('select');
$compile($element)($scope);
$scope.$digest();
});
};
it('should call the querySelector function upon the input to redefine it with the expected selector then call focus', () => {
let html = `<div vn-focus><input></input></div>`;
let childHtml = '<input></input>';
compile(html, childHtml);
expect($element[0].firstChild.focus).toHaveBeenCalled();
});
it('should print a warning message on console', () => {
let html = `<potato vn-focus></potato>`;
console.warn = jasmine.createSpy('warn');
compile(html);
expect(console.warn).toHaveBeenCalledWith(`vnFocus: Can't find a focusable element`);
});
it('should call focus function on the element', () => {
let html = `<input vn-focus></input>`;
compile(html);
expect($element[0].focus).toHaveBeenCalledWith();
});
it('should call select function on the element', () => {
let html = `<input vn-focus></input>`;
compile(html);
expect($element[0].select).toHaveBeenCalledWith();
});
});