Skip to content

Instantly share code, notes, and snippets.

@fatso83
Last active June 21, 2017 19:48
Show Gist options
  • Save fatso83/d848fc9b8e90a1df32c706488580b9ff to your computer and use it in GitHub Desktop.
Save fatso83/d848fc9b8e90a1df32c706488580b9ff to your computer and use it in GitHub Desktop.
{
"presets": [
"es2015"
],
"plugins": [
"transform-class-properties",
"transform-react-jsx",
"transform-object-rest-spread",
"babel-root-slash-import"
]
}
import Bluebird from 'bluebird';
import NaturalPerson from './model'
class NaturalController {
constructor() {
this.naturalPersonModel = NaturalPerson;
}
add(data) {
var newNP = new this.naturalPersonModel(data);
return newNP.save()
.catch(function(reason){
throw new Error(reason);
});
}
}
export default NaturalController;
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
import Bluebird from 'bluebird';
var autoIncrement = require('mongoose-auto-increment');
autoIncrement.initialize(mongoose.connection);
var naturalPersonSchemaBr = new mongoose.Schema({
naturalPersonData:{
personUUID: String,
nome: {type: String,required:true},
sobrenome: {type: String,required:true},
cpf: {type: String,required:true,unique:true},
rg: {type: String,required:true},
purchase:{
firstBuyStoreUUID: Number,
firstSellerUUID: Number
}
}
});
naturalPersonSchemaBr.methods.capitalizeFn = function(obj, list) {
String.prototype.capitalize = function() {
return this.split(' ').map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
};
list.forEach((attr) => {
if (obj[attr] && typeof(obj[attr]) === 'string') {
obj[attr] = obj[attr].toLowerCase().capitalize();
}
});
};
naturalPersonSchemaBr.plugin(autoIncrement.plugin, {
model: 'NaturalPerson',
field: 'UUIDcounter',
startAt: 1,
incrementBy: 1
});
naturalPersonSchemaBr.pre('save', function(next) {
var user = this;
user.naturalPersonData.personUUID = "NP"+ user.UUIDcounter;
console.log("user",user);
var list = ['nome','sobrenome','cpf','rg'];
naturalPersonSchemaBr.methods.capitalizeFn(user.naturalPersonData,list);
return next();
});
module.exports = mongoose.model('NaturalPerson', naturalPersonSchemaBr);
import sinon from 'sinon';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);
import Bluebird from 'bluebird';
import NaturalPersonModel from './model';
import NaturalController from './controller'
const naturalPersonFixture = {};
describe('NaturalController', function() {
var stubSave,
naturalController = new NaturalController();
context('#add', function() {
beforeEach(function() {
stubSave = sinon.stub(NaturalPersonModel.prototype, "save");
});
afterEach(function() {
stubSave.restore();
});
it('resolve when data is ok', function() {
stubSave.returns(Bluebird.resolve(naturalPersonFixture.save));
return naturalController.add(naturalPersonFixture.add)
.then(function(value) {
chai.assert(value === naturalPersonFixture.save);
});
});
});
});
@fatso83
Copy link
Author

fatso83 commented Jun 21, 2017

And as suspected, dropping Bluebird completely works as well:

stubSave.resolves(); works just as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment