From c88023182ad4cc63e3f8ae3257cb057a7b9a19dc Mon Sep 17 00:00:00 2001 From: Carlos Date: Mon, 23 Oct 2017 16:25:47 +0200 Subject: [PATCH] client side unit test for vnFocus directive tests --- .../core/src/directives/specs/focus.spec.js | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 client/core/src/directives/specs/focus.spec.js diff --git a/client/core/src/directives/specs/focus.spec.js b/client/core/src/directives/specs/focus.spec.js new file mode 100644 index 000000000..f610e9ffb --- /dev/null +++ b/client/core/src/directives/specs/focus.spec.js @@ -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 = `
`; + let childHtml = ''; + compile(html, childHtml); + + expect($element[0].firstChild.focus).toHaveBeenCalled(); + }); + + it('should print a warning message on console', () => { + let html = ``; + 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 = ``; + compile(html); + + expect($element[0].focus).toHaveBeenCalledWith(); + }); + + it('should call select function on the element', () => { + let html = ``; + compile(html); + + expect($element[0].select).toHaveBeenCalledWith(); + }); +});