Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #95 from barbaraliau/add-getters
Browse files Browse the repository at this point in the history
Add getters to Credit model
  • Loading branch information
James Prestwich committed Apr 20, 2017
2 parents b405f3e + 54bddba commit dea0abe
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/models/credit.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ const Credit = new mongoose.Schema({
});

Credit.set('toObject', {
getters: true,
transform: function(doc, ret) {
delete ret.__v;
delete ret._id;
}
});

Credit.set('toJSON', {
getters: true,
transform: function(doc, ret) {
delete ret.__v;
delete ret._id;
Expand Down
65 changes: 65 additions & 0 deletions test/credit.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,71 @@ describe('Storage/models/Credit', function() {
});
});

it('should call getters', function(done) {
const newCredit = new Credit({
user: 'user123@domain.tld',
type: CREDIT_TYPES.MANUAL,
promo_code: PROMO_CODE.NEW_SIGNUP,
promo_amount: PROMO_AMOUNT.NEW_SIGNUP,
promo_expires: PROMO_EXPIRES.NEW_SIGNUP
});

newCredit.save(function(err, credit) {
if (err) {
return done(err);
}
expect(credit.toObject().promo_amount).to.equal(488);
done();
});
});

});

describe('#toJSON', function() {

it('should remove expected fields', function (done) {
const amount = 1000;

const newCredit = new Credit({
user: 'user123@domain.tld',
type: CREDIT_TYPES.MANUAL,
promo_code: PROMO_CODE.STORJ_EVENT,
promo_amount: amount,
promo_expires: PROMO_EXPIRES.DEFAULT
});

newCredit.save(function(err, credit) {
if (err) {
return done(err);
}

const creditKeys = Object.keys(credit.toJSON());
expect(creditKeys).to.not.contain('__v', '_id');
done();
});
});

it('should call getters', function (done) {
const amount = 1000;

const newCredit = new Credit({
user: 'user123@domain.tld',
type: CREDIT_TYPES.MANUAL,
promo_code: PROMO_CODE.STORJ_EVENT,
promo_amount: amount,
promo_expires: PROMO_EXPIRES.DEFAULT
});

newCredit.save(function(err, credit) {
if (err) {
return done(err);
}
expect(credit.promo_amount).to.equal(amount);
done();
});

});

});

});

0 comments on commit dea0abe

Please sign in to comment.