tellform/app/tests/form.server.model.test.js

93 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-03-30 03:45:16 +00:00
'use strict';
2015-09-10 22:06:28 +00:00
2016-03-30 03:45:16 +00:00
/**
* Module dependencies.
*/
var should = require('should'),
mongoose = require('mongoose'),
User = mongoose.model('User'),
2017-04-23 02:56:47 +00:00
Form = mongoose.model('Form');
2015-09-10 22:06:28 +00:00
2016-03-30 03:45:16 +00:00
/**
* Globals
*/
2017-04-23 02:56:47 +00:00
var user, myForm;
2015-09-10 22:06:28 +00:00
2016-03-30 03:45:16 +00:00
/**
* Unit tests
*/
describe('Form Model Unit Tests:', function() {
this.timeout(15000);
beforeEach(function(done) {
user = new User({
firstName: 'Full',
lastName: 'Name',
email: 'test@test.com',
username: 'aueoaueoa',
password: 'password',
provider: 'local'
});
2015-10-06 20:14:38 +00:00
2016-03-30 03:45:16 +00:00
user.save(function(err) {
if(err) {
2017-04-23 02:56:47 +00:00
return done(err);
2016-03-30 03:45:16 +00:00
}
myForm = new Form({
title: 'Form Title',
admin: user,
2016-11-09 20:25:38 +00:00
language: 'en',
2016-03-30 03:45:16 +00:00
form_fields: [
{'fieldType':'textfield', title:'First Name', 'fieldValue': ''},
{'fieldType':'checkbox', title:'nascar', 'fieldValue': ''},
{'fieldType':'checkbox', title:'hockey', 'fieldValue': ''}
]
});
done();
});
});
2015-09-10 22:06:28 +00:00
2016-03-30 03:45:16 +00:00
describe('Method Save', function() {
it('should be able to save without problems', function(done) {
2016-11-09 20:25:38 +00:00
myForm.save(function(err) {
2016-03-30 03:45:16 +00:00
should.not.exist(err);
done();
});
});
2015-09-10 22:06:28 +00:00
2016-03-30 03:45:16 +00:00
it('should be able to show an error when try to save without title', function(done) {
2015-09-10 22:06:28 +00:00
2016-03-30 03:45:16 +00:00
var _form = myForm;
_form.title = '';
2016-11-09 20:25:38 +00:00
_form.save(function(err) {
2016-03-30 03:45:16 +00:00
should.exist(err);
should.equal(err.errors.title.message, 'Form Title cannot be blank');
done();
});
});
});
2015-09-10 22:06:28 +00:00
2016-03-30 03:45:16 +00:00
describe('Method Find', function(){
beforeEach(function(done){
myForm.save(function(err) {
2017-04-23 02:56:47 +00:00
return done(err);
2016-03-30 03:45:16 +00:00
});
});
it('should be able to findOne my form without problems', function(done) {
2017-04-23 02:56:47 +00:00
Form.findOne({title: myForm.title}).exec(function(err, form) {
2016-03-30 03:45:16 +00:00
should.not.exist(err);
should.exist(form);
2016-11-09 20:25:38 +00:00
should.equal(form.toObject().id, myForm.toObject().id);
2016-03-30 03:45:16 +00:00
done();
});
});
});
2015-09-10 22:06:28 +00:00
2016-03-30 03:45:16 +00:00
afterEach(function(done) {
Form.remove().exec(function() {
User.remove().exec(done);
});
});
});