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-08-21 00:17:14 +00:00
|
|
|
{fieldType:'textfield', title:'First Name', fieldValue: '', deletePreserved: false},
|
|
|
|
{fieldType:'checkbox', title:'nascar', fieldValue: '', deletePreserved: false},
|
|
|
|
{fieldType:'checkbox', title:'hockey', fieldValue: '', deletePreserved: false}
|
2015-08-19 22:29:01 +00:00
|
|
|
],
|
|
|
|
_id: '525a8422f6d0f87f0e407a33'
|
|
|
|
};
|
|
|
|
|
|
|
|
var expectedForm = {
|
|
|
|
title: 'Form Title',
|
|
|
|
admin: 'ed873933b1f1dea0ce12fab9',
|
|
|
|
language: 'english',
|
|
|
|
form_fields: [
|
2015-08-21 00:17:14 +00:00
|
|
|
{fieldType:'textfield', title:'First Name', fieldValue: '', deletePreserved: false},
|
|
|
|
{fieldType:'checkbox', title:'nascar', fieldValue: '', deletePreserved: false},
|
|
|
|
{fieldType:'checkbox', title:'hockey', fieldValue: '', deletePreserved: false}
|
2015-08-19 22:29:01 +00:00
|
|
|
],
|
|
|
|
visible_form_fields: [
|
2015-08-21 00:17:14 +00:00
|
|
|
{fieldType:'textfield', title:'First Name', fieldValue: '', deletePreserved: false},
|
|
|
|
{fieldType:'checkbox', title:'nascar', fieldValue: '', deletePreserved: false},
|
|
|
|
{fieldType:'checkbox', title:'hockey', fieldValue: '', deletePreserved: false}
|
2015-08-19 22:29:01 +00:00
|
|
|
],
|
|
|
|
_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 );
|
|
|
|
};
|
2015-08-21 00:17:14 +00:00
|
|
|
};
|
2015-08-19 22:29:01 +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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
beforeEach(inject(function($modal) {
|
|
|
|
spyOn($modal, 'open').and.returnValue(new fakeModal());
|
|
|
|
}));
|
|
|
|
|
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
|
|
|
|
|
|
|
it('$scope.findOne() should fetch current Form', inject(function(Forms) {
|
2015-08-19 22:29:01 +00:00
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
// Define a sample article object
|
2015-08-19 22:29:01 +00:00
|
|
|
var expectedFormObj = new Forms(expectedForm);
|
|
|
|
|
|
|
|
var controller = createAdminFormController();
|
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
// Set the URL parameter
|
2015-08-19 22:29:01 +00:00
|
|
|
$stateParams.formId = expectedForm._id;
|
2015-08-18 21:44:36 +00:00
|
|
|
|
|
|
|
// Set GET response
|
2015-08-19 22:29:01 +00:00
|
|
|
$httpBackend.expectGET(/^(\/forms\/)([0-9a-fA-F]{24})$/).respond(200, sampleForm);
|
2015-08-18 21:44:36 +00:00
|
|
|
|
|
|
|
// Run controller functionality
|
|
|
|
scope.findOne();
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
// Test scope value
|
2015-09-15 22:21:49 +00:00
|
|
|
expect( scope.myform.toJSON() ).toEqualData(expectedFormObj);
|
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();
|
|
|
|
|
2015-08-20 18:47:25 +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.deleteModal.result(function(selectedItem){
|
2015-08-21 00:17:14 +00:00
|
|
|
this.selected = selectedItem;
|
2015-08-19 22:29:01 +00:00
|
|
|
}, function(type){
|
2015-08-21 00:17:14 +00:00
|
|
|
this.canceled = true;
|
2015-08-19 22:29:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
scope.removeCurrentForm();
|
|
|
|
|
|
|
|
$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
|
|
|
|
scope.update(false, null);
|
|
|
|
|
|
|
|
$httpBackend.flush();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('$scope.openDeleteModal() should open scope.deleteModal', function() {
|
|
|
|
var controller = createAdminFormController();
|
|
|
|
|
|
|
|
//Run controller functionality
|
|
|
|
scope.openDeleteModal();
|
|
|
|
expect( scope.deleteModal.opened ).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('$scope.cancelDeleteModal() should close scope.deleteModal', inject(function($modal) {
|
|
|
|
var controller = createAdminFormController();
|
|
|
|
|
|
|
|
//Run controller functionality
|
|
|
|
scope.openDeleteModal();
|
|
|
|
console.log(scope.deleteModal.opened);
|
|
|
|
|
|
|
|
scope.deleteModal.result(function(selectedItem){
|
|
|
|
this.selected = selectedItem;
|
|
|
|
}, function(type){
|
|
|
|
this.canceled = true;
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
expect( scope.deleteModal.canceled ).toEqual(true);
|
|
|
|
|
|
|
|
}));
|
2015-08-18 21:44:36 +00:00
|
|
|
});
|
|
|
|
}());
|