2016-03-30 01:21:45 +00:00
'use strict' ;
/ * *
* Module dependencies .
* /
var should = require ( 'should' ) ,
mongoose = require ( 'mongoose' ) ,
User = mongoose . model ( 'User' ) ,
Form = mongoose . model ( 'Form' ) ,
Field = mongoose . model ( 'Field' ) ,
_ = require ( 'lodash' ) ,
async = require ( 'async' ) ,
config = require ( '../../config/config' ) ,
FormSubmission = mongoose . model ( 'FormSubmission' ) ;
2016-03-30 03:45:16 +00:00
var exampleDemo = {
2016-03-30 01:21:45 +00:00
address : '880-9650 Velit. St.' ,
city : '' ,
dateOfBirth : '10' ,
displayName : 'Test User' ,
email : 'polydaic@gmail.com' ,
firstName : 'Test User' ,
hin : '' ,
lastName : 'AddDemographic' ,
lastUpdateDate : '2014-10-01 00:00:00' ,
monthOfBirth : '05' ,
officialLanguage : 'English' ,
phone : '250-222-2222' ,
phone2 : '' ,
postal : 'S4M 7T8' ,
province : 'BC' ,
sex : 'F' ,
sexDesc : 'Female' ,
sin : '' ,
spokenLanguage : 'English' ,
title : 'MS.' ,
2016-03-30 03:45:16 +00:00
yearOfBirth : '2015'
2016-03-30 01:21:45 +00:00
} ;
var sampleFormFields = [
{ 'fieldType' : 'textfield' , 'title' : 'What\'s your first name' , 'fieldValue' : '' } ,
{ 'fieldType' : 'textfield' , 'title' : 'And your last name' , 'fieldValue' : '' } ,
{ 'fieldType' : 'radio' , 'title' : 'And your sex' , 'fieldOptions' : [ { 'option_id' : 0 , 'option_title' : 'Male' , 'option_value' : 'M' } , { 'option_id' : 1 , 'option_title' : 'Female' , 'option_value' : 'F' } ] , 'fieldValue' : '' } ,
{ 'fieldType' : 'date' , 'title' : 'When were you born?' , 'fieldValue' : '' } ,
{ 'fieldType' : 'number' , 'title' : 'What\'s your phone #?' , 'fieldValue' : '' }
] ;
var sampleSubmission = [
{ 'fieldType' : 'textfield' , 'title' : 'What\'s your first name' , 'fieldValue' : 'David' } ,
{ 'fieldType' : 'textfield' , 'title' : 'And your last name' , 'fieldValue' : 'Baldwynn' } ,
{ 'fieldType' : 'radio' , 'title' : 'And your sex' , 'fieldValue' : 'M' , 'fieldOptions' : [ { 'option_id' : 0 , 'option_title' : 'Male' , 'option_value' : 'M' } , { 'option_id' : 1 , 'option_title' : 'Female' , 'option_value' : 'F' } ] } ,
{ 'fieldType' : 'date' , 'title' : 'When were you born?' , 'fieldValue' : 'Tue Oct 06 2015 15:17:48 GMT-0700 (PDT)' } ,
{ 'fieldType' : 'number' , 'title' : 'What\'s your phone #?' , 'fieldValue' : '6043158008' }
] ;
2016-03-30 03:45:16 +00:00
_ . mixin ( {
deepOmit : function ( obj , iteratee , context ) {
var r = _ . omit ( obj , iteratee , context ) ;
_ . each ( r , function ( val , key ) {
2016-04-17 02:45:17 +00:00
if ( typeof ( val ) === 'object' )
2016-03-30 03:45:16 +00:00
r [ key ] = _ . deepOmit ( val , iteratee , context ) ;
} ) ;
return r ;
}
} ) ;
2016-03-30 01:21:45 +00:00
/ * *
* Globals
* /
var user , myForm , mySubmission ;
/ * *
* Unit tests
* /
describe ( 'FormSubmission Model Unit Tests:' , function ( ) {
this . timeout ( 15000 ) ;
beforeEach ( function ( done ) {
user = new User ( {
firstName : 'Full' ,
lastName : 'Name' ,
displayName : 'Full Name' ,
email : 'test1@test.com' + Date . now ( ) ,
2016-11-09 20:25:38 +00:00
username : 'test1' + Date . now ( ) ,
2016-03-30 01:21:45 +00:00
password : 'password' ,
provider : 'local'
} ) ;
user . save ( function ( err ) {
if ( err ) {
done ( err ) ;
return ;
2016-03-30 03:45:16 +00:00
}
2016-03-30 01:21:45 +00:00
myForm = new Form ( {
title : 'Form Title1' ,
admin : user . _id ,
2016-11-09 20:25:38 +00:00
language : 'en' ,
2016-03-30 01:21:45 +00:00
form _fields : [
{ 'fieldType' : 'textfield' , 'title' : 'What\'s your first name' , 'fieldValue' : '' } ,
{ 'fieldType' : 'textfield' , 'title' : 'And your last name' , 'fieldValue' : '' } ,
{ 'fieldType' : 'radio' , 'title' : 'And your sex' , 'fieldOptions' : [ { 'option_id' : 0 , 'option_title' : 'Male' , 'option_value' : 'M' } , { 'option_id' : 1 , 'option_title' : 'Female' , 'option_value' : 'F' } ] , 'fieldValue' : '' } ,
{ 'fieldType' : 'date' , 'title' : 'When were you born?' , 'fieldValue' : '' } ,
2016-04-29 06:00:41 +00:00
{ 'fieldType' : 'number' , 'title' : 'What\'s your phone #?' , 'fieldValue' : '' }
2016-04-29 06:22:47 +00:00
]
2016-03-30 01:21:45 +00:00
} ) ;
myForm . save ( function ( err , form ) {
if ( err ) {
console . log ( err . errors ) ;
done ( err ) ;
return ;
}
var submissionFields = _ . clone ( myForm . form _fields ) ;
for ( var z = 0 ; z < submissionFields . length ; z ++ ) {
submissionFields [ z ] = _ . extend ( myForm . form _fields [ z ] , sampleSubmission [ z ] ) ;
}
2016-03-30 03:45:16 +00:00
2016-03-30 01:21:45 +00:00
mySubmission = new FormSubmission ( {
2016-03-30 03:45:16 +00:00
admin : user . _id ,
2016-03-30 01:21:45 +00:00
form : myForm . _id ,
timeElapsed : 17.55 ,
form _fields : submissionFields
} ) ;
2016-03-30 03:45:16 +00:00
2016-03-30 01:21:45 +00:00
done ( ) ;
} ) ;
} ) ;
} ) ;
describe ( 'Method Save' , function ( ) {
this . timeout ( 15000 ) ;
var oscar _demo _num ;
beforeEach ( function ( done ) {
var myFieldMap = { } ;
myForm . save ( function ( err , form ) {
if ( err ) done ( err ) ;
else done ( ) ;
} ) ;
} ) ;
it ( 'should be able to save a FormSubmission without problems' , function ( done ) {
2016-11-09 20:25:38 +00:00
mySubmission . save ( function ( err , submission ) {
2016-03-30 01:21:45 +00:00
should . not . exist ( err ) ;
should . exist ( submission ) ;
done ( ) ;
} ) ;
} ) ;
} ) ;
describe ( 'Method Find' , function ( ) {
beforeEach ( function ( done ) {
mySubmission . save ( function ( err ) {
done ( ) ;
} ) ;
} ) ;
it ( 'should be able to findOne FormSubmission without problems' , function ( done ) {
2016-11-09 20:25:38 +00:00
FormSubmission . findOne ( { _id : mySubmission . _id } ) . exec ( function ( err , submission ) {
2016-03-30 01:21:45 +00:00
should . not . exist ( err ) ;
should . exist ( submission ) ;
should . deepEqual ( submission . toObject ( ) , mySubmission . toObject ( ) ) ;
done ( ) ;
} ) ;
} ) ;
it ( 'should be able to find FormSubmission by $elemMatch on form_fields id' , function ( done ) {
2016-11-09 20:25:38 +00:00
FormSubmission . findOne ( { form : myForm . _id , admin : user , form _fields : { $elemMatch : { _id : myForm . form _fields [ 0 ] . _id } } } )
2016-03-30 01:21:45 +00:00
. exec ( function ( err , submission ) {
should . not . exist ( err ) ;
should . exist ( submission ) ;
should . deepEqual ( submission . toObject ( ) , mySubmission . toObject ( ) ) ;
done ( ) ;
} ) ;
} ) ;
} ) ;
describe ( 'Test FormField and Submission Logic' , function ( ) {
beforeEach ( function ( done ) {
2016-03-30 03:45:16 +00:00
2016-03-30 01:21:45 +00:00
//Create Submission
mySubmission = new FormSubmission ( {
form _fields : sampleSubmission ,
2016-03-30 03:45:16 +00:00
admin : user ,
2016-03-30 01:21:45 +00:00
form : myForm ,
timeElapsed : 17.55
2016-03-30 03:45:16 +00:00
} ) ;
2016-03-30 01:21:45 +00:00
mySubmission . save ( function ( err ) {
should . not . exist ( err ) ;
done ( ) ;
} ) ;
2016-03-30 03:45:16 +00:00
2016-03-30 01:21:45 +00:00
} ) ;
2015-09-18 16:32:17 +00:00
2016-04-21 18:48:35 +00:00
/ *
it ( 'should preserve deleted form_fields that have submissions without any problems' , function ( done ) {
2015-09-18 16:32:17 +00:00
2016-03-30 03:45:16 +00:00
var old _fields = myForm . toObject ( ) . form _fields ;
var new _form _fields = _ . clone ( myForm . toObject ( ) . form _fields ) ;
new _form _fields . splice ( 0 , 1 ) ;
2015-09-18 16:32:17 +00:00
2016-03-30 03:45:16 +00:00
myForm . form _fields = new _form _fields ;
2015-09-18 16:32:17 +00:00
2016-03-30 03:45:16 +00:00
myForm . save ( function ( err , _form ) {
2015-09-18 16:32:17 +00:00
2016-03-30 03:45:16 +00:00
should . not . exist ( err ) ;
should . exist ( _form . form _fields ) ;
2015-09-18 16:32:17 +00:00
2016-03-30 03:45:16 +00:00
var actual _fields = _ . deepOmit ( _form . toObject ( ) . form _fields , [ 'lastModified' , 'created' , '_id' ] ) ;
old _fields = _ . deepOmit ( old _fields , [ 'lastModified' , 'created' , '_id' ] ) ;
should . deepEqual ( JSON . stringify ( actual _fields ) , JSON . stringify ( 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 ;
var new _form _fields = _ . clone ( myForm . toObject ( ) . form _fields ) ;
new _form _fields . splice ( 0 , 1 ) ;
myForm . form _fields = new _form _fields ;
myForm . save ( function ( err , _form ) {
should . not . exist ( err ) ;
should . exist ( _form . form _fields ) ;
should . exist ( old _fields ) ;
var actual _fields = _ . deepOmit ( _form . toObject ( ) . form _fields , [ 'lastModified' , 'created' , '_id' ] ) ;
old _fields = _ . deepOmit ( old _fields , [ 'lastModified' , 'created' , '_id' ] ) ;
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();
// });
} ) ;
} ) ;
2016-04-21 18:48:35 +00:00
* /
2016-03-30 01:21:45 +00:00
afterEach ( function ( done ) {
mySubmission . remove ( function ( ) {
done ( ) ;
} ) ;
} ) ;
} ) ;
afterEach ( function ( done ) {
Form . remove ( ) . exec ( function ( ) {
User . remove ( ) . exec ( function ( ) {
FormSubmission . remove ( ) . exec ( done ) ;
} ) ;
} ) ;
} ) ;
} ) ;