From 5e8f95dd65979a4e04973c3a1feb4b0cb1be7822 Mon Sep 17 00:00:00 2001 From: Carlos Date: Wed, 30 Aug 2017 08:38:41 +0200 Subject: [PATCH] Billing data copyData and submit unit tests plus small refactor at billing-data.js --- .../client/src/billing-data/billing-data.js | 7 +++---- .../src/billing-data/billing-data.spec.js | 21 +++++++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/client/client/src/billing-data/billing-data.js b/client/client/src/billing-data/billing-data.js index 9a10095c9..0b374fcbd 100644 --- a/client/client/src/billing-data/billing-data.js +++ b/client/client/src/billing-data/billing-data.js @@ -1,10 +1,9 @@ import ngModule from '../module'; export default class Controller { - constructor($scope, $http, $timeout, vnApp, $translate) { + constructor($scope, $http, vnApp, $translate) { this.$ = $scope; this.$http = $http; - this.timeout = $timeout; this.vnApp = vnApp; this.translate = $translate; this.billData = {}; @@ -24,7 +23,7 @@ export default class Controller { } } submit() { - this.$.watcher.submit().then( + return this.$.watcher.submit().then( () => this.checkPaymentChanges()); } checkPaymentChanges() { @@ -48,7 +47,7 @@ export default class Controller { } } } -Controller.$inject = ['$scope', '$http', '$timeout', 'vnApp', '$translate']; +Controller.$inject = ['$scope', '$http', 'vnApp', '$translate']; ngModule.component('vnClientBillingData', { template: require('./billing-data.html'), diff --git a/client/client/src/billing-data/billing-data.spec.js b/client/client/src/billing-data/billing-data.spec.js index da2e2f21d..e6984da89 100644 --- a/client/client/src/billing-data/billing-data.spec.js +++ b/client/client/src/billing-data/billing-data.spec.js @@ -3,17 +3,17 @@ import './billing-data.js'; describe('Component vnClientBillingData', () => { let $componentController; let $scope; - let vnApp; beforeEach(() => { angular.mock.module('client'); }); - beforeEach(angular.mock.inject(function(_$componentController_, $rootScope, _vnApp_) { + beforeEach(angular.mock.inject(function(_$componentController_, $rootScope) { $componentController = _$componentController_; $scope = $rootScope.$new(); - vnApp = _vnApp_; - spyOn(vnApp, 'showError'); + // try to instanciate the actual watcher instead of submit spy usage. + let submit = jasmine.createSpy('submit').and.returnValue(Promise.resolve()); + $scope.watcher = {submit}; })); describe('copyData()', () => { @@ -32,4 +32,17 @@ describe('Component vnClientBillingData', () => { expect(controller.billData).toEqual(controller.client); }); }); + + describe('submit()', () => { + it(`should call submit() on the watcher then receive a callback`, done => { + let controller = $componentController('vnClientBillingData', {$scope: $scope}); + spyOn(controller, 'checkPaymentChanges'); + controller.submit() + .then(() => { + expect(controller.$.watcher.submit).toHaveBeenCalled(); + expect(controller.checkPaymentChanges).toHaveBeenCalled(); + done(); + }); + }); + }); });