|
'use strict'; |
|
|
|
describe('Controller', function() { |
|
|
|
var userProfileWithApiKey = { |
|
"apiKey": "MY-VALID-API-KEY" |
|
}; |
|
|
|
var jsonEnvelopesSuccess = { |
|
"success": true, |
|
"data": [{ |
|
"id": 6, |
|
"type": "Spending", |
|
"description": "Gas", |
|
"balance": 20000, |
|
"monthlyAmount": 20000, |
|
"archived": false, |
|
"splitFunding": false, |
|
"autoTransferLocked": false, |
|
"overflow": false, |
|
"warning": false |
|
}] |
|
}; |
|
|
|
beforeEach(function() { |
|
// spyOn(window, 'getUserProfile').andReturn(userProfileWithApiKey); |
|
angular.mock.module('Controllers'); |
|
angular.mock.module('ajoslin.mobile-navigate'); |
|
angular.mock.module('deviceServices'); |
|
angular.mock.module('appServices'); |
|
}); |
|
|
|
describe('TxController', function() { |
|
|
|
var controller; |
|
var scope; |
|
var routeParams; |
|
|
|
var mockEnvelopesServiceTemplate = { |
|
get: function(parameters, success, error) { |
|
log('parameters: ' + angular.toJson(parameters)); |
|
log('success: ' + success); |
|
log('error: ' + error); |
|
// return success({"success": true,"message": null, "data": jsonEnvelopesSuccess}); |
|
return error({ |
|
"success": false, |
|
"message": "Mocked failure", |
|
"data": null |
|
}); |
|
}, |
|
save: function(parameters, postData, success, error) { |
|
log('parameters: ' + angular.toJson(parameters)); |
|
log('postData: ' + angular.toJson(postData)); |
|
log('success: ' + success); |
|
log('error: ' + error); |
|
// return success({"success": true,"message": null, "data": "whatever"}); |
|
return error({ |
|
"success": false, |
|
"message": "Mocked failure", |
|
"data": null |
|
}); |
|
} |
|
}; |
|
|
|
beforeEach(inject(function($controller, $rootScope, $routeParams) { |
|
log('beforeEach inject: after'); |
|
|
|
scope = $rootScope.$new(); |
|
scope.logAjaxError = function(data, status, headers, config) {}; |
|
spyOn(scope, 'logAjaxError').andReturn(); |
|
routeParams = {}; |
|
controller = $controller('TxController', { |
|
$scope: scope, |
|
$routeParams: routeParams, |
|
Envelopes: { |
|
get: function() {} |
|
} |
|
}); |
|
})); |
|
|
|
it('should be defined', function() { |
|
expect(controller).toBeDefined(); |
|
}); |
|
|
|
it('should have a tx scope variable', function() { |
|
expect(scope.tx).toBeDefined(); |
|
}); |
|
|
|
it('should default tx date to todays date', function() { |
|
expect(scope.tx.date).toBeDefined(); |
|
expect(scope.tx.date).toEqual(controller.getToday()); |
|
}); |
|
|
|
it('should default tx type to expense', function() { |
|
expect(scope.tx.type).toEqual('expense'); |
|
}); |
|
|
|
it('should not default tx envelopeId to anything', function() { |
|
expect(scope.tx.envelopeId).toEqual(undefined); |
|
}); |
|
|
|
it('should set tx envelopeId route parameter envelopeId as integer', inject(function($controller) { |
|
routeParams.envelopeId = '123'; // Always strings |
|
|
|
controller = $controller('TxController', { |
|
$scope: scope, |
|
$routeParams: routeParams, |
|
Envelopes: { |
|
get: function() {} |
|
} |
|
}); |
|
|
|
expect(scope.tx.envelopeId).toEqual(123); |
|
})); |
|
|
|
it('should have an messages scope variable when envelopes data is not successful', inject(function($controller) { |
|
var mockEnvelopesService = { |
|
get: function(parameters, success, error) { |
|
log('parameters: ' + angular.toJson(parameters)); |
|
log('success: ' + success); |
|
log('error: ' + error); |
|
return success({ |
|
"success": false, |
|
"message": "Mocked data failure", |
|
"data": null |
|
}); |
|
} |
|
}; |
|
|
|
controller = $controller('TxController', { |
|
$scope: scope, |
|
$routeParams: routeParams, |
|
Envelopes: mockEnvelopesService |
|
}); |
|
|
|
log('scope.messages: ' + scope.messages); |
|
expect(scope.messages).toBeDefined(); |
|
})); |
|
|
|
it('should have an messages scope variable when envelopes request throws error', inject(function($controller) { |
|
var mockEnvelopesService = { |
|
get: function(parameters, success, error) { |
|
return error(); |
|
} |
|
}; |
|
|
|
controller = $controller('TxController', { |
|
$scope: scope, |
|
$routeParams: routeParams, |
|
Envelopes: mockEnvelopesService |
|
}); |
|
|
|
log('scope.messages: ' + scope.messages); |
|
expect(scope.messages).toBeDefined(); |
|
})); |
|
|
|
it('should have an envelopes scope variable when envelopes data is returned/successful', inject(function($controller) { |
|
var mockEnvelopesService = { |
|
get: function(parameters, success, error) { |
|
log('parameters: ' + angular.toJson(parameters)); |
|
log('success: ' + success); |
|
log('error: ' + error); |
|
return success({ |
|
"success": true, |
|
"message": null, |
|
"data": jsonEnvelopesSuccess |
|
}); |
|
} |
|
}; |
|
|
|
controller = $controller('TxController', { |
|
$scope: scope, |
|
$routeParams: routeParams, |
|
Envelopes: mockEnvelopesService |
|
}); |
|
|
|
log('scope.envelopes: ' + scope.envelopes); |
|
expect(scope.envelopes).toBeDefined(); |
|
})); |
|
|
|
}); |
|
|
|
}); |
Hi, thanks for this example. I'm new to unit testing and i'm utilizing the ngResource just like this, but with the $promise. It would be great if you could demonstrate it using the $promise.