diff --git a/app/scripts/controllers/client/UploadClientIdentifierDocumentController.js b/app/scripts/controllers/client/UploadClientIdentifierDocumentController.js index 85a00d24..6a21b673 100644 --- a/app/scripts/controllers/client/UploadClientIdentifierDocumentController.js +++ b/app/scripts/controllers/client/UploadClientIdentifierDocumentController.js @@ -1,6 +1,6 @@ (function (module) { mifosX.controllers = _.extend(module, { - UploadClientIdentifierDocumentController: function (scope, location, http, routeParams, API_VERSION, $upload, $rootScope) { + UploadClientIdentifierDocumentController: function (scope, location, routeParams, API_VERSION, $upload, $rootScope) { scope.clientId = routeParams.clientId; scope.resourceId = routeParams.resourceId; scope.onFileSelect = function ($files) { @@ -9,20 +9,20 @@ scope.submit = function () { $upload.upload({ - url: $rootScope.hostUrl + API_VERSION + '/client_identifiers/' + scope.resourceId + '/documents', + url: $rootScope.hostUrl + API_VERSION + '/client_identifiers/' + scope.resourceId + '/documents', data: scope.formData, file: scope.file }).then(function (data) { // to fix IE not refreshing the model if (!scope.$$phase) { - scope.$apply(); + scope.$apply(); } location.path('/viewclient/' + scope.clientId); }); }; } }); - mifosX.ng.application.controller('UploadClientIdentifierDocumentController', ['$scope', '$location', '$http', '$routeParams', 'API_VERSION', '$upload', '$rootScope', mifosX.controllers.UploadClientIdentifierDocumentController]).run(function ($log) { + mifosX.ng.application.controller('UploadClientIdentifierDocumentController', ['$scope', '$location', '$routeParams', 'API_VERSION', '$upload', '$rootScope', mifosX.controllers.UploadClientIdentifierDocumentController]).run(function ($log) { $log.info("UploadClientIdentifierDocumentController initialized"); }); -}(mifosX.controllers || {})); \ No newline at end of file +}(mifosX.controllers || {})); diff --git a/test/spec/controllers/client/UploadClientIdentifierDocumentController_spec.js b/test/spec/controllers/client/UploadClientIdentifierDocumentController_spec.js new file mode 100644 index 00000000..a6a86eec --- /dev/null +++ b/test/spec/controllers/client/UploadClientIdentifierDocumentController_spec.js @@ -0,0 +1,56 @@ +describe('UploadClientIdentifierDocumentController', function() { + var deferedUpload = undefined; + + beforeEach(inject(function($rootScope, $q) { + this.scope = $rootScope.$new(); + this.location = jasmine.createSpyObj('location', ['path']); + this.routeParams = {clientId: 'clientId', resourceId: 'resourceId'}; + this.API_VERSION = 'api_version'; + this.upload = { + upload: jasmine.createSpy().andCallFake(function(){ + deferedUpload = $q.defer(); + return deferedUpload.promise; + }) + } + this.$rootScope = $rootScope; + this.controller = new mifosX.controllers.UploadClientIdentifierDocumentController( + this.scope, + this.location, + this.routeParams, + this.API_VERSION, + this.upload, + this.$rootScope + ); + })); + + it('should put the select file in the scope', function(){ + var file = {name: 'some file'} + this.scope.onFileSelect ([file]); + expect(this.scope.file).toBe(file); + }); + + describe('on submit', function(){ + var url; + beforeEach(function(){ + url = this.$rootScope.hostUrl + this.API_VERSION + '/client_identifiers/' + this.routeParams.resourceId + '/documents'; + this.scope.file = {name: 'some file'} + this.scope.formData = 'formData'; + }); + + it('should call the upload service', function(){ + this.scope.submit(); + expect(this.upload.upload).toHaveBeenCalledWith({ + url: url, + data: this.scope.formData, + file: this.scope.file, + }); + }); + + it('should change the location after file has been uploaded', function(){ + this.scope.submit(); + deferedUpload.resolve('data'); + this.scope.$digest(); + expect(this.location.path).toHaveBeenCalledWith('/viewclient/' + this.routeParams.clientId); + }); + }); +});