got karma:unit tests working
This commit is contained in:
parent
44e37ce1da
commit
0f21478c46
|
@ -32,10 +32,10 @@ exports.validateVerificationToken = function(req, res, next){
|
||||||
exports.resendVerificationEmail = function(req, res, next){
|
exports.resendVerificationEmail = function(req, res, next){
|
||||||
nev.resendVerificationEmail(req.body.email, function(user) {
|
nev.resendVerificationEmail(req.body.email, function(user) {
|
||||||
if (user){
|
if (user){
|
||||||
res.status(200).send('User successfully verified');
|
res.status(200).send('Verification email successfully Re-Sent');
|
||||||
}else {
|
}else {
|
||||||
// user hasn't been found yet
|
// user hasn't been found yet
|
||||||
res.status(400).send( {message: 'Error: User could NOT be verified'} );
|
res.status(400).send( {message: 'Error: Verification Email could NOT be sent'} );
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,7 +3,10 @@
|
||||||
/**
|
/**
|
||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
var applicationConfiguration = require('./config/config');
|
var applicationConfiguration = require('./config/config'),
|
||||||
|
bowerFiles = require('main-bower-files');
|
||||||
|
|
||||||
|
var bowerDep = bowerFiles('**/**.js');
|
||||||
|
|
||||||
// Karma configuration
|
// Karma configuration
|
||||||
module.exports = function(config) {
|
module.exports = function(config) {
|
||||||
|
@ -17,11 +20,6 @@ module.exports = function(config) {
|
||||||
// Test results reporter to use
|
// Test results reporter to use
|
||||||
// Possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
|
// Possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
|
||||||
reporters: ['mocha', 'html', 'progress'],
|
reporters: ['mocha', 'html', 'progress'],
|
||||||
|
|
||||||
// plugins: [
|
|
||||||
// 'karma-jasmine',
|
|
||||||
// 'karma-mocha-reporter',
|
|
||||||
// ],
|
|
||||||
|
|
||||||
// Web server port
|
// Web server port
|
||||||
port: 9876,
|
port: 9876,
|
||||||
|
@ -29,6 +27,14 @@ module.exports = function(config) {
|
||||||
// Enable / disable colors in the output (reporters and logs)
|
// Enable / disable colors in the output (reporters and logs)
|
||||||
colors: true,
|
colors: true,
|
||||||
|
|
||||||
|
//Make sure we capture console.log output
|
||||||
|
client: {
|
||||||
|
captureConsole: true,
|
||||||
|
mocha: {
|
||||||
|
bail: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// Level of logging
|
// Level of logging
|
||||||
// Possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
|
// Possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
|
||||||
logLevel: config.LOG_INFO,
|
logLevel: config.LOG_INFO,
|
||||||
|
|
|
@ -24,55 +24,55 @@ angular.module(ApplicationConfiguration.applicationModuleName).constant('USER_RO
|
||||||
superuser: 'superuser',
|
superuser: 'superuser',
|
||||||
});
|
});
|
||||||
|
|
||||||
angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', '$state', '$stateParams',
|
// angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', '$state', '$stateParams',
|
||||||
function($rootScope, Auth, $state, $stateParams) {
|
// function($rootScope, Auth, $state, $stateParams) {
|
||||||
|
|
||||||
$rootScope.$state = $state;
|
// $rootScope.$state = $state;
|
||||||
$rootScope.$stateParams = $stateParams;
|
// $rootScope.$stateParams = $stateParams;
|
||||||
|
|
||||||
// add previous state property
|
// // add previous state property
|
||||||
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState) {
|
// $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState) {
|
||||||
$state.previous = fromState;
|
// $state.previous = fromState;
|
||||||
|
|
||||||
//Redirect to listForms if user is authenticated
|
// //Redirect to listForms if user is authenticated
|
||||||
if(toState.name === 'home' || toState.name === 'signin' || toState.name === 'resendVerifyEmail' || toState.name === 'verify' || toState.name === 'signup' || toState.name === 'signup-success'){
|
// if(toState.name === 'home' || toState.name === 'signin' || toState.name === 'resendVerifyEmail' || toState.name === 'verify' || toState.name === 'signup' || toState.name === 'signup-success'){
|
||||||
if(Auth.isAuthenticated()){
|
// if(Auth.isAuthenticated()){
|
||||||
event.preventDefault(); // stop current execution
|
// event.preventDefault(); // stop current execution
|
||||||
$state.go('listForms'); // go to listForms page
|
// $state.go('listForms'); // go to listForms page
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
//Redirect to 'home' route if user is not authenticated
|
// //Redirect to 'home' route if user is not authenticated
|
||||||
else if(toState.name !== 'access_denied' && !Auth.isAuthenticated() ){
|
// else if(toState.name !== 'access_denied' && !Auth.isAuthenticated() ){
|
||||||
event.preventDefault(); // stop current execution
|
// event.preventDefault(); // stop current execution
|
||||||
$state.go('home'); // go to listForms page
|
// $state.go('home'); // go to listForms page
|
||||||
}
|
// }
|
||||||
|
|
||||||
});
|
// });
|
||||||
|
|
||||||
}
|
// }
|
||||||
]);
|
// ]);
|
||||||
|
|
||||||
//Page access/authorization logic
|
// //Page access/authorization logic
|
||||||
angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', 'User', 'Authorizer', '$state', '$stateParams',
|
// angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', 'User', 'Authorizer', '$state', '$stateParams',
|
||||||
function($rootScope, Auth, User, Authorizer, $state, $stateParams) {
|
// function($rootScope, Auth, User, Authorizer, $state, $stateParams) {
|
||||||
$rootScope.$on('$stateChangeStart', function(event, next) {
|
// $rootScope.$on('$stateChangeStart', function(event, next) {
|
||||||
var authenticator, permissions, user;
|
// var authenticator, permissions, user;
|
||||||
permissions = next && next.data && next.data.permissions ? next.data.permissions : null;
|
// permissions = next && next.data && next.data.permissions ? next.data.permissions : null;
|
||||||
|
|
||||||
Auth.ensureHasCurrentUser(User);
|
// Auth.ensureHasCurrentUser(User);
|
||||||
user = Auth.currentUser;
|
// user = Auth.currentUser;
|
||||||
|
|
||||||
if(user){
|
// if(user){
|
||||||
authenticator = new Authorizer(user);
|
// authenticator = new Authorizer(user);
|
||||||
|
|
||||||
if( (permissions !== null) && !authenticator.canAccess(permissions) ){
|
// if( (permissions !== null) && !authenticator.canAccess(permissions) ){
|
||||||
event.preventDefault();
|
// event.preventDefault();
|
||||||
console.log('access denied')
|
// console.log('access denied')
|
||||||
$state.go('access_denied');
|
// $state.go('access_denied');
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
}]);
|
// }]);
|
||||||
|
|
||||||
//Then define the init function for starting up the application
|
//Then define the init function for starting up the application
|
||||||
angular.element(document).ready(function() {
|
angular.element(document).ready(function() {
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
angular.module('forms').controller('AdminFormController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', '$http', '$modal',
|
angular.module('forms').controller('AdminFormController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', '$http', '$modal',
|
||||||
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http, $modal) {
|
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http, $modal) {
|
||||||
|
|
||||||
var deleteModal;
|
|
||||||
$scope = $rootScope;
|
$scope = $rootScope;
|
||||||
|
|
||||||
$scope.myform = CurrentForm.getForm();
|
$scope.myform = CurrentForm.getForm();
|
||||||
|
@ -17,7 +16,6 @@ angular.module('forms').controller('AdminFormController', ['$rootScope', '$scope
|
||||||
});
|
});
|
||||||
CurrentForm.setForm($scope.myform);
|
CurrentForm.setForm($scope.myform);
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.setForm = function(form){
|
$scope.setForm = function(form){
|
||||||
$scope.myform = form;
|
$scope.myform = form;
|
||||||
};
|
};
|
||||||
|
@ -31,82 +29,70 @@ angular.module('forms').controller('AdminFormController', ['$rootScope', '$scope
|
||||||
** DeleteModal Functions
|
** DeleteModal Functions
|
||||||
*/
|
*/
|
||||||
$scope.openDeleteModal = function(){
|
$scope.openDeleteModal = function(){
|
||||||
|
$scope.deleteModal = $modal.open({
|
||||||
deleteModal = $modal.open({
|
|
||||||
animation: $scope.animationsEnabled,
|
animation: $scope.animationsEnabled,
|
||||||
templateUrl: 'myModalContent.html',
|
templateUrl: 'myModalContent.html',
|
||||||
controller: 'AdminFormController',
|
controller: 'AdminFormController',
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
$scope.cancelDeleteModal = function(){
|
$scope.cancelDeleteModal = function(){
|
||||||
if(deleteModal){
|
if($scope.deleteModal){
|
||||||
deleteModal.dismiss('cancel');
|
$scope.deleteModal.dismiss('cancel');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Remove existing Form
|
// Remove existing Form
|
||||||
$scope.remove = function(form_id) {
|
$scope.removeCurrentForm = function() {
|
||||||
if(deleteModal && deleteModal.opened){
|
if($scope.deleteModal && $scope.deleteModal.opened){
|
||||||
|
|
||||||
deleteModal.close();
|
$scope.deleteModal.close();
|
||||||
|
|
||||||
var form = {};
|
var form_id = $scope.myform._id;
|
||||||
if(!form_id){
|
if(!form_id) throw new Error('Error - removeCurrentForm(): $scope.myform._id does not exist');
|
||||||
form = CurrentForm.getForm();
|
|
||||||
if(!form) form = $scope.myform;
|
|
||||||
}else {
|
|
||||||
form._id = form_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
$http.delete('/forms/'+form._id)
|
$http.delete('/forms/'+form_id)
|
||||||
.success(function(data, status, headers){
|
.success(function(data, status, headers){
|
||||||
console.log('form deleted successfully');
|
console.log('form deleted successfully');
|
||||||
|
|
||||||
if(!form_id){
|
$state.go('listForms', {}, {reload: true});
|
||||||
$state.go('listForms', {}, {reload: true});
|
|
||||||
}
|
|
||||||
if($scope.myforms.length > 0){
|
|
||||||
$scope.myforms = _.filter($scope.myforms, function(myform){
|
|
||||||
return myform._id !== form._id;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}).error(function(error){
|
}).error(function(error){
|
||||||
console.log('ERROR: Form could not be deleted.');
|
console.log('ERROR: Form could not be deleted.');
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}).finally(function(){
|
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Update existing Form
|
// Update existing Form
|
||||||
$scope.update = $rootScope.update = function(shouldUpdateNow, cb){
|
$scope.update = $rootScope.update = function(updateImmediately, cb){
|
||||||
// console.log('shouldUpdateNow: '+shouldUpdateNow);
|
|
||||||
var continueUpdate = true;
|
var continueUpdate = true;
|
||||||
if(shouldUpdateNow){
|
if(!updateImmediately){
|
||||||
continueUpdate = !$rootScope.saveInProgress;
|
continueUpdate = !$rootScope.saveInProgress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Update form if we **are not currently updating** or if **shouldUpdateNow flag is set**
|
||||||
if(continueUpdate){
|
if(continueUpdate){
|
||||||
console.log('begin updating form');
|
// console.log('begin updating form');
|
||||||
var err = null;
|
var err = null;
|
||||||
|
|
||||||
if(shouldUpdateNow){ $rootScope.saveInProgress = true; }
|
if(!updateImmediately){ $rootScope.saveInProgress = true; }
|
||||||
|
|
||||||
$scope.updatePromise = $http.put('/forms/'+$scope.myform._id, {form: $scope.myform})
|
$scope.updatePromise = $http.put('/forms/'+$scope.myform._id, {form: $scope.myform})
|
||||||
.then(function(response){
|
.then(function(response){
|
||||||
$rootScope.myform = $scope.myform = response.data;
|
$rootScope.myform = $scope.myform = response.data;
|
||||||
console.log(response.data);
|
// console.log(response.data);
|
||||||
}).catch(function(response){
|
}).catch(function(response){
|
||||||
console.log('Error occured during form UPDATE.\n');
|
// console.log('Error occured during form UPDATE.\n');
|
||||||
console.log(response.data);
|
// console.log(response.data);
|
||||||
err = response.data;
|
err = response.data;
|
||||||
}).finally(function() {
|
}).finally(function() {
|
||||||
console.log('finished updating');
|
// console.log('finished updating');
|
||||||
if(shouldUpdateNow){$rootScope.saveInProgress = false; }
|
if(!updateImmediately){$rootScope.saveInProgress = false; }
|
||||||
cb(err);
|
|
||||||
|
if( (typeof cb) === 'function'){
|
||||||
|
cb(err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Forms controller
|
// Forms controller
|
||||||
angular.module('forms').controller('ListFormsController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm','$http',
|
angular.module('forms').controller('ListFormsController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', '$http',
|
||||||
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http) {
|
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http) {
|
||||||
|
|
||||||
$scope = $rootScope;
|
$scope = $rootScope;
|
||||||
|
@ -33,13 +33,14 @@ angular.module('forms').controller('ListFormsController', ['$rootScope', '$scope
|
||||||
$state.go(route, {'formId': id}, {reload: true});
|
$state.go(route, {'formId': id}, {reload: true});
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.duplicate = function(form, form_index){
|
$scope.duplicate = function(form_index){
|
||||||
|
var form = $scope.myforms[form_index];
|
||||||
delete form._id;
|
delete form._id;
|
||||||
|
|
||||||
$http.post('/forms', {form: form})
|
$http.post('/forms', {form: form})
|
||||||
.success(function(data, status, headers){
|
.success(function(data, status, headers){
|
||||||
console.log('form duplicated');
|
// console.log('form duplicated');
|
||||||
$scope.myforms.splice(form_index, 0, data);
|
$scope.myforms.splice(form_index+1, 0, data);
|
||||||
}).error(function(errorResponse){
|
}).error(function(errorResponse){
|
||||||
console.log(errorResponse);
|
console.log(errorResponse);
|
||||||
$scope.error = errorResponse.data.message;
|
$scope.error = errorResponse.data.message;
|
||||||
|
@ -69,19 +70,16 @@ angular.module('forms').controller('ListFormsController', ['$rootScope', '$scope
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.removeFromList = function(deleted_form_id) {
|
$scope.removeForm = function(form_index) {
|
||||||
|
if(form_index >= $scope.myforms.length || form_index < 0){
|
||||||
|
throw new Error('Error: form_index in removeForm() must be between 0 and '+$scope.myforms.length-1);
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Remove existing form');
|
$http.delete('/forms/'+$scope.myforms[form_index]._id)
|
||||||
|
|
||||||
$http.delete('/forms/'+deleted_form_id)
|
|
||||||
.success(function(data, status, headers){
|
.success(function(data, status, headers){
|
||||||
console.log('form deleted successfully');
|
console.log('form deleted successfully');
|
||||||
|
|
||||||
if($scope.myforms.length > 0){
|
$scope.myforms.splice(form_index, 1);
|
||||||
$scope.myforms = _.filter($scope.myforms, function(myform){
|
|
||||||
return myform._id !== deleted_form_id;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}).error(function(error){
|
}).error(function(error){
|
||||||
console.log('ERROR: Form could not be deleted.');
|
console.log('ERROR: Form could not be deleted.');
|
||||||
|
|
|
@ -37,12 +37,10 @@ angular.module('forms').directive('autoSaveForm', ['$rootScope', '$timeout', fun
|
||||||
|
|
||||||
var debounceSave = function () {
|
var debounceSave = function () {
|
||||||
$rootScope.saveInProgress = true;
|
$rootScope.saveInProgress = true;
|
||||||
$rootScope[$attrs.autoSaveCallback](false,
|
$rootScope[$attrs.autoSaveCallback](true,
|
||||||
function(err){
|
function(err){
|
||||||
if(!err){
|
if(!err){
|
||||||
console.log('\n\nForm data persisted -- setting pristine flag');
|
console.log('\n\nForm data persisted -- setting pristine flag');
|
||||||
// console.log('\n\n---------\nUpdate form CLIENT');
|
|
||||||
// console.log(Date.now());
|
|
||||||
$formCtrl.$setPristine();
|
$formCtrl.$setPristine();
|
||||||
}else{
|
}else{
|
||||||
console.error('Error form data NOT persisted');
|
console.error('Error form data NOT persisted');
|
||||||
|
@ -51,7 +49,7 @@ angular.module('forms').directive('autoSaveForm', ['$rootScope', '$timeout', fun
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//Update/save Form if any Form fields are Dirty and Touched
|
||||||
$scope.$watch(function(newValue, oldValue) {
|
$scope.$watch(function(newValue, oldValue) {
|
||||||
if($scope.anyDirtyAndTouched($scope.editForm) && !$rootScope.saveInProgress){
|
if($scope.anyDirtyAndTouched($scope.editForm) && !$rootScope.saveInProgress){
|
||||||
debounceSave();
|
debounceSave();
|
||||||
|
@ -86,7 +84,9 @@ angular.module('forms').directive('autoSaveForm', ['$rootScope', '$timeout', fun
|
||||||
console.log('Saving Form');
|
console.log('Saving Form');
|
||||||
debounceSave();
|
debounceSave();
|
||||||
});
|
});
|
||||||
}else if($rootScope.finishedRender && $rootScope.saveInProgress){
|
}
|
||||||
|
//If we are finished rendering then form saving should be finished
|
||||||
|
else if($rootScope.finishedRender && $rootScope.saveInProgress){
|
||||||
$rootScope.saveInProgress = false;
|
$rootScope.saveInProgress = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
//Forms service used for communicating with the forms REST endpoints
|
//Forms service used for communicating with the forms REST endpoints
|
||||||
angular.module('forms').service('CurrentForm', ['Forms',
|
angular.module('forms').service('CurrentForm',
|
||||||
function(Forms){
|
function(){
|
||||||
|
|
||||||
//Private variables
|
//Private variables
|
||||||
var _form = {};
|
var _form = {};
|
||||||
|
@ -15,4 +15,4 @@ angular.module('forms').service('CurrentForm', ['Forms',
|
||||||
_form = form;
|
_form = form;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
]);
|
);
|
|
@ -3,21 +3,22 @@
|
||||||
//Forms service used for communicating with the forms REST endpoints
|
//Forms service used for communicating with the forms REST endpoints
|
||||||
angular.module('forms').factory('Forms', ['$resource',
|
angular.module('forms').factory('Forms', ['$resource',
|
||||||
function($resource) {
|
function($resource) {
|
||||||
return $resource('forms/:formId', {
|
return $resource('/forms/:formId', {
|
||||||
formId: '@_id'
|
formId: '@_id'
|
||||||
}, {
|
}, {
|
||||||
'query' : {
|
'query' : {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
isArray: true,
|
isArray: true,
|
||||||
transformResponse: function(data, header) {
|
//DAVID: TODO: Do we really need to get visible_form_fields for a Query?
|
||||||
var forms = angular.fromJson(data);
|
// transformResponse: function(data, header) {
|
||||||
angular.forEach(forms, function(form, idx) {
|
// var forms = angular.fromJson(data);
|
||||||
form.visible_form_fields = _.filter(form.form_fields, function(field){
|
// angular.forEach(forms, function(form, idx) {
|
||||||
return field.deletePreserved === false;
|
// form.visible_form_fields = _.filter(form.form_fields, function(field){
|
||||||
}); //<-- replace each item with an instance of the resource object
|
// return (field.deletePreserved === false);
|
||||||
});
|
// });
|
||||||
return forms;
|
// });
|
||||||
}
|
// return forms;
|
||||||
|
// }
|
||||||
},
|
},
|
||||||
'get' : {
|
'get' : {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
@ -25,8 +26,8 @@ angular.module('forms').factory('Forms', ['$resource',
|
||||||
var form = angular.fromJson(data);
|
var form = angular.fromJson(data);
|
||||||
|
|
||||||
form.visible_form_fields = _.filter(form.form_fields, function(field){
|
form.visible_form_fields = _.filter(form.form_fields, function(field){
|
||||||
return field.deletePreserved === false;
|
return (field.deletePreserved === false);
|
||||||
}); //<-- replace each item with an instance of the resource object
|
});
|
||||||
return form;
|
return form;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,10 +5,74 @@
|
||||||
describe('AdminFormController Tests', function() {
|
describe('AdminFormController Tests', function() {
|
||||||
// Initialize global variables
|
// Initialize global variables
|
||||||
var AdminFormController,
|
var AdminFormController,
|
||||||
|
createAdminFormController,
|
||||||
scope,
|
scope,
|
||||||
$httpBackend,
|
$httpBackend,
|
||||||
$stateParams,
|
$stateParams,
|
||||||
$location;
|
$location,
|
||||||
|
$state;
|
||||||
|
|
||||||
|
var sampleUser = {
|
||||||
|
firstName: 'Full',
|
||||||
|
lastName: 'Name',
|
||||||
|
email: 'test@test.com',
|
||||||
|
username: 'test@test.com',
|
||||||
|
password: 'password',
|
||||||
|
provider: 'local',
|
||||||
|
roles: ['user'],
|
||||||
|
_id: 'ed873933b1f1dea0ce12fab9'
|
||||||
|
};
|
||||||
|
|
||||||
|
var sampleForm = {
|
||||||
|
title: 'Form Title',
|
||||||
|
admin: 'ed873933b1f1dea0ce12fab9',
|
||||||
|
language: 'english',
|
||||||
|
form_fields: [
|
||||||
|
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'nascar', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'hockey', 'fieldValue': '', 'deletePreserved': false}
|
||||||
|
],
|
||||||
|
_id: '525a8422f6d0f87f0e407a33'
|
||||||
|
};
|
||||||
|
|
||||||
|
var expectedForm = {
|
||||||
|
title: 'Form Title',
|
||||||
|
admin: 'ed873933b1f1dea0ce12fab9',
|
||||||
|
language: 'english',
|
||||||
|
form_fields: [
|
||||||
|
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'nascar', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'hockey', 'fieldValue': '', 'deletePreserved': false}
|
||||||
|
],
|
||||||
|
visible_form_fields: [
|
||||||
|
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'nascar', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'hockey', 'fieldValue': '', 'deletePreserved': false}
|
||||||
|
],
|
||||||
|
_id: '525a8422f6d0f87f0e407a33'
|
||||||
|
};
|
||||||
|
|
||||||
|
var fakeModal = function(){
|
||||||
|
this.opened = true;
|
||||||
|
|
||||||
|
this.result = function(confirmCallback, cancelCallback) {
|
||||||
|
//Store the callbacks for later when the user clicks on the OK or Cancel button of the dialog
|
||||||
|
this.confirmCallBack = confirmCallback;
|
||||||
|
this.cancelCallback = cancelCallback;
|
||||||
|
};
|
||||||
|
this.close = function( item ) {
|
||||||
|
//The user clicked OK on the modal dialog, call the stored confirm callback with the selected item
|
||||||
|
this.opened = false;
|
||||||
|
this.confirmCallBack( item );
|
||||||
|
};
|
||||||
|
this.dismiss = function( type ) {
|
||||||
|
//The user clicked cancel on the modal dialog, call the stored cancel callback
|
||||||
|
this.opened = false;
|
||||||
|
this.cancelCallback( type );
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// The $resource service augments the response object with methods for updating and deleting the resource.
|
// The $resource service augments the response object with methods for updating and deleting the resource.
|
||||||
// If we were to use the standard toEqual matcher, our tests would fail because the test values would not match
|
// If we were to use the standard toEqual matcher, our tests would fail because the test values would not match
|
||||||
|
@ -29,156 +93,185 @@
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Then we can start by loading the main application module
|
//Mock Users Service
|
||||||
|
beforeEach(module(function($provide) {
|
||||||
|
$provide.service('User', function($q) {
|
||||||
|
return {
|
||||||
|
getCurrent: function() {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
deferred.resolve( JSON.stringify(sampleUser) );
|
||||||
|
return deferred.promise;
|
||||||
|
},
|
||||||
|
login: function(credentials) {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
if( credentials.password === sampleUser.password && credentials.username === sampleUser.username){
|
||||||
|
deferred.resolve( JSON.stringify(sampleUser) );
|
||||||
|
}else {
|
||||||
|
deferred.resolve('Error: User could not be loggedin');
|
||||||
|
}
|
||||||
|
|
||||||
|
return deferred.promise;
|
||||||
|
},
|
||||||
|
logout: function() {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
deferred.resolve(null);
|
||||||
|
return deferred.promise;
|
||||||
|
},
|
||||||
|
signup: function(credentials) {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
if( credentials.password === sampleUser.password && credentials.username === sampleUser.username){
|
||||||
|
deferred.resolve( JSON.stringify(sampleUser) );
|
||||||
|
}else {
|
||||||
|
deferred.resolve('Error: User could not be signed up');
|
||||||
|
}
|
||||||
|
|
||||||
|
return deferred.promise;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
//Mock Authentication Service
|
||||||
|
beforeEach(module(function($provide) {
|
||||||
|
$provide.service('Auth', function() {
|
||||||
|
return {
|
||||||
|
ensureHasCurrentUser: function() {
|
||||||
|
return sampleUser;
|
||||||
|
},
|
||||||
|
isAuthenticated: function() {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
getUserState: function() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Load the main application module
|
||||||
beforeEach(module(ApplicationConfiguration.applicationModuleName));
|
beforeEach(module(ApplicationConfiguration.applicationModuleName));
|
||||||
|
|
||||||
|
beforeEach(module('stateMock'));
|
||||||
|
|
||||||
|
beforeEach(inject(function($modal) {
|
||||||
|
spyOn($modal, 'open').and.returnValue(new fakeModal());
|
||||||
|
}));
|
||||||
|
|
||||||
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
|
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
|
||||||
// This allows us to inject a service but then attach it to a variable
|
// This allows us to inject a service but then attach it to a variable
|
||||||
// with the same name as the service.
|
// with the same name as the service.
|
||||||
beforeEach(inject(function($controller, $rootScope, _$location_, _$state_, _$stateParams_, _$httpBackend_) {
|
beforeEach(inject(function($controller, $rootScope, _$state_, _$location_, _$stateParams_, _$httpBackend_, CurrentForm, Forms) {
|
||||||
// Set a new global scope
|
// Set a new global scope
|
||||||
scope = $rootScope.$new();
|
scope = $rootScope.$new();
|
||||||
|
|
||||||
|
//Set CurrentForm
|
||||||
|
CurrentForm.setForm(sampleForm);
|
||||||
|
|
||||||
// Point global variables to injected services
|
// Point global variables to injected services
|
||||||
$stateParams = _$stateParams_;
|
$stateParams = _$stateParams_;
|
||||||
$httpBackend = _$httpBackend_;
|
$httpBackend = _$httpBackend_;
|
||||||
$location = _$location_;
|
$location = _$location_;
|
||||||
|
$state = _$state_;
|
||||||
|
|
||||||
|
$httpBackend.whenGET(/\.html$/).respond('');
|
||||||
|
$httpBackend.whenGET('/users/me/').respond('');
|
||||||
|
|
||||||
// Initialize the Forms controller.
|
// Initialize the Forms controller.
|
||||||
AdminFormController = $controller('AdminFormController', {
|
createAdminFormController = function(){
|
||||||
$scope: scope,
|
return $controller('AdminFormController', { $scope: scope });
|
||||||
});
|
};
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('AdminFormController should fetch current Form when instantiated', inject(function() {
|
||||||
|
// Run controller functionality
|
||||||
|
var controller = createAdminFormController();
|
||||||
|
|
||||||
|
// Test scope value
|
||||||
|
expect(scope.myform).toEqualData(sampleForm);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
//Mock Authentication Service
|
|
||||||
$provide.value("MyLoginServce", {
|
|
||||||
ensureHasCurrentUser: function(){
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
redirectForLogin: function {}
|
|
||||||
});
|
|
||||||
|
|
||||||
it('$scope.findOne() should fetch current Form', inject(function(Forms) {
|
it('$scope.findOne() should fetch current Form', inject(function(Forms) {
|
||||||
|
|
||||||
// Define a sample article object
|
// Define a sample article object
|
||||||
var sampleForm = new Forms({
|
var expectedFormObj = new Forms(expectedForm);
|
||||||
title: 'Form Title',
|
|
||||||
admin: 'ed873933b1f1dea0ce12fab9',
|
var controller = createAdminFormController();
|
||||||
language: 'english',
|
|
||||||
form_fields: [
|
|
||||||
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': ''},
|
|
||||||
{'fieldType':'checkbox', 'title':'nascar', 'fieldValue': ''},
|
|
||||||
{'fieldType':'checkbox', 'title':'hockey', 'fieldValue': ''}
|
|
||||||
]
|
|
||||||
});
|
|
||||||
// Set the URL parameter
|
// Set the URL parameter
|
||||||
$stateParams.formId = '525a8422f6d0f87f0e407a33';
|
$stateParams.formId = expectedForm._id;
|
||||||
|
|
||||||
// Set GET response
|
// Set GET response
|
||||||
$httpBackend.expectGET(/forms\/([0-9a-fA-F]{24})$/).respond(sampleForm);
|
$httpBackend.expectGET(/^(\/forms\/)([0-9a-fA-F]{24})$/).respond(200, sampleForm);
|
||||||
|
|
||||||
// Run controller functionality
|
// Run controller functionality
|
||||||
scope.findOne();
|
scope.findOne();
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
|
|
||||||
// Test scope value
|
// Test scope value
|
||||||
expect(scope.article).toEqualData(sampleArticle);
|
expect( scope.myform.toJSON() ).toEqualData(expectedFormObj.toJSON());
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// it('$scope.find() should create an array with at least one article object fetched from XHR', inject(function(Forms) {
|
it('$scope.removeCurrentForm() with valid form data should send a DELETE request with the id of form', function() {
|
||||||
// // Create sample article using the Forms service
|
var controller = createAdminFormController();
|
||||||
// var sampleArticle = new Forms({
|
|
||||||
// title: 'An Article about MEAN',
|
|
||||||
// content: 'MEAN rocks!'
|
|
||||||
// });
|
|
||||||
|
|
||||||
// // Create a sample Forms array that includes the new article
|
//Set expected $state transition
|
||||||
// var sampleForms = [sampleForm];
|
$state.expectTransitionTo('listForms');
|
||||||
|
|
||||||
// // Set GET response
|
// Set DELETE response
|
||||||
// $httpBackend.expectGET('Forms').respond(sampleForms);
|
$httpBackend.expect('DELETE', /^(\/forms\/)([0-9a-fA-F]{24})$/).respond(200, sampleForm);
|
||||||
|
|
||||||
// // Run controller functionality
|
//Run controller functionality
|
||||||
// scope.find();
|
scope.openDeleteModal();
|
||||||
// $httpBackend.flush();
|
|
||||||
|
|
||||||
// // Test scope value
|
scope.deleteModal.result(function(selectedItem){
|
||||||
// expect(scope.Forms).toEqualData(sampleForms);
|
scope.selected = selectedItem;
|
||||||
// }));
|
}, function(type){
|
||||||
|
$scope.canceled = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
scope.removeCurrentForm();
|
||||||
|
|
||||||
|
$httpBackend.flush();
|
||||||
|
});
|
||||||
|
|
||||||
// it('$scope.create() with valid form data should send a POST request with the form input values and then locate to new object URL', inject(function(Forms) {
|
it('$scope.update() should send a PUT request with the id of form', function() {
|
||||||
// // Create a sample article object
|
var controller = createAdminFormController();
|
||||||
// var sampleArticlePostData = new Forms({
|
|
||||||
// title: 'An Article about MEAN',
|
|
||||||
// content: 'MEAN rocks!'
|
|
||||||
// });
|
|
||||||
|
|
||||||
// // Create a sample article response
|
//Set PUT response
|
||||||
// var sampleArticleResponse = new Forms({
|
$httpBackend.expect('PUT', /^(\/forms\/)([0-9a-fA-F]{24})$/).respond(200, sampleForm);
|
||||||
// _id: '525cf20451979dea2c000001',
|
|
||||||
// title: 'An Article about MEAN',
|
|
||||||
// content: 'MEAN rocks!'
|
|
||||||
// });
|
|
||||||
|
|
||||||
// // Fixture mock form input values
|
//Run controller functionality
|
||||||
// scope.title = 'An Article about MEAN';
|
scope.update(false, null);
|
||||||
// scope.content = 'MEAN rocks!';
|
|
||||||
|
|
||||||
// // Set POST response
|
$httpBackend.flush();
|
||||||
// $httpBackend.expectPOST('Forms', sampleArticlePostData).respond(sampleArticleResponse);
|
});
|
||||||
|
|
||||||
// // Run controller functionality
|
it('$scope.openDeleteModal() should open scope.deleteModal', function() {
|
||||||
// scope.create();
|
var controller = createAdminFormController();
|
||||||
// $httpBackend.flush();
|
|
||||||
|
|
||||||
// // Test form inputs are reset
|
//Run controller functionality
|
||||||
// expect(scope.title).toEqual('');
|
scope.openDeleteModal();
|
||||||
// expect(scope.content).toEqual('');
|
expect( scope.deleteModal.opened ).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
// // Test URL redirection after the article was created
|
it('$scope.cancelDeleteModal() should close scope.deleteModal', inject(function($modal) {
|
||||||
// expect($location.path()).toBe('/Forms/' + sampleArticleResponse._id);
|
var controller = createAdminFormController();
|
||||||
// }));
|
|
||||||
|
|
||||||
// it('$scope.update() should update a valid article', inject(function(Forms) {
|
//Run controller functionality
|
||||||
// // Define a sample article put data
|
scope.openDeleteModal();
|
||||||
// var sampleArticlePutData = new Forms({
|
console.log(scope.deleteModal.opened);
|
||||||
// _id: '525cf20451979dea2c000001',
|
|
||||||
// title: 'An Article about MEAN',
|
|
||||||
// content: 'MEAN Rocks!'
|
|
||||||
// });
|
|
||||||
|
|
||||||
// // Mock article in scope
|
scope.deleteModal.result(function(selectedItem){
|
||||||
// scope.article = sampleArticlePutData;
|
this.selected = selectedItem;
|
||||||
|
}, function(type){
|
||||||
|
this.canceled = true;
|
||||||
|
});
|
||||||
|
|
||||||
// // Set PUT response
|
scope.cancelDeleteModal();
|
||||||
// $httpBackend.expectPUT(/Forms\/([0-9a-fA-F]{24})$/).respond();
|
console.log(scope.deleteModal.opened);
|
||||||
|
expect( scope.deleteModal.opened ).toEqual(false);
|
||||||
|
expect( scope.deleteModal.canceled ).toEqual(true);
|
||||||
|
|
||||||
// // Run controller functionality
|
}));
|
||||||
// scope.update();
|
|
||||||
// $httpBackend.flush();
|
|
||||||
|
|
||||||
// // Test URL location to new object
|
|
||||||
// expect($location.path()).toBe('/Forms/' + sampleArticlePutData._id);
|
|
||||||
// }));
|
|
||||||
|
|
||||||
// it('$scope.remove() should send a DELETE request with a valid articleId and remove the article from the scope', inject(function(Forms) {
|
|
||||||
// // Create new article object
|
|
||||||
// var sampleArticle = new Forms({
|
|
||||||
// _id: '525a8422f6d0f87f0e407a33'
|
|
||||||
// });
|
|
||||||
|
|
||||||
// // Create new Forms array and include the article
|
|
||||||
// scope.Forms = [sampleArticle];
|
|
||||||
|
|
||||||
// // Set expected DELETE response
|
|
||||||
// $httpBackend.expectDELETE(/Forms\/([0-9a-fA-F]{24})$/).respond(204);
|
|
||||||
|
|
||||||
// // Run controller functionality
|
|
||||||
// scope.remove(sampleArticle);
|
|
||||||
// $httpBackend.flush();
|
|
||||||
|
|
||||||
// // Test array after successful delete
|
|
||||||
// expect(scope.Forms.length).toBe(0);
|
|
||||||
// }));
|
|
||||||
});
|
});
|
||||||
}());
|
}());
|
255
public/modules/forms/tests/list-forms.client.controller.test.js
Normal file
255
public/modules/forms/tests/list-forms.client.controller.test.js
Normal file
|
@ -0,0 +1,255 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
// Forms Controller Spec
|
||||||
|
describe('ListFormsController Tests', function() {
|
||||||
|
// Initialize global variables
|
||||||
|
var ListFormsController,
|
||||||
|
createListFormsController,
|
||||||
|
scope,
|
||||||
|
$httpBackend,
|
||||||
|
$stateParams,
|
||||||
|
$location,
|
||||||
|
$state;
|
||||||
|
|
||||||
|
var sampleUser = {
|
||||||
|
firstName: 'Full',
|
||||||
|
lastName: 'Name',
|
||||||
|
email: 'test@test.com',
|
||||||
|
username: 'test@test.com',
|
||||||
|
password: 'password',
|
||||||
|
provider: 'local',
|
||||||
|
roles: ['user'],
|
||||||
|
_id: 'ed873933b1f1dea0ce12fab9'
|
||||||
|
};
|
||||||
|
|
||||||
|
var sampleForm = {
|
||||||
|
title: 'Form Title',
|
||||||
|
admin: 'ed873933b1f1dea0ce12fab9',
|
||||||
|
language: 'english',
|
||||||
|
form_fields: [
|
||||||
|
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'nascar', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'hockey', 'fieldValue': '', 'deletePreserved': false}
|
||||||
|
],
|
||||||
|
_id: '525a8422f6d0f87f0e407a33'
|
||||||
|
};
|
||||||
|
|
||||||
|
var sampleFormList = [{
|
||||||
|
title: 'Form Title1',
|
||||||
|
admin: 'ed873933b1f1dea0ce12fab9',
|
||||||
|
language: 'english',
|
||||||
|
form_fields: [
|
||||||
|
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'nascar', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'hockey', 'fieldValue': '', 'deletePreserved': false}
|
||||||
|
],
|
||||||
|
_id: '525a8422f6d0f87f0e407a33'
|
||||||
|
},{
|
||||||
|
title: 'Form Title2',
|
||||||
|
admin: '39223933b1f1dea0ce12fab9',
|
||||||
|
language: 'english',
|
||||||
|
form_fields: [
|
||||||
|
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'nascar', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'hockey', 'fieldValue': '', 'deletePreserved': false}
|
||||||
|
],
|
||||||
|
_id: '52f6d0f87f5a407a384220e3'
|
||||||
|
},{
|
||||||
|
title: 'Form Title3',
|
||||||
|
admin: '2fab9ed873937f0e1dea0ce1',
|
||||||
|
language: 'english',
|
||||||
|
form_fields: [
|
||||||
|
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'nascar', 'fieldValue': '', 'deletePreserved': false},
|
||||||
|
{'fieldType':'checkbox', 'title':'hockey', 'fieldValue': '', 'deletePreserved': false}
|
||||||
|
],
|
||||||
|
_id: '922f6d0f87fed8730e4e1233'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
// The $resource service augments the response object with methods for updating and deleting the resource.
|
||||||
|
// If we were to use the standard toEqual matcher, our tests would fail because the test values would not match
|
||||||
|
// the responses exactly. To solve the problem, we define a new toEqualData Jasmine matcher.
|
||||||
|
// When the toEqualData matcher compares two objects, it takes only object properties into
|
||||||
|
// account and ignores methods.
|
||||||
|
beforeEach(function() {
|
||||||
|
jasmine.addMatchers({
|
||||||
|
toEqualData: function(util, customEqualityTesters) {
|
||||||
|
return {
|
||||||
|
compare: function(actual, expected) {
|
||||||
|
return {
|
||||||
|
pass: angular.equals(actual, expected)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
//Mock Users Service
|
||||||
|
beforeEach(module(function($provide) {
|
||||||
|
$provide.service('User', function($q) {
|
||||||
|
return {
|
||||||
|
getCurrent: function() {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
deferred.resolve( JSON.stringify(sampleUser) );
|
||||||
|
return deferred.promise;
|
||||||
|
},
|
||||||
|
login: function(credentials) {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
if( credentials.password === sampleUser.password && credentials.username === sampleUser.username){
|
||||||
|
deferred.resolve( JSON.stringify(sampleUser) );
|
||||||
|
}else {
|
||||||
|
deferred.resolve('Error: User could not be loggedin');
|
||||||
|
}
|
||||||
|
|
||||||
|
return deferred.promise;
|
||||||
|
},
|
||||||
|
logout: function() {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
deferred.resolve(null);
|
||||||
|
return deferred.promise;
|
||||||
|
},
|
||||||
|
signup: function(credentials) {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
if( credentials.password === sampleUser.password && credentials.username === sampleUser.username){
|
||||||
|
deferred.resolve( JSON.stringify(sampleUser) );
|
||||||
|
}else {
|
||||||
|
deferred.resolve('Error: User could not be signed up');
|
||||||
|
}
|
||||||
|
|
||||||
|
return deferred.promise;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
//Mock Authentication Service
|
||||||
|
beforeEach(module(function($provide) {
|
||||||
|
$provide.service('Auth', function() {
|
||||||
|
return {
|
||||||
|
ensureHasCurrentUser: function() {
|
||||||
|
return sampleUser;
|
||||||
|
},
|
||||||
|
isAuthenticated: function() {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
getUserState: function() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Load the main application module
|
||||||
|
beforeEach(module(ApplicationConfiguration.applicationModuleName));
|
||||||
|
|
||||||
|
beforeEach(module('stateMock'));
|
||||||
|
|
||||||
|
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
|
||||||
|
// This allows us to inject a service but then attach it to a variable
|
||||||
|
// with the same name as the service.
|
||||||
|
beforeEach(inject(function($controller, $rootScope, _$state_, _$location_, _$stateParams_, _$httpBackend_, CurrentForm, Forms) {
|
||||||
|
// Set a new global scope
|
||||||
|
scope = $rootScope.$new();
|
||||||
|
|
||||||
|
//Set CurrentForm
|
||||||
|
CurrentForm.setForm(sampleForm);
|
||||||
|
|
||||||
|
// Point global variables to injected services
|
||||||
|
$stateParams = _$stateParams_;
|
||||||
|
$httpBackend = _$httpBackend_;
|
||||||
|
$location = _$location_;
|
||||||
|
$state = _$state_;
|
||||||
|
|
||||||
|
$httpBackend.whenGET(/\.html$/).respond('');
|
||||||
|
$httpBackend.whenGET('/users/me/').respond('');
|
||||||
|
|
||||||
|
// Initialize the Forms controller.
|
||||||
|
createListFormsController = function(){
|
||||||
|
return $controller('ListFormsController', { $scope: scope });
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('$scope.findAll() should query all User\'s Forms', inject(function(Forms) {
|
||||||
|
|
||||||
|
var controller = createListFormsController();
|
||||||
|
|
||||||
|
// Set GET response
|
||||||
|
$httpBackend.expectGET(/^(\/forms)$/).respond(200, sampleFormList);
|
||||||
|
|
||||||
|
// Run controller functionality
|
||||||
|
scope.findAll();
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
// Test scope value
|
||||||
|
expect( scope.myforms ).toEqualData(sampleFormList);
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('$scope.duplicate() should duplicate a Form', inject(function(Forms) {
|
||||||
|
|
||||||
|
var dupSampleForm = sampleFormList[2],
|
||||||
|
dupSampleForm_index = 3,
|
||||||
|
newSampleFormList = sampleFormList;
|
||||||
|
dupSampleForm._id = 'a02df75b44c1d26b6a5e05b8';
|
||||||
|
newSampleFormList.splice(2, 0, dupSampleForm);
|
||||||
|
|
||||||
|
var controller = createListFormsController();
|
||||||
|
|
||||||
|
// Set GET response
|
||||||
|
$httpBackend.expectGET(/^(\/forms)$/).respond(200, sampleFormList);
|
||||||
|
|
||||||
|
// Run controller functionality
|
||||||
|
scope.findAll();
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
// Set GET response
|
||||||
|
$httpBackend.expect('POST', '/forms').respond(200, dupSampleForm);
|
||||||
|
|
||||||
|
// Run controller functionality
|
||||||
|
scope.duplicate(2);
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
// Test scope value
|
||||||
|
expect( scope.myforms.length ).toEqual(sampleFormList.length+1);
|
||||||
|
// expect( scope.myforms ).toEqualData(newSampleFormList);
|
||||||
|
expect( scope.myforms[dupSampleForm_index] ).toEqualData(dupSampleForm);
|
||||||
|
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('$scope.removeForm() should remove a Form', inject(function(Forms) {
|
||||||
|
|
||||||
|
var delIndex = 0,
|
||||||
|
delSampleForm = sampleFormList[delIndex],
|
||||||
|
delSampleFormList = sampleFormList;
|
||||||
|
delSampleFormList.splice(delIndex, 1);
|
||||||
|
|
||||||
|
var controller = createListFormsController();
|
||||||
|
|
||||||
|
// Set GET response
|
||||||
|
$httpBackend.expectGET(/^(\/forms)$/).respond(200, sampleFormList);
|
||||||
|
|
||||||
|
// Run controller functionality
|
||||||
|
scope.findAll();
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
// Set GET response
|
||||||
|
$httpBackend.expect('DELETE', /^(\/forms\/)([0-9a-fA-F]{24})$/).respond(200, delSampleForm);
|
||||||
|
|
||||||
|
// Run controller functionality
|
||||||
|
scope.removeForm(delIndex);
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
// Test scope value
|
||||||
|
expect( scope.myforms.length ).toEqual(sampleFormList.length-1);
|
||||||
|
for(var i=0; i<scope.myforms.length){
|
||||||
|
expect( scope.myforms[i] ).toEqualData(sampleFormList.slice(1,2)[i]);
|
||||||
|
}
|
||||||
|
expect( scope.myforms[0] ).not.toEqualData(delSampleForm);
|
||||||
|
|
||||||
|
}));
|
||||||
|
|
||||||
|
});
|
||||||
|
}());
|
30
public/modules/forms/tests/stateMock.js
Normal file
30
public/modules/forms/tests/stateMock.js
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
angular.module('stateMock',[]);
|
||||||
|
angular.module('stateMock').service("$state", function($q){
|
||||||
|
this.expectedTransitions = [];
|
||||||
|
this.transitionTo = function(stateName){
|
||||||
|
if(this.expectedTransitions.length > 0){
|
||||||
|
var expectedState = this.expectedTransitions.shift();
|
||||||
|
if(expectedState !== stateName){
|
||||||
|
throw Error("Expected transition to state: " + expectedState + " but transitioned to " + stateName );
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
throw Error("No more transitions were expected! Tried to transition to "+ stateName );
|
||||||
|
}
|
||||||
|
console.log("Mock transition to: " + stateName);
|
||||||
|
var deferred = $q.defer();
|
||||||
|
var promise = deferred.promise;
|
||||||
|
deferred.resolve();
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
this.go = this.transitionTo;
|
||||||
|
this.expectTransitionTo = function(stateName){
|
||||||
|
this.expectedTransitions.push(stateName);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
this.ensureAllTransitionsHappened = function(){
|
||||||
|
if(this.expectedTransitions.length > 0){
|
||||||
|
throw Error("Not all transitions happened!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
|
@ -19,7 +19,7 @@
|
||||||
<p>
|
<p>
|
||||||
<input type="text" data-ng-model="deleteConfirm" class="input-block" autofocus required aria-label="Type in the name of the form to confirm that you want to delete this form.">
|
<input type="text" data-ng-model="deleteConfirm" class="input-block" autofocus required aria-label="Type in the name of the form to confirm that you want to delete this form.">
|
||||||
</p>
|
</p>
|
||||||
<button type="submit" ng-click="remove()" class="btn btn-block btn-danger" ng-disabled="myform.title != deleteConfirm">
|
<button type="submit" ng-click="removeCurrentForm()" class="btn btn-block btn-danger" ng-disabled="myform.title != deleteConfirm">
|
||||||
I understand the consequences, delete this form
|
I understand the consequences, delete this form
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -276,7 +276,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-sm-offset-4 col-sm-2">
|
<div class="col-sm-offset-4 col-sm-2">
|
||||||
<button class="btn btn-primary btn-large" type="button" ng-click="update()"><i class="icon-arrow-left icon-white"></i> Save Changes</button>
|
<button class="btn btn-primary btn-large" type="button" ng-click="update(false, null)"><i class="icon-arrow-left icon-white"></i> Save Changes</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm-1">
|
<div class="col-sm-1">
|
||||||
<button class="btn btn-default" type="button" ng-click="resetForm()"><i class="icon-eye-open icon-white"></i> Cancel</button>
|
<button class="btn btn-default" type="button" ng-click="resetForm()"><i class="icon-eye-open icon-white"></i> Cancel</button>
|
||||||
|
|
|
@ -269,15 +269,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- <div class="row"><br><hr></div>
|
|
||||||
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-sm-4 col-sm-offset-4">
|
|
||||||
<button class="btn btn-primary btn-large" type="button" ng-click="update()">
|
|
||||||
<i class="icon-arrow-left icon-white"></i> Save Changes
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div> -->
|
|
||||||
</div>
|
</div>
|
||||||
</accordion-group>
|
</accordion-group>
|
||||||
|
|
||||||
|
|
|
@ -54,8 +54,8 @@
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<span class="pull-right">
|
<span class="pull-right">
|
||||||
<i style="cursor:pointer;" class="fa fa-trash-o" ng-click="removeFromList(form._id)"></i>
|
<i style="cursor:pointer;" class="fa fa-trash-o" ng-click="removeForm(form._id)"></i>
|
||||||
<i style="cursor:pointer;" class="fa fa-files-o" ng-click="duplicate(form)"></i>
|
<i style="cursor:pointer;" class="fa fa-files-o" ng-click="duplicate($index)"></i>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -24,7 +24,6 @@ angular.module('users').factory('User', ['$window', '$q', '$timeout', '$http', '
|
||||||
$http.post('/auth/signin', credentials).success(function(response) {
|
$http.post('/auth/signin', credentials).success(function(response) {
|
||||||
deferred.resolve(response);
|
deferred.resolve(response);
|
||||||
}).error(function(error) {
|
}).error(function(error) {
|
||||||
|
|
||||||
deferred.reject(error.message || error);
|
deferred.reject(error.message || error);
|
||||||
});
|
});
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
|
@ -79,7 +78,6 @@ angular.module('users').factory('User', ['$window', '$q', '$timeout', '$http', '
|
||||||
resetPassword: function(passwordDetails, token) {
|
resetPassword: function(passwordDetails, token) {
|
||||||
var deferred = $q.defer();
|
var deferred = $q.defer();
|
||||||
$http.get('/auth/password/'+token, passwordDetails).success(function(response) {
|
$http.get('/auth/password/'+token, passwordDetails).success(function(response) {
|
||||||
|
|
||||||
deferred.resolve();
|
deferred.resolve();
|
||||||
}).error(function(error) {
|
}).error(function(error) {
|
||||||
deferred.reject(error.message || error);
|
deferred.reject(error.message || error);
|
||||||
|
|
Loading…
Reference in a new issue