2015-08-25 21:33:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
// Forms Controller Spec
|
|
|
|
describe('EditForm Directive-Controller Tests', function() {
|
|
|
|
// Initialize global variables
|
|
|
|
var el, scope, controller, $httpBackend;
|
|
|
|
|
|
|
|
var sampleUser = {
|
|
|
|
firstName: 'Full',
|
|
|
|
lastName: 'Name',
|
|
|
|
email: 'test@test.com',
|
|
|
|
username: 'test@test.com',
|
|
|
|
password: 'password',
|
|
|
|
provider: 'local',
|
|
|
|
roles: ['user'],
|
2017-03-27 20:32:06 +00:00
|
|
|
_id: 'ed873933b1f1dea0ce12fab9'
|
2015-08-25 21:33:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var sampleForm = {
|
|
|
|
title: 'Form Title',
|
|
|
|
admin: 'ed873933b1f1dea0ce12fab9',
|
|
|
|
language: 'english',
|
|
|
|
form_fields: [
|
2015-11-23 19:19:02 +00:00
|
|
|
{fieldType:'textfield', title:'First Name', fieldOptions: [], fieldValue: '', required: true, disabled: false, deletePreserved: false, _id: 'ed873933b0ce121f1deafab9'},
|
|
|
|
{fieldType:'checkbox', title:'nascar', fieldOptions: [], fieldValue: '', required: true, disabled: false, deletePreserved: false, _id: 'ed83b0ce121f17393deafab9'},
|
|
|
|
{fieldType:'checkbox', title:'hockey', fieldOptions: [], fieldValue: '', required: true, disabled: false, deletePreserved: false, _id: 'ed8317393deab0ce121ffab9'}
|
2015-08-25 21:33:52 +00:00
|
|
|
],
|
|
|
|
pdf: {},
|
|
|
|
pdfFieldMap: {},
|
|
|
|
startPage: {
|
|
|
|
showStart: false,
|
2017-03-27 20:32:06 +00:00
|
|
|
buttons: []
|
2015-08-25 21:33:52 +00:00
|
|
|
},
|
|
|
|
hideFooter: false,
|
|
|
|
isGenerated: false,
|
|
|
|
isLive: false,
|
2017-03-27 20:32:06 +00:00
|
|
|
_id: '525a8422f6d0f87f0e407a33'
|
2015-08-25 21:33:52 +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)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Load the main application module
|
|
|
|
beforeEach(module(ApplicationConfiguration.applicationModuleName));
|
|
|
|
beforeEach(module('module-templates'));
|
2015-11-23 19:19:02 +00:00
|
|
|
beforeEach(module('stateMock'));
|
2015-08-25 21:33:52 +00:00
|
|
|
|
|
|
|
beforeEach(inject(function($compile, $controller, $rootScope, _$httpBackend_) {
|
|
|
|
//Instantiate directive.
|
|
|
|
var tmp_scope = $rootScope.$new();
|
|
|
|
tmp_scope.myform = _.cloneDeep(sampleForm);
|
|
|
|
|
|
|
|
//gotacha: Controller and link functions will execute.
|
2015-11-23 22:00:21 +00:00
|
|
|
el = angular.element('<edit-form-directive myform=\'myform\'></edit-form-directive>');
|
2015-08-25 21:33:52 +00:00
|
|
|
$compile(el)(tmp_scope);
|
|
|
|
$rootScope.$digest();
|
|
|
|
|
|
|
|
// Point global variables to injected services
|
|
|
|
$httpBackend = _$httpBackend_;
|
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
//$httpBackend.whenGET(/.+\.html$/).respond('');
|
2015-08-25 21:33:52 +00:00
|
|
|
$httpBackend.whenGET('/users/me/').respond('');
|
|
|
|
|
|
|
|
//Grab controller instance
|
|
|
|
controller = el.controller();
|
|
|
|
|
|
|
|
//Grab scope. Depends on type of scope.
|
|
|
|
//See angular.element documentation.
|
|
|
|
scope = el.isolateScope() || el.scope();
|
|
|
|
|
2017-03-27 20:32:06 +00:00
|
|
|
scope.update = function(updateImmediately, data, isDiffed, refreshAfterUpdate, cb){
|
|
|
|
if(cb){
|
|
|
|
cb();
|
|
|
|
}
|
2017-06-23 17:54:28 +00:00
|
|
|
};
|
2017-03-27 20:32:06 +00:00
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
}));
|
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
describe('> Form Field >',function(){
|
2015-08-25 21:33:52 +00:00
|
|
|
|
|
|
|
beforeEach(function(){
|
|
|
|
scope.myform = _.cloneDeep(sampleForm);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('$scope.addNewField() should ADD a new field to $scope.myform.form_fields', function() {
|
|
|
|
|
|
|
|
//Run controller methods
|
|
|
|
scope.addNewField(true, 'textfield');
|
|
|
|
|
|
|
|
var expectedFormField = {
|
2017-03-27 20:32:06 +00:00
|
|
|
title: 'Short Text2',
|
|
|
|
fieldType: 'textfield',
|
|
|
|
fieldValue: '',
|
|
|
|
required: true,
|
|
|
|
disabled: false,
|
|
|
|
deletePreserved: false,
|
|
|
|
logicJump: Object({ })
|
|
|
|
};
|
|
|
|
|
|
|
|
var actualFormField = _.cloneDeep(_.last(scope.myform.form_fields));
|
2015-08-25 21:33:52 +00:00
|
|
|
delete actualFormField._id;
|
|
|
|
|
|
|
|
expect(scope.myform.form_fields.length).toEqual(sampleForm.form_fields.length+1);
|
|
|
|
expect(actualFormField).toEqualData(expectedFormField);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('$scope.deleteField() should DELETE a field to $scope.myform.form_fields', function() {
|
2017-03-27 20:32:06 +00:00
|
|
|
|
|
|
|
spyOn(scope, 'update');
|
|
|
|
|
|
|
|
//Run controller methods
|
2015-08-25 21:33:52 +00:00
|
|
|
scope.deleteField(0);
|
|
|
|
|
2017-03-27 20:32:06 +00:00
|
|
|
expect(scope.update).toHaveBeenCalled();
|
2015-08-25 21:33:52 +00:00
|
|
|
expect(scope.myform.form_fields.length).toEqual(sampleForm.form_fields.length-1);
|
|
|
|
expect(_.first(scope.myform.form_fields)).toEqualData(sampleForm.form_fields[1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('$scope.duplicateField() should DUPLICATE a field and update $scope.myform.form_fields', function() {
|
2017-03-27 20:32:06 +00:00
|
|
|
|
|
|
|
spyOn(scope, 'update');
|
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
//Run controller methods
|
|
|
|
scope.duplicateField(0);
|
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
var originalField = _.cloneDeep(scope.myform.form_fields[0]);
|
|
|
|
originalField.title += ' copy';
|
|
|
|
|
|
|
|
delete originalField._id;
|
|
|
|
var copyField = _.cloneDeep(scope.myform.form_fields[1]);
|
|
|
|
delete copyField._id;
|
|
|
|
|
2017-03-27 20:32:06 +00:00
|
|
|
expect(scope.update).toHaveBeenCalled();
|
2015-08-25 21:33:52 +00:00
|
|
|
expect(scope.myform.form_fields.length).toEqual(sampleForm.form_fields.length+1);
|
2015-11-23 19:19:02 +00:00
|
|
|
expect(originalField).toEqualData(copyField);
|
2015-08-25 21:33:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2017-03-27 20:32:06 +00:00
|
|
|
/*
|
2015-11-23 19:19:02 +00:00
|
|
|
describe('> Form Field Button >',function(){
|
2015-08-25 21:33:52 +00:00
|
|
|
|
|
|
|
it('$scope.addButton() should ADD a button to $scope.myform.startPage.buttons', function() {
|
2017-03-27 20:32:06 +00:00
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
var expectedStartPageBtn = {
|
2017-03-27 20:32:06 +00:00
|
|
|
bgColor:'#ddd',
|
|
|
|
color:'#ffffff',
|
2015-08-25 21:33:52 +00:00
|
|
|
text: 'Button'
|
|
|
|
};
|
|
|
|
|
|
|
|
//Run controller methods
|
|
|
|
scope.addButton();
|
|
|
|
var actualStartPageBtn = _.cloneDeep(_.last(scope.myform.startPage.buttons));
|
|
|
|
delete actualStartPageBtn._id;
|
|
|
|
|
|
|
|
expect(scope.myform.startPage.buttons.length).toEqual(sampleForm.startPage.buttons.length+1);
|
|
|
|
expect(actualStartPageBtn).toEqualData(expectedStartPageBtn);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('$scope.deleteButton() should DELETE a button from $scope.myform.startPage.buttons', function() {
|
|
|
|
//Run controller methods
|
|
|
|
scope.deleteButton(scope.myform.startPage.buttons[0]);
|
2017-03-27 20:32:06 +00:00
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
expect(scope.myform.startPage.buttons.length).toEqual(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
describe('> Form Field Option >',function(){
|
2015-08-25 21:33:52 +00:00
|
|
|
it('$scope.addOption() should ADD a new option to a field.fieldOptions', function() {
|
2015-11-23 19:19:02 +00:00
|
|
|
var originalOptionLen = scope.myform.form_fields[1].fieldOptions.length;
|
2015-08-25 21:33:52 +00:00
|
|
|
|
|
|
|
//Run controller methods
|
2015-11-23 19:19:02 +00:00
|
|
|
scope.addOption(1);
|
2015-08-25 21:33:52 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
expect(originalOptionLen+1).toEqual(scope.myform.form_fields[1].fieldOptions.length);
|
|
|
|
expect(scope.myform.form_fields[1].fieldOptions[0].option_title).toEqualData('Option 0');
|
|
|
|
expect(scope.myform.form_fields[1].fieldOptions[0].option_value).toEqualData('Option 0');
|
2015-08-25 21:33:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('$scope.deleteOption() should DELETE remove option from field.fieldOptions', function() {
|
2015-11-23 19:19:02 +00:00
|
|
|
//Run controller methods
|
|
|
|
scope.deleteOption(1, scope.myform.form_fields[1].fieldOptions[0]);
|
2015-08-25 21:33:52 +00:00
|
|
|
|
|
|
|
expect(scope.myform.form_fields[0].fieldOptions.length).toEqual(0);
|
|
|
|
expect(scope.myform.form_fields[0].fieldOptions[0]).not.toBeDefined();
|
|
|
|
});
|
2017-03-27 20:32:06 +00:00
|
|
|
});*/
|
2015-08-25 21:33:52 +00:00
|
|
|
});
|
2017-03-27 20:32:06 +00:00
|
|
|
}());
|