fixed form field preservation logic
This commit is contained in:
parent
ed032d6db9
commit
5a18735b4c
|
@ -280,22 +280,6 @@ FormSchema.plugin(mUtilities.timestamp, {
|
|||
useVirtual: false
|
||||
});
|
||||
|
||||
var _original;
|
||||
|
||||
function getDeletedIndexes(needle, haystack){
|
||||
var deletedIndexes = [];
|
||||
|
||||
if(haystack.length > 0){
|
||||
for(var i = 0; i < needle.length; i++){
|
||||
if(haystack.indexOf(needle[i]) === -1){
|
||||
deletedIndexes.push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return deletedIndexes;
|
||||
}
|
||||
|
||||
|
||||
FormSchema.pre('save', function (next) {
|
||||
switch(this.language){
|
||||
case 'spanish':
|
||||
|
@ -317,14 +301,39 @@ FormSchema.pre('save', function (next) {
|
|||
next();
|
||||
});
|
||||
|
||||
function getDeletedIndexes(needle, haystack){
|
||||
var deletedIndexes = [];
|
||||
|
||||
if(haystack.length > 0){
|
||||
for(var i = 0; i < needle.length; i++){
|
||||
if(haystack.indexOf(needle[i]) === -1){
|
||||
deletedIndexes.push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return deletedIndexes;
|
||||
}
|
||||
|
||||
function formFieldsAllHaveIds(form_fields){
|
||||
for(var i=0; i<form_fields.length; i++){
|
||||
if(!form_fields[i].hasOwnProperty('_id') && !form_fields[i].hasOwnProperty('globalId')){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
FormSchema.pre('save', function (next) {
|
||||
var that = this;
|
||||
var _original;
|
||||
|
||||
async.series([function(cb) {
|
||||
that.constructor
|
||||
.findOne({_id: that._id}).exec(function (err, original) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
} else if (!original){
|
||||
return next();
|
||||
} else {
|
||||
_original = original;
|
||||
return cb(null);
|
||||
|
@ -332,21 +341,18 @@ FormSchema.pre('save', function (next) {
|
|||
});
|
||||
},
|
||||
function(cb) {
|
||||
var hasIds = true;
|
||||
for(var i=0; i<that.form_fields.length; i++){
|
||||
if(!that.form_fields.hasOwnProperty('_id')){
|
||||
hasIds = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(that.isModified('form_fields') && that.form_fields && _original && hasIds){
|
||||
if(that.form_fields && that.isModified('form_fields') && formFieldsAllHaveIds(that.toObject().form_fields)){
|
||||
|
||||
var old_form_fields = _original.form_fields,
|
||||
new_ids = _.map(_.pluck(that.form_fields, 'id'), function(id){ return ''+id;}),
|
||||
old_ids = _.map(_.pluck(old_form_fields, 'id'), function(id){ return ''+id;}),
|
||||
var current_form = that.toObject(),
|
||||
old_form_fields = _original.toObject().form_fields,
|
||||
new_ids = _.map(_.map(current_form.form_fields, 'globalId'), function(id){ return ''+id;}),
|
||||
old_ids = _.map(_.map(old_form_fields, 'globalId'), function(id){ return ''+id;}),
|
||||
deletedIds = getDeletedIndexes(old_ids, new_ids);
|
||||
|
||||
//Preserve fields that have at least one submission
|
||||
console.log(deletedIds);
|
||||
console.log(new_ids);
|
||||
console.log(old_ids);
|
||||
//Check if any form_fileds were deleted
|
||||
if( deletedIds.length > 0 ){
|
||||
|
||||
var modifiedSubmissions = [];
|
||||
|
@ -355,23 +361,21 @@ FormSchema.pre('save', function (next) {
|
|||
function (deletedIdIndex, key, cb_id) {
|
||||
|
||||
var deleted_id = old_ids[deletedIdIndex];
|
||||
|
||||
//Find FormSubmissions that contain field with _id equal to 'deleted_id'
|
||||
FormSubmission.
|
||||
find({ form: that._id, admin: that.admin, form_fields: {$elemMatch: {submissionId: deleted_id} } }).
|
||||
find({ form: that, form_fields: {$elemMatch: {globalId: deleted_id} } }).
|
||||
exec(function(err, submissions){
|
||||
if(err) {
|
||||
return cb_id(err);
|
||||
}
|
||||
|
||||
//Delete field if there are no submission(s) found
|
||||
}
|
||||
|
||||
//Preserve fields that have at least one submission
|
||||
if (submissions.length) {
|
||||
//Add submissions
|
||||
modifiedSubmissions.push.apply(modifiedSubmissions, submissions);
|
||||
}
|
||||
|
||||
return cb_id(null);
|
||||
|
||||
});
|
||||
},
|
||||
function (err) {
|
||||
|
@ -383,29 +387,33 @@ FormSchema.pre('save', function (next) {
|
|||
//Iterate through all submissions with modified form_fields
|
||||
async.forEachOfSeries(modifiedSubmissions, function (submission, key, callback) {
|
||||
|
||||
//Iterate through ids of deleted fields
|
||||
for (i = 0; i < deletedIds.length; i++) {
|
||||
var submission_form_fields = submission.toObject().form_fields;
|
||||
var currentform_form_fields = that.toObject().form_fields;
|
||||
|
||||
var index = _.findIndex(submission.form_fields, function (field) {
|
||||
var tmp_id = field._id + '';
|
||||
//Iterate through ids of deleted fields
|
||||
for (var i = 0; i < deletedIds.length; i++) {
|
||||
var index = _.findIndex(submission_form_fields, function (field) {
|
||||
var tmp_id = field.globalId + '';
|
||||
return tmp_id === old_ids[deletedIds[i]];
|
||||
});
|
||||
|
||||
var deletedField = submission.form_fields[index];
|
||||
var deletedField = submission_form_fields[index];
|
||||
|
||||
//Hide field if it exists
|
||||
if (deletedField) {
|
||||
|
||||
//Delete old form_field
|
||||
submission.form_fields.splice(index, 1);
|
||||
submission_form_fields.splice(index, 1);
|
||||
|
||||
deletedField.deletePreserved = true;
|
||||
|
||||
//Move deleted form_field to start
|
||||
submission.form_fields.unshift(deletedField);
|
||||
that.form_fields.unshift(deletedField);
|
||||
submission_form_fields.unshift(deletedField);
|
||||
currentform_form_fields.unshift(deletedField);
|
||||
}
|
||||
}
|
||||
submission.form_fields = submission_form_fields;
|
||||
that.form_fields = currentform_form_fields;
|
||||
|
||||
submission.save(function (saveErr) {
|
||||
return callback(saveErr);
|
||||
|
@ -413,13 +421,14 @@ FormSchema.pre('save', function (next) {
|
|||
}, function (err) {
|
||||
return cb(err);
|
||||
});
|
||||
|
||||
}
|
||||
);
|
||||
} else {
|
||||
return cb(null);
|
||||
}
|
||||
} else {
|
||||
return cb(null);
|
||||
}
|
||||
return cb(null);
|
||||
}],
|
||||
function(err, results){
|
||||
next(err);
|
||||
|
|
|
@ -10,6 +10,9 @@ var mongoose = require('mongoose'),
|
|||
Schema = mongoose.Schema,
|
||||
LogicJumpSchema = require('./logic_jump.server.model');
|
||||
|
||||
const UIDGenerator = require('uid-generator');
|
||||
const uidgen3 = new UIDGenerator(256, UIDGenerator.BASE62);
|
||||
|
||||
var FieldOptionSchema = new Schema({
|
||||
option_id: {
|
||||
type: Number
|
||||
|
@ -61,6 +64,9 @@ function BaseFieldSchema(){
|
|||
Schema.apply(this, arguments);
|
||||
|
||||
this.add({
|
||||
globalId: {
|
||||
type: String,
|
||||
},
|
||||
isSubmission: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
|
@ -193,6 +199,9 @@ FormFieldSchema.pre('save', function(next) {
|
|||
if(this.logicJump && this.logicJump.fieldA) {
|
||||
if(this.logicJump.jumpTo === '') delete this.logicJump.jumpTo;
|
||||
}
|
||||
if(!this.globalId){
|
||||
this.globalId = uidgen3.generateSync()
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
|
|
|
@ -149,7 +149,6 @@ describe('FormSubmission Model Unit Tests:', function() {
|
|||
mySubmission.save(function(err, submission) {
|
||||
should.not.exist(err);
|
||||
should.exist(submission);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -187,7 +186,7 @@ describe('FormSubmission Model Unit Tests:', function() {
|
|||
|
||||
//Create Submission
|
||||
mySubmission = new FormSubmission({
|
||||
form_fields: sampleSubmission,
|
||||
form_fields: _.merge(sampleSubmission, myForm.form_fields),
|
||||
admin: user,
|
||||
form: myForm,
|
||||
timeElapsed: 17.55
|
||||
|
@ -200,7 +199,6 @@ describe('FormSubmission Model Unit Tests:', function() {
|
|||
|
||||
});
|
||||
|
||||
/*
|
||||
it('should preserve deleted form_fields that have submissions without any problems', function(done) {
|
||||
|
||||
var old_fields = myForm.toObject().form_fields;
|
||||
|
@ -214,17 +212,18 @@ describe('FormSubmission Model Unit Tests:', function() {
|
|||
should.not.exist(err);
|
||||
should.exist(_form.form_fields);
|
||||
|
||||
var actual_fields = _.deepOmit(_form.toObject().form_fields, ['lastModified', 'created', '_id']);
|
||||
old_fields = _.deepOmit(old_fields, ['lastModified', 'created', '_id']);
|
||||
var actual_fields = _.deepOmit(_form.toObject().form_fields, ['deletePreserved', 'globalId', 'lastModified', 'created', '_id', 'submissionId']);
|
||||
old_fields = _.deepOmit(old_fields, ['deletePreserved', 'globalId', 'lastModified', 'created', '_id', 'submissionId']);
|
||||
|
||||
should.deepEqual(JSON.stringify(actual_fields), JSON.stringify(old_fields), 'old form_fields not equal to newly saved form_fields');
|
||||
should.deepEqual(actual_fields, old_fields, 'old form_fields not equal to newly saved form_fields');
|
||||
done();
|
||||
});
|
||||
});
|
||||
//
|
||||
|
||||
it('should delete \'preserved\' form_fields whose submissions have been removed without any problems', function(done) {
|
||||
|
||||
var old_fields = myForm.toObject().form_fields;
|
||||
old_fields.splice(0,1);
|
||||
var new_form_fields = _.clone(myForm.toObject().form_fields);
|
||||
new_form_fields.splice(0, 1);
|
||||
|
||||
|
@ -240,16 +239,9 @@ describe('FormSubmission Model Unit Tests:', function() {
|
|||
|
||||
should.deepEqual(JSON.stringify(actual_fields), JSON.stringify(old_fields)); //'old form_fields not equal to newly saved form_fields');
|
||||
done();
|
||||
|
||||
// //Remove submission
|
||||
// mySubmission.remove(function(err){
|
||||
// myForm.submissions.should.have.length(0);
|
||||
// myForm.form_fields.should.not.containDeep(old_fields[0]);
|
||||
// done();
|
||||
// });
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
||||
afterEach(function(done){
|
||||
mySubmission.remove(function(){
|
||||
done();
|
||||
|
|
|
@ -210,7 +210,8 @@ module.exports = function(grunt) {
|
|||
reporter: 'spec',
|
||||
quiet: false,
|
||||
require: 'server.js',
|
||||
ui: 'bdd'
|
||||
ui: 'bdd',
|
||||
debug: true
|
||||
}
|
||||
},
|
||||
karma: {
|
||||
|
|
|
@ -95,6 +95,7 @@
|
|||
"socket.io": "^1.4.6",
|
||||
"socket.io-redis": "^1.0.0",
|
||||
"swig": "~1.4.1",
|
||||
"uid-generator": "^0.1.1",
|
||||
"uuid-token-generator": "^0.5.0",
|
||||
"wildcard-subdomains": "github:tellform/wildcard-subdomains",
|
||||
"winston": "^2.3.1",
|
||||
|
|
|
@ -86,9 +86,7 @@
|
|||
scope = el.isolateScope() || el.scope();
|
||||
|
||||
scope.update = function(updateImmediately, data, isDiffed, refreshAfterUpdate, cb){
|
||||
if(cb){
|
||||
cb();
|
||||
}
|
||||
if(cb) cb();
|
||||
};
|
||||
|
||||
}));
|
||||
|
@ -153,53 +151,5 @@
|
|||
});
|
||||
|
||||
});
|
||||
/*
|
||||
describe('> Form Field Button >',function(){
|
||||
|
||||
it('$scope.addButton() should ADD a button to $scope.myform.startPage.buttons', function() {
|
||||
|
||||
var expectedStartPageBtn = {
|
||||
bgColor:'#ddd',
|
||||
color:'#ffffff',
|
||||
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]);
|
||||
|
||||
expect(scope.myform.startPage.buttons.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('> Form Field Option >',function(){
|
||||
it('$scope.addOption() should ADD a new option to a field.fieldOptions', function() {
|
||||
var originalOptionLen = scope.myform.form_fields[1].fieldOptions.length;
|
||||
|
||||
//Run controller methods
|
||||
scope.addOption(1);
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
it('$scope.deleteOption() should DELETE remove option from field.fieldOptions', function() {
|
||||
//Run controller methods
|
||||
scope.deleteOption(1, scope.myform.form_fields[1].fieldOptions[0]);
|
||||
|
||||
expect(scope.myform.form_fields[0].fieldOptions.length).toEqual(0);
|
||||
expect(scope.myform.form_fields[0].fieldOptions[0]).not.toBeDefined();
|
||||
});
|
||||
});*/
|
||||
});
|
||||
}());
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
|
||||
it('should be able render all field-icon types', inject(function($compile) {
|
||||
var currType, currClass;
|
||||
|
||||
|
||||
for(var i=0; i<FormFields.types.length; i++){
|
||||
currType = FormFields.types[i];
|
||||
currClass = faClasses[currType.name];
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
}));
|
||||
|
||||
it('should emit Custom "Finished" and "Started" events on ng-repeat', inject(function($compile, $rootScope) {
|
||||
|
||||
scope.myfields = FormFields.types;
|
||||
|
||||
$compile('<div><div ng-repeat="item in myfields" on-finish-render="editFormFields">{{item.name}}</div></div>')(scope);
|
||||
|
@ -30,8 +29,6 @@
|
|||
}));
|
||||
|
||||
it('should emit "ngRepeat Finished" and "ngRepeat Started" events on ng-repeat when attr is not set to string', inject(function($compile, $rootScope) {
|
||||
|
||||
// console.log(FormFields.types);
|
||||
scope.myfields = FormFields.types;
|
||||
|
||||
$compile('<div><div ng-repeat="item in myfields" on-finish-render>{{item.name}}</div></div>')(scope);
|
||||
|
|
|
@ -169,7 +169,6 @@
|
|||
expect(scope.myform.submitted).toBe(true);
|
||||
expect(scope.error).toEqual('');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('$scope.reloadForm() should reset and reload form', function(){
|
||||
|
|
|
@ -1,170 +0,0 @@
|
|||
// 'use strict';
|
||||
|
||||
// (function() {
|
||||
|
||||
|
||||
// // Principal controller Spec for E2E Tests
|
||||
// describe('AuthenticationController E2E Tests', function() {
|
||||
|
||||
// describe('/signup should work for a unique username', function() {
|
||||
// beforeEach(function() {
|
||||
// var ptor = protractor.getInstance();
|
||||
// ptor.get('http://localhost:3000/#!/signup');
|
||||
// });
|
||||
|
||||
// it('should show the signup panel on page load', function() {
|
||||
// expect($('section > section.row.auth > .col-md-12.text-center')).toEqual('Signup with your email');
|
||||
// });
|
||||
|
||||
|
||||
// //Jasmine it statement : What "it" will do.
|
||||
// it('Verify that the user is logged in', function() {
|
||||
// //Delete all cookies
|
||||
// browser.driver.manage().deleteAllCookies();
|
||||
// //Enter UserName
|
||||
// element.all(by.model('username')).get(0).sendKeys('abc@wingify.com');
|
||||
// //Enter Password
|
||||
// element(by.model('password')).sendKeys('test');
|
||||
// //Click Submit button
|
||||
// element(by.css('.login-form button[type="submit"]')).click();
|
||||
// //Wait for the current URL to change to welcome
|
||||
// browser.driver.wait(function() {
|
||||
// return browser.driver.getCurrentUrl().then(function(url) {
|
||||
// return (/welcome/).test(url);
|
||||
// });
|
||||
// });
|
||||
// var firstname = element(by.model('credentials.firstname')),
|
||||
// lastname = element(by.model('credentials.lastname')),
|
||||
// email = element(by.model('credentials.email')),
|
||||
// password = element(by.model('credentials.password'));
|
||||
|
||||
// email.sendKeys('admin@app.com');
|
||||
// firstname.sendKeys('admin_first');
|
||||
// lastname.sendKeys('admin_last');
|
||||
// password.sendKeys('1234');
|
||||
|
||||
// //Click signup button
|
||||
// element(by.css('.btn.btn-large.btn-primary')).click().then(function () {
|
||||
// expect(browser.getCurrentUrl()).toEqual('http://localhost:3000/#!/signup-success');
|
||||
// });
|
||||
|
||||
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
|
||||
// // Principal controller Spec
|
||||
// describe('AuthenticationController Unit Tests', function() {
|
||||
// // Initialize global variables
|
||||
// var AuthenticationController,
|
||||
// scope,
|
||||
// $httpBackend,
|
||||
// $stateParams,
|
||||
// $location;
|
||||
|
||||
// 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));
|
||||
|
||||
// // 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, _$location_, _$stateParams_, _$httpBackend_) {
|
||||
// // Set a new global scope
|
||||
// scope = $rootScope.$new();
|
||||
|
||||
// // Point global variables to injected services
|
||||
// $stateParams = _$stateParams_;
|
||||
// $httpBackend = _$httpBackend_;
|
||||
// $location = _$location_;
|
||||
|
||||
// // Initialize the Principal controller
|
||||
// AuthenticationController = $controller('AuthenticationController', {
|
||||
// $scope: scope
|
||||
// });
|
||||
// }));
|
||||
|
||||
|
||||
// it('$scope.signin() should login with a correct user and password', function() {
|
||||
// // Test expected GET request
|
||||
// $httpBackend.when('POST', '/auth/signin').respond(200, 'Fred');
|
||||
|
||||
// scope.signin();
|
||||
// $httpBackend.flush();
|
||||
|
||||
// // Test scope value
|
||||
// expect(scope.authentication.user).toEqual('Fred');
|
||||
// expect($location.url()).toEqual('/');
|
||||
// });
|
||||
|
||||
// it('$scope.signin() should fail to log in with nothing', function() {
|
||||
// // Test expected POST request
|
||||
// $httpBackend.expectPOST('/auth/signin').respond(400, {
|
||||
// 'message': 'Missing credentials'
|
||||
// });
|
||||
|
||||
// scope.signin();
|
||||
// $httpBackend.flush();
|
||||
|
||||
// // Test scope value
|
||||
// expect(scope.error).toEqual('Missing credentials');
|
||||
// });
|
||||
|
||||
// it('$scope.signin() should fail to log in with wrong credentials', function() {
|
||||
// // Foo/Bar combo assumed to not exist
|
||||
// scope.authentication.user = 'Foo';
|
||||
// scope.credentials = 'Bar';
|
||||
|
||||
// // Test expected POST request
|
||||
// $httpBackend.expectPOST('/auth/signin').respond(400, {
|
||||
// 'message': 'Unknown user'
|
||||
// });
|
||||
|
||||
// scope.signin();
|
||||
// $httpBackend.flush();
|
||||
|
||||
// // Test scope value
|
||||
// expect(scope.error).toEqual('Unknown user');
|
||||
// });
|
||||
|
||||
// it('$scope.signup() should register with correct data', function() {
|
||||
// // Test expected GET request
|
||||
// scope.authentication.user = 'Fred';
|
||||
// $httpBackend.when('POST', '/auth/signup').respond(200, 'Fred');
|
||||
|
||||
// scope.signup();
|
||||
// $httpBackend.flush();
|
||||
|
||||
// // test scope value
|
||||
// expect(scope.authentication.user).toBe('Fred');
|
||||
// expect(scope.error).toEqual(undefined);
|
||||
// expect($location.url()).toBe('/');
|
||||
// });
|
||||
|
||||
// it('$scope.signup() should fail to register with duplicate Username', function() {
|
||||
// // Test expected POST request
|
||||
// $httpBackend.when('POST', '/auth/signup').respond(400, {
|
||||
// 'message': 'Username already exists'
|
||||
// });
|
||||
|
||||
// scope.signup();
|
||||
// $httpBackend.flush();
|
||||
|
||||
// // Test scope value
|
||||
// expect(scope.error).toBe('Username already exists');
|
||||
// });
|
||||
// });
|
||||
// }());
|
|
@ -1,181 +1,181 @@
|
|||
// 'use strict';
|
||||
'use strict';
|
||||
|
||||
// (function() {
|
||||
// // Forms Controller Spec
|
||||
// describe('Authentication Controller Tests', function() {
|
||||
// // Initialize global variables
|
||||
// var AuthenticationController,
|
||||
// scope,
|
||||
// $httpBackend,
|
||||
// $stateParams,
|
||||
// $location,
|
||||
// $state;
|
||||
(function() {
|
||||
// Forms Controller Spec
|
||||
describe('Authentication Controller Tests', function() {
|
||||
// Initialize global variables
|
||||
var AuthenticationController,
|
||||
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 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 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 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 sampleCredentials = {
|
||||
// username: sampleUser.username,
|
||||
// password: sampleUser.password,
|
||||
// };
|
||||
var sampleCredentials = {
|
||||
username: sampleUser.username,
|
||||
password: sampleUser.password,
|
||||
};
|
||||
|
||||
|
||||
|
||||
// // 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)
|
||||
// };
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
// 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));
|
||||
// Load the main application module
|
||||
beforeEach(module(ApplicationConfiguration.applicationModuleName));
|
||||
|
||||
// beforeEach(module('stateMock'));
|
||||
beforeEach(module('stateMock'));
|
||||
|
||||
// // 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');
|
||||
// }
|
||||
// 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;
|
||||
},
|
||||
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;
|
||||
// }
|
||||
// };
|
||||
// });
|
||||
// }));
|
||||
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;
|
||||
// }
|
||||
// };
|
||||
// });
|
||||
// }));
|
||||
// Mock Authentication Service
|
||||
beforeEach(module(function($provide) {
|
||||
$provide.service('Auth', function() {
|
||||
return {
|
||||
ensureHasCurrentUser: function() {
|
||||
return sampleUser;
|
||||
},
|
||||
isAuthenticated: function() {
|
||||
return true;
|
||||
},
|
||||
getUserState: function() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
});
|
||||
}));
|
||||
|
||||
// // 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();
|
||||
// scope.abc = 'hello';
|
||||
// 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();
|
||||
scope.abc = 'hello';
|
||||
|
||||
// // Point global variables to injected services
|
||||
// $stateParams = _$stateParams_;
|
||||
// $httpBackend = _$httpBackend_;
|
||||
// $location = _$location_;
|
||||
// $state = _$state_;
|
||||
// Point global variables to injected services
|
||||
$stateParams = _$stateParams_;
|
||||
$httpBackend = _$httpBackend_;
|
||||
$location = _$location_;
|
||||
$state = _$state_;
|
||||
|
||||
// // $httpBackend.whenGET(/\.html$/).respond('');
|
||||
// $httpBackend.whenGET('/users/me/').respond('');
|
||||
// $httpBackend.whenGET(/\.html$/).respond('');
|
||||
$httpBackend.whenGET('/users/me/').respond('');
|
||||
|
||||
// // Initialize the Forms controller.
|
||||
// AuthenticationController = $controller('AuthenticationController', { $scope: scope });
|
||||
// }));
|
||||
// Initialize the Forms controller.
|
||||
AuthenticationController = $controller('AuthenticationController', { $scope: scope });
|
||||
}));
|
||||
|
||||
// it('$scope.signin should sigin in user with valid credentials', inject(function(Auth) {
|
||||
it('$scope.signin should sigin in user with valid credentials', inject(function(Auth) {
|
||||
|
||||
// //Set $state transition
|
||||
// // $state.expectTransitionTo('listForms');
|
||||
// //Set POST response
|
||||
// // $httpBackend.expect('POST', '/auth/signin', sampleCredentials).respond(200, sampleUser);
|
||||
//Set $state transition
|
||||
// $state.expectTransitionTo('listForms');
|
||||
//Set POST response
|
||||
// $httpBackend.expect('POST', '/auth/signin', sampleCredentials).respond(200, sampleUser);
|
||||
|
||||
// scope.abc = 'sampleCredentials';
|
||||
// //Run Controller Logic to Test
|
||||
// scope.signin();
|
||||
scope.abc = 'sampleCredentials';
|
||||
//Run Controller Logic to Test
|
||||
scope.signin();
|
||||
|
||||
// // $httpBackend.flush();
|
||||
// $httpBackend.flush();
|
||||
|
||||
// // Test scope value
|
||||
// // expect(Auth.ensureHasCurrentUser()).toEqualData(sampleUser);
|
||||
// }));
|
||||
// Test scope value
|
||||
// expect(Auth.ensureHasCurrentUser()).toEqualData(sampleUser);
|
||||
}));
|
||||
|
||||
|
||||
// });
|
||||
// }());
|
||||
});
|
||||
}());
|
|
@ -65,7 +65,6 @@
|
|||
}));
|
||||
|
||||
it('Auth.getUserState() should fetch current user state', function() {
|
||||
|
||||
//Run Service Logic to Test
|
||||
Auth.login(sampleUser);
|
||||
var currUserState = Auth.getUserState();
|
||||
|
|
Loading…
Reference in a new issue