added tests
This commit is contained in:
parent
6983440113
commit
0c041cabf4
|
@ -99,10 +99,12 @@ FormSchema.pre('remove', function (next) {
|
||||||
|
|
||||||
//Set _original
|
//Set _original
|
||||||
FormSchema.pre('save', function (next) {
|
FormSchema.pre('save', function (next) {
|
||||||
|
console.log(this.constructor.modelName);
|
||||||
this.constructor // ≈ mongoose.model('…', FieldSchema).findById
|
this.constructor // ≈ mongoose.model('…', FieldSchema).findById
|
||||||
.findOne({title: this.title}, function(err, original){
|
.findOne({title: this.title}, function(err, original){
|
||||||
if(err) next(err);
|
if(err) next(err);
|
||||||
else {
|
else {
|
||||||
|
console.log(original);
|
||||||
_original = original;
|
_original = original;
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
@ -110,8 +112,6 @@ FormSchema.pre('save', function (next) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Update lastModified and created everytime we save
|
//Update lastModified and created everytime we save
|
||||||
FormSchema.pre('save', function (next) {
|
FormSchema.pre('save', function (next) {
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
|
@ -257,6 +257,8 @@ FormSchema.pre('save', function (next) {
|
||||||
// console.log('_original\n------------\n\n');
|
// console.log('_original\n------------\n\n');
|
||||||
// console.log(_original);
|
// console.log(_original);
|
||||||
// console.log(this.isModified('form_fields') && !!this.form_fields && !!_original);
|
// console.log(this.isModified('form_fields') && !!this.form_fields && !!_original);
|
||||||
|
|
||||||
|
console.log(_original)
|
||||||
if(this.isModified('form_fields') && this.form_fields && _original){
|
if(this.isModified('form_fields') && this.form_fields && _original){
|
||||||
|
|
||||||
var old_form_fields = _original.form_fields,
|
var old_form_fields = _original.form_fields,
|
||||||
|
@ -265,7 +267,7 @@ FormSchema.pre('save', function (next) {
|
||||||
deletedIds = getDeletedIndexes(old_ids, new_ids),
|
deletedIds = getDeletedIndexes(old_ids, new_ids),
|
||||||
that = this;
|
that = this;
|
||||||
|
|
||||||
// console.log('deletedId Indexes\n--------');
|
console.log('deletedId Indexes\n--------');
|
||||||
// console.log(deletedIds);
|
// console.log(deletedIds);
|
||||||
// console.log('old_ids\n--------');
|
// console.log('old_ids\n--------');
|
||||||
// console.log(old_ids);
|
// console.log(old_ids);
|
||||||
|
|
|
@ -76,7 +76,7 @@ FormSubmissionSchema.pre('save', function (next){
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('ipAddr check');
|
// console.log('ipAddr check');
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -85,10 +85,10 @@ FormSubmissionSchema.pre('save', function (next) {
|
||||||
var fdfData, dest_filename, dest_path,
|
var fdfData, dest_filename, dest_path,
|
||||||
that = this,
|
that = this,
|
||||||
_form = this.form;
|
_form = this.form;
|
||||||
console.log(this.pdf);
|
|
||||||
|
|
||||||
if(this.pdf.path){
|
|
||||||
|
|
||||||
|
if(this.pdf && this.pdf.path){
|
||||||
|
console.log(this.pdf);
|
||||||
dest_filename = that.title.replace(/ /g,'')+'_submission_'+Date.now()+'.pdf';
|
dest_filename = that.title.replace(/ /g,'')+'_submission_'+Date.now()+'.pdf';
|
||||||
var __path = this.pdf.path.split('/').slice(0,this.pdf.path.split('/').length-1).join('/');
|
var __path = this.pdf.path.split('/').slice(0,this.pdf.path.split('/').length-1).join('/');
|
||||||
dest_path = path.join(__path, dest_filename);
|
dest_path = path.join(__path, dest_filename);
|
||||||
|
|
|
@ -84,7 +84,7 @@ describe('Form Model Unit Tests:', function() {
|
||||||
mySubmission = new FormSubmission({
|
mySubmission = new FormSubmission({
|
||||||
form_fields: submission_fields,
|
form_fields: submission_fields,
|
||||||
admin: user,
|
admin: user,
|
||||||
form: myForm._id,
|
form: myForm,
|
||||||
timeElapsed: 17.55
|
timeElapsed: 17.55
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -114,7 +114,8 @@ describe('Form Model Unit Tests:', function() {
|
||||||
myForm.form_fields = new_form_fields_del;
|
myForm.form_fields = new_form_fields_del;
|
||||||
return myForm.save(function(err, form) {
|
return myForm.save(function(err, form) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
var actual_fields = form.toObject();
|
var actual_fields = form.toObject().form_fields;
|
||||||
|
// console.log(actual_fields);
|
||||||
|
|
||||||
should.deepEqual(form.toObject().form_fields, expected_fields, 'old form_fields not equal to newly saved form_fields');
|
should.deepEqual(form.toObject().form_fields, expected_fields, 'old form_fields not equal to newly saved form_fields');
|
||||||
done();
|
done();
|
||||||
|
|
|
@ -53,7 +53,7 @@ angular.module('forms').controller('ViewFormController', ['$rootScope', '$scope'
|
||||||
* Table Functions
|
* Table Functions
|
||||||
*/
|
*/
|
||||||
$scope.isAtLeastOneChecked = function(){
|
$scope.isAtLeastOneChecked = function(){
|
||||||
// console.log('isAtLeastOneChecked');
|
console.log('isAtLeastOneChecked');
|
||||||
for(var i=0; i<$scope.table.rows.length; i++){
|
for(var i=0; i<$scope.table.rows.length; i++){
|
||||||
if($scope.table.rows[i].selected) return true;
|
if($scope.table.rows[i].selected) return true;
|
||||||
}
|
}
|
||||||
|
@ -78,14 +78,7 @@ angular.module('forms').controller('ViewFormController', ['$rootScope', '$scope'
|
||||||
//Delete selected submissions of Form
|
//Delete selected submissions of Form
|
||||||
$scope.deleteSelectedSubmissions = function(){
|
$scope.deleteSelectedSubmissions = function(){
|
||||||
// console.log('deleteSelectedSubmissions');
|
// console.log('deleteSelectedSubmissions');
|
||||||
var delete_ids = [];
|
var delete_ids = _.chain($scope.table.rows).filter(function(row){
|
||||||
// for(var i=0; i<$scope.table.rows.length; i++){
|
|
||||||
// if($scope.table.rows[i].selected){
|
|
||||||
// delete_ids.push($scope.table.rows[i]._id);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
delete_ids = _.chain($scope.table.rows).filter(function(row){
|
|
||||||
return !!row.selected;
|
return !!row.selected;
|
||||||
}).pluck('_id').value();
|
}).pluck('_id').value();
|
||||||
console.log(delete_ids);
|
console.log(delete_ids);
|
||||||
|
@ -156,8 +149,6 @@ angular.module('forms').controller('ViewFormController', ['$rootScope', '$scope'
|
||||||
// console.log('form submissions successfully fetched');
|
// console.log('form submissions successfully fetched');
|
||||||
// console.log( JSON.parse(JSON.stringify($scope.submissions)) ) ;
|
// console.log( JSON.parse(JSON.stringify($scope.submissions)) ) ;
|
||||||
// console.log( JSON.parse(JSON.stringify($scope.myform.form_fields)) );
|
// console.log( JSON.parse(JSON.stringify($scope.myform.form_fields)) );
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
.error(function(err){
|
.error(function(err){
|
||||||
console.log('Could not fetch form submissions.\nError: '+err);
|
console.log('Could not fetch form submissions.\nError: '+err);
|
||||||
|
@ -238,8 +229,6 @@ angular.module('forms').controller('ViewFormController', ['$rootScope', '$scope'
|
||||||
if(!$scope.$digest){
|
if(!$scope.$digest){
|
||||||
$scope.$apply();
|
$scope.$apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}).catch(function(response){
|
}).catch(function(response){
|
||||||
console.log('Error occured during form UPDATE.\n');
|
console.log('Error occured during form UPDATE.\n');
|
||||||
console.log(response.data);
|
console.log(response.data);
|
||||||
|
|
170
public/modules/forms/tests/view-from.client.controller.test.js
Normal file
170
public/modules/forms/tests/view-from.client.controller.test.js
Normal file
|
@ -0,0 +1,170 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
// Forms Controller Spec
|
||||||
|
describe('ViewForm Controller Tests', function() {
|
||||||
|
// Initialize global variables
|
||||||
|
var ViewFormController,
|
||||||
|
scope,
|
||||||
|
$httpBackend,
|
||||||
|
$stateParams,
|
||||||
|
$location;
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Then we can start by loading 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 Forms controller.
|
||||||
|
FormsController = $controller('FormsController', {
|
||||||
|
$scope: scope
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('$scope.find() should create an array with at least one article object fetched from XHR', inject(function(Forms) {
|
||||||
|
// Create sample article using the Forms service
|
||||||
|
var sampleArticle = new Forms({
|
||||||
|
title: 'An Article about MEAN',
|
||||||
|
content: 'MEAN rocks!'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create a sample Forms array that includes the new article
|
||||||
|
var sampleForms = [sampleForm];
|
||||||
|
|
||||||
|
// Set GET response
|
||||||
|
$httpBackend.expectGET('Forms').respond(sampleForms);
|
||||||
|
|
||||||
|
// Run controller functionality
|
||||||
|
scope.find();
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
// Test scope value
|
||||||
|
expect(scope.Forms).toEqualData(sampleForms);
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('$scope.findOne() should create an array with one article object fetched from XHR using a articleId URL parameter', inject(function(Forms) {
|
||||||
|
// Define a sample article object
|
||||||
|
var sampleArticle = new Forms({
|
||||||
|
title: 'An Article about MEAN',
|
||||||
|
content: 'MEAN rocks!'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Set the URL parameter
|
||||||
|
$stateParams.articleId = '525a8422f6d0f87f0e407a33';
|
||||||
|
|
||||||
|
// Set GET response
|
||||||
|
$httpBackend.expectGET(/Forms\/([0-9a-fA-F]{24})$/).respond(sampleArticle);
|
||||||
|
|
||||||
|
// Run controller functionality
|
||||||
|
scope.findOne();
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
// Test scope value
|
||||||
|
expect(scope.article).toEqualData(sampleArticle);
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('$scope.create() with valid form data should send a POST request with the form input values and then locate to new object URL', inject(function(Forms) {
|
||||||
|
// Create a sample article object
|
||||||
|
var sampleArticlePostData = new Forms({
|
||||||
|
title: 'An Article about MEAN',
|
||||||
|
content: 'MEAN rocks!'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create a sample article response
|
||||||
|
var sampleArticleResponse = new Forms({
|
||||||
|
_id: '525cf20451979dea2c000001',
|
||||||
|
title: 'An Article about MEAN',
|
||||||
|
content: 'MEAN rocks!'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fixture mock form input values
|
||||||
|
scope.title = 'An Article about MEAN';
|
||||||
|
scope.content = 'MEAN rocks!';
|
||||||
|
|
||||||
|
// Set POST response
|
||||||
|
$httpBackend.expectPOST('Forms', sampleArticlePostData).respond(sampleArticleResponse);
|
||||||
|
|
||||||
|
// Run controller functionality
|
||||||
|
scope.create();
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
// Test form inputs are reset
|
||||||
|
expect(scope.title).toEqual('');
|
||||||
|
expect(scope.content).toEqual('');
|
||||||
|
|
||||||
|
// Test URL redirection after the article was created
|
||||||
|
expect($location.path()).toBe('/Forms/' + sampleArticleResponse._id);
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('$scope.update() should update a valid article', inject(function(Forms) {
|
||||||
|
// Define a sample article put data
|
||||||
|
var sampleArticlePutData = new Forms({
|
||||||
|
_id: '525cf20451979dea2c000001',
|
||||||
|
title: 'An Article about MEAN',
|
||||||
|
content: 'MEAN Rocks!'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mock article in scope
|
||||||
|
scope.article = sampleArticlePutData;
|
||||||
|
|
||||||
|
// Set PUT response
|
||||||
|
$httpBackend.expectPUT(/Forms\/([0-9a-fA-F]{24})$/).respond();
|
||||||
|
|
||||||
|
// Run controller functionality
|
||||||
|
scope.update();
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
// Test URL location to new object
|
||||||
|
expect($location.path()).toBe('/Forms/' + sampleArticlePutData._id);
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('$scope.remove() should send a DELETE request with a valid articleId and remove the article from the scope', inject(function(Forms) {
|
||||||
|
// Create new article object
|
||||||
|
var sampleArticle = new Forms({
|
||||||
|
_id: '525a8422f6d0f87f0e407a33'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create new Forms array and include the article
|
||||||
|
scope.Forms = [sampleArticle];
|
||||||
|
|
||||||
|
// Set expected DELETE response
|
||||||
|
$httpBackend.expectDELETE(/Forms\/([0-9a-fA-F]{24})$/).respond(204);
|
||||||
|
|
||||||
|
// Run controller functionality
|
||||||
|
scope.remove(sampleArticle);
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
// Test array after successful delete
|
||||||
|
expect(scope.Forms.length).toBe(0);
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
}());
|
Loading…
Reference in a new issue