From f062c802ae49451799c9c79257b54e2e15cb4bae Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Thu, 5 Mar 2020 10:08:02 +0100 Subject: [PATCH 1/4] Unit test --- front/core/directives/http-click.js | 2 +- .../core/directives/specs/http-click.spec.js | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 front/core/directives/specs/http-click.spec.js diff --git a/front/core/directives/http-click.js b/front/core/directives/http-click.js index 4fd932289..c994878ee 100644 --- a/front/core/directives/http-click.js +++ b/front/core/directives/http-click.js @@ -17,7 +17,7 @@ export function directive($parse) { const controller = element.$ctrl; controller.$oldDisabled = controller.disabled; controller.disabled = true; - + console.log(cb()); cb($scope).finally(() => { if (!controller.$oldDisabled) controller.disabled = false; diff --git a/front/core/directives/specs/http-click.spec.js b/front/core/directives/specs/http-click.spec.js new file mode 100644 index 000000000..e50925177 --- /dev/null +++ b/front/core/directives/specs/http-click.spec.js @@ -0,0 +1,46 @@ +fdescribe('Directive http-click', () => { + let $scope; + let $element; + let element; + let compile; + let controller; + + beforeEach(ngModule('vnCore')); + + compile = (_element, _childElement) => { + inject(($componentController, $compile, $rootScope) => { + $scope = $rootScope.$new(); + element = angular.element(_element); + $compile(element)($scope); + + /* controller = element.controller('vnHttpClick'); + controller.myEvent = () => { + return Promise.resolve('hola'); + }; */ + + controller = $componentController('vnHttpClick', {$element: element, $scope: $scope}); + controller.myEvent = () => { + return Promise.resolve('hola'); + }; + + $scope.$digest(); + }); + }; + + beforeEach(angular.mock.inject(($rootScope, $compile) => { + $element = $compile('')($rootScope); + controller = $element.controller('vnHttpClick'); + })); + + it('should call focus function on the element', () => { + // jest.spyOn(controller, 'myEvent').mockReturnValue(Promise.resolve()); + + let html = ``; + compile(html); + + element[0].$ctrl = {}; + element[0].click(); + + // expect($element[0].focus).toHaveBeenCalledWith(); + }); +}); From e0f87b4b06306556cfcb907daee1355783d08c03 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Sun, 8 Mar 2020 17:42:49 +0100 Subject: [PATCH 2/4] http-click unit test --- front/core/directives/http-click.js | 2 +- .../core/directives/specs/http-click.spec.js | 70 ++++++++++++------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/front/core/directives/http-click.js b/front/core/directives/http-click.js index c994878ee..4fd932289 100644 --- a/front/core/directives/http-click.js +++ b/front/core/directives/http-click.js @@ -17,7 +17,7 @@ export function directive($parse) { const controller = element.$ctrl; controller.$oldDisabled = controller.disabled; controller.disabled = true; - console.log(cb()); + cb($scope).finally(() => { if (!controller.$oldDisabled) controller.disabled = false; diff --git a/front/core/directives/specs/http-click.spec.js b/front/core/directives/specs/http-click.spec.js index e50925177..d6b3d9e3b 100644 --- a/front/core/directives/specs/http-click.spec.js +++ b/front/core/directives/specs/http-click.spec.js @@ -1,46 +1,66 @@ fdescribe('Directive http-click', () => { let $scope; - let $element; let element; let compile; - let controller; beforeEach(ngModule('vnCore')); compile = (_element, _childElement) => { - inject(($componentController, $compile, $rootScope) => { + inject(($compile, $rootScope) => { $scope = $rootScope.$new(); element = angular.element(_element); $compile(element)($scope); - - /* controller = element.controller('vnHttpClick'); - controller.myEvent = () => { - return Promise.resolve('hola'); - }; */ - - controller = $componentController('vnHttpClick', {$element: element, $scope: $scope}); - controller.myEvent = () => { - return Promise.resolve('hola'); - }; - $scope.$digest(); }); }; - beforeEach(angular.mock.inject(($rootScope, $compile) => { - $element = $compile('')($rootScope); - controller = $element.controller('vnHttpClick'); - })); - - it('should call focus function on the element', () => { - // jest.spyOn(controller, 'myEvent').mockReturnValue(Promise.resolve()); - - let html = ``; + xit('should call click function on the element, disable it and then enable it again', () => { + let html = ``; compile(html); - element[0].$ctrl = {}; + const myPromise = new Promise(resolve => resolve()); + $scope.myEvent = () => { + return myPromise; + }; + + element[0].$ctrl = {disabled: false}; element[0].click(); - // expect($element[0].focus).toHaveBeenCalledWith(); + expect(element[0].$ctrl.disabled).toEqual(true); + + let finalValue; + myPromise.then(() => { + finalValue = 'called!'; + + expect(element[0].$ctrl.disabled).toEqual(false); + }).finally(() => { + expect(finalValue).toEqual('called!'); + }); + }); + + it('should call click function on the element and not disable it', () => { + let html = ``; + compile(html); + + const myPromise = new Promise(resolve => resolve()); + $scope.myEvent = () => { + return myPromise; + }; + + element[0].$ctrl = {disabled: true}; + element[0].click(); + + expect(element[0].$ctrl.disabled).toEqual(true); + + let finalValue; + myPromise.then(() => { + finalValue = 'called!'; + + expect(element[0].$ctrl.disabled).toEqual(false); + }).finally(() => { + expect(finalValue).toEqual('called!'); + }).catch(err => { + console.log(err); + }); }); }); From bbeca87295bb0d36f553218b6e44679e680ee2b9 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Mon, 9 Mar 2020 12:22:55 +0100 Subject: [PATCH 3/4] 2172 - Added unit test --- front/core/directives/specs/http-click.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/front/core/directives/specs/http-click.spec.js b/front/core/directives/specs/http-click.spec.js index d6b3d9e3b..ac8e77075 100644 --- a/front/core/directives/specs/http-click.spec.js +++ b/front/core/directives/specs/http-click.spec.js @@ -14,7 +14,7 @@ fdescribe('Directive http-click', () => { }); }; - xit('should call click function on the element, disable it and then enable it again', () => { + it('should call click function on the element, disable it and then enable it again', () => { let html = ``; compile(html); @@ -56,7 +56,7 @@ fdescribe('Directive http-click', () => { myPromise.then(() => { finalValue = 'called!'; - expect(element[0].$ctrl.disabled).toEqual(false); + expect(element[0].$ctrl.disabled).toEqual(true); }).finally(() => { expect(finalValue).toEqual('called!'); }).catch(err => { From 41d25078b8ad856b91c20141e5238626bb70c835 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Mon, 9 Mar 2020 12:23:32 +0100 Subject: [PATCH 4/4] Removed focus --- front/core/directives/specs/http-click.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front/core/directives/specs/http-click.spec.js b/front/core/directives/specs/http-click.spec.js index ac8e77075..70a79bcff 100644 --- a/front/core/directives/specs/http-click.spec.js +++ b/front/core/directives/specs/http-click.spec.js @@ -1,4 +1,4 @@ -fdescribe('Directive http-click', () => { +describe('Directive http-click', () => { let $scope; let element; let compile;