2015-08-18 21:44:36 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
// Forms Controller Spec
|
2015-08-21 00:17:14 +00:00
|
|
|
describe('AdminForm Controller Tests', function() {
|
2015-08-18 21:44:36 +00:00
|
|
|
// Initialize global variables
|
|
|
|
var AdminFormController,
|
2015-08-19 22:29:01 +00:00
|
|
|
createAdminFormController,
|
2015-08-18 21:44:36 +00:00
|
|
|
scope,
|
|
|
|
$httpBackend,
|
|
|
|
$stateParams,
|
2015-08-19 22:29:01 +00:00
|
|
|
$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: [
|
2015-11-13 18:47:59 +00:00
|
|
|
{fieldType:'textfield', title:'First Name', fieldValue: '', deletePreserved: false, _id:'56340745f59a6fc9e22028e9'},
|
|
|
|
{fieldType:'checkbox', title:'nascar', fieldValue: '', deletePreserved: false, _id:'5c9e22028e907634f45f59a6'},
|
|
|
|
{fieldType:'checkbox', title:'hockey', fieldValue: '', deletePreserved: false, _id:'56e90745f5934fc9e22028a6'}
|
2015-08-19 22:29:01 +00:00
|
|
|
],
|
2017-03-27 20:32:06 +00:00
|
|
|
analytics: {
|
|
|
|
visitors: []
|
|
|
|
},
|
|
|
|
submissions: [],
|
|
|
|
_id: '525a8422f6d0f87f0e407a33',
|
|
|
|
id: '525a8422f6d0f87f0e407a33'
|
2015-08-19 22:29:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var expectedForm = {
|
|
|
|
title: 'Form Title',
|
|
|
|
admin: 'ed873933b1f1dea0ce12fab9',
|
|
|
|
language: 'english',
|
|
|
|
form_fields: [
|
2015-11-13 18:47:59 +00:00
|
|
|
{fieldType:'textfield', title:'First Name', fieldValue: '', deletePreserved: false, _id:'56340745f59a6fc9e22028e9'},
|
|
|
|
{fieldType:'checkbox', title:'nascar', fieldValue: '', deletePreserved: false, _id:'5c9e22028e907634f45f59a6'},
|
|
|
|
{fieldType:'checkbox', title:'hockey', fieldValue: '', deletePreserved: false, _id:'56e90745f5934fc9e22028a6'}
|
2015-08-19 22:29:01 +00:00
|
|
|
],
|
|
|
|
visible_form_fields: [
|
2015-11-13 18:47:59 +00:00
|
|
|
{fieldType:'textfield', title:'First Name', fieldValue: '', deletePreserved: false, _id:'56340745f59a6fc9e22028e9'},
|
|
|
|
{fieldType:'checkbox', title:'nascar', fieldValue: '', deletePreserved: false, _id:'5c9e22028e907634f45f59a6'},
|
|
|
|
{fieldType:'checkbox', title:'hockey', fieldValue: '', deletePreserved: false, _id:'56e90745f5934fc9e22028a6'}
|
2015-08-19 22:29:01 +00:00
|
|
|
],
|
|
|
|
_id: '525a8422f6d0f87f0e407a33'
|
|
|
|
};
|
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
var newFakeModal = function(){
|
|
|
|
var result = {
|
|
|
|
opened: true,
|
|
|
|
result: {
|
|
|
|
then: 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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
close: function( item ) {
|
|
|
|
//The user clicked OK on the modal dialog, call the stored confirm callback with the selected item
|
|
|
|
this.opened = false;
|
|
|
|
this.result.confirmCallBack( item );
|
|
|
|
},
|
|
|
|
dismiss: function( type ) {
|
|
|
|
//The user clicked cancel on the modal dialog, call the stored cancel callback
|
|
|
|
this.opened = false;
|
|
|
|
this.result.cancelCallback( type );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return result;
|
2015-08-21 00:17:14 +00:00
|
|
|
};
|
2017-03-27 20:32:06 +00:00
|
|
|
|
2015-10-30 18:40:02 +00:00
|
|
|
//Mock Users Service
|
|
|
|
beforeEach(module(function($provide) {
|
|
|
|
$provide.service('myForm', function($q) {
|
|
|
|
return sampleForm;
|
|
|
|
});
|
|
|
|
}));
|
2015-08-19 22:29:01 +00:00
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-08-21 00:17:14 +00:00
|
|
|
// Load the main application module
|
|
|
|
beforeEach(module(ApplicationConfiguration.applicationModuleName));
|
|
|
|
|
|
|
|
beforeEach(module('stateMock'));
|
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
//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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
|
|
|
|
//Mock $uibModal
|
2015-11-13 18:47:59 +00:00
|
|
|
beforeEach(inject(function($uibModal) {
|
2015-11-23 19:19:02 +00:00
|
|
|
var modal = newFakeModal();
|
|
|
|
spyOn($uibModal, 'open').and.returnValue(modal);
|
|
|
|
//spyOn($uibModal, 'close').and.callFake(modal.close());
|
2015-08-19 22:29:01 +00:00
|
|
|
}));
|
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
// 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.
|
2015-08-19 22:29:01 +00:00
|
|
|
beforeEach(inject(function($controller, $rootScope, _$state_, _$location_, _$stateParams_, _$httpBackend_, CurrentForm, Forms) {
|
2015-08-18 21:44:36 +00:00
|
|
|
// Set a new global scope
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
//Set CurrentForm
|
|
|
|
CurrentForm.setForm(sampleForm);
|
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
// Point global variables to injected services
|
|
|
|
$stateParams = _$stateParams_;
|
|
|
|
$httpBackend = _$httpBackend_;
|
|
|
|
$location = _$location_;
|
2015-08-19 22:29:01 +00:00
|
|
|
$state = _$state_;
|
|
|
|
|
|
|
|
$httpBackend.whenGET(/\.html$/).respond('');
|
|
|
|
$httpBackend.whenGET('/users/me/').respond('');
|
2015-08-18 21:44:36 +00:00
|
|
|
|
|
|
|
// Initialize the Forms controller.
|
2015-08-19 22:29:01 +00:00
|
|
|
createAdminFormController = function(){
|
|
|
|
return $controller('AdminFormController', { $scope: scope });
|
|
|
|
};
|
|
|
|
}));
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
it('AdminFormController should fetch current Form when instantiated', function() {
|
2015-08-19 22:29:01 +00:00
|
|
|
// Run controller functionality
|
|
|
|
var controller = createAdminFormController();
|
|
|
|
|
|
|
|
// Test scope value
|
|
|
|
expect(scope.myform).toEqualData(sampleForm);
|
2015-08-25 21:33:52 +00:00
|
|
|
});
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
it('$scope.removeCurrentForm() with valid form data should send a DELETE request with the id of form', function() {
|
|
|
|
var controller = createAdminFormController();
|
|
|
|
|
2017-03-27 20:32:06 +00:00
|
|
|
//Set $state transition
|
2015-08-19 22:29:01 +00:00
|
|
|
$state.expectTransitionTo('listForms');
|
|
|
|
|
|
|
|
// Set DELETE response
|
|
|
|
$httpBackend.expect('DELETE', /^(\/forms\/)([0-9a-fA-F]{24})$/).respond(200, sampleForm);
|
|
|
|
|
|
|
|
//Run controller functionality
|
|
|
|
scope.openDeleteModal();
|
|
|
|
scope.removeCurrentForm();
|
2017-03-27 20:32:06 +00:00
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
$httpBackend.flush();
|
2015-08-21 00:17:14 +00:00
|
|
|
$state.ensureAllTransitionsHappened();
|
2015-08-19 22:29:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('$scope.update() should send a PUT request with the id of form', function() {
|
|
|
|
var controller = createAdminFormController();
|
|
|
|
|
|
|
|
//Set PUT response
|
|
|
|
$httpBackend.expect('PUT', /^(\/forms\/)([0-9a-fA-F]{24})$/).respond(200, sampleForm);
|
|
|
|
|
|
|
|
//Run controller functionality
|
2017-03-27 20:32:06 +00:00
|
|
|
scope.update(false, sampleForm, false, false);
|
2015-08-19 22:29:01 +00:00
|
|
|
|
|
|
|
$httpBackend.flush();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('$scope.openDeleteModal() should open scope.deleteModal', function() {
|
|
|
|
var controller = createAdminFormController();
|
|
|
|
|
|
|
|
//Run controller functionality
|
|
|
|
scope.openDeleteModal();
|
2015-11-23 19:19:02 +00:00
|
|
|
expect(scope.deleteModal.opened).toEqual(true);
|
2015-08-19 22:29:01 +00:00
|
|
|
});
|
|
|
|
|
2015-11-13 18:47:59 +00:00
|
|
|
it('$scope.cancelDeleteModal() should close $scope.deleteModal', inject(function($uibModal) {
|
2015-08-19 22:29:01 +00:00
|
|
|
var controller = createAdminFormController();
|
|
|
|
|
|
|
|
//Run controller functionality
|
|
|
|
scope.openDeleteModal();
|
|
|
|
|
2015-08-21 00:17:14 +00:00
|
|
|
//Run controller functionality
|
2015-08-19 22:29:01 +00:00
|
|
|
scope.cancelDeleteModal();
|
|
|
|
expect( scope.deleteModal.opened ).toEqual(false);
|
|
|
|
}));
|
2015-08-18 21:44:36 +00:00
|
|
|
});
|
2017-03-27 20:32:06 +00:00
|
|
|
}());
|