Skip to content

Commit

Permalink
added #isSuspended
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Aug 6, 2014
1 parent 325603e commit d2b0008
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
24 changes: 11 additions & 13 deletions app/models/student.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ function Student(obj){
this.name = obj.name;
this.color = obj.color;
this.tests = [];
this.onHonorRoll = checkHonorRoll(this);
this.isSuspended = checkSuspended(this);
}

Object.defineProperty(Student, 'collection', {
Expand All @@ -19,7 +17,6 @@ Student.prototype.avg = function(){

var sum = this.tests.reduce(function(a,b){return a+b;});
return sum/this.tests.length;

};

Student.prototype.letter = function(){
Expand All @@ -39,6 +36,17 @@ Student.prototype.letter = function(){
}
};

Student.prototype.onHonorRoll = function(){
if(!this.tests.length){return false;}
return this.avg() > 95;
};

Student.prototype.isSuspended = function(){
if(!this.tests.length){return false;}
var numFailed = this.tests.reduce(function(a, b){return a += (b < 60) ? 1 : 0;}, 0);
return numFailed >= 3;
};

Student.prototype.save = function(cb){
Student.collection.save(this, cb);
};
Expand All @@ -51,13 +59,3 @@ function reProto(obj){
return _.create(Student.prototype, obj);
}

function checkHonorRoll(me){
if(!me.tests.length){return false;}
return me.avg() > 95;
}

function checkSuspended(me){
if(!me.tests.length){return false;}
var numFailed = me.tests.reduce(function(a, b){return a += (b < 60) ? 1 : 0;}, 0);
return numFailed >= 3;
}
37 changes: 27 additions & 10 deletions test/unit/student.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* jshint expr: true */
/* global describe, it, before*/
/* global describe, it, before, beforeEach */
'use strict';

var expect = require('chai').expect;
Expand All @@ -15,51 +15,68 @@ describe('Student', function(){
done();
});
});
beforeEach(function(done){
Student.collection.remove(function(){
s1 = new Student({name:'Jim Jones', color:'blue'});
done();
});
});
describe('Constructor', function(){
it('should create a new instance of Student with properties', function(){
s1 = new Student({name:'Jim Jones', color:'blue'});
expect(s1).to.be.instanceof(Student);
expect(s1.name).to.equal('Jim Jones');
expect(s1.color).to.equal('blue');
expect(s1.tests).to.have.length(0);
expect(s1.onHonorRoll).to.be.false;
expect(s1.isSuspended).to.be.false;
});
});
describe('#avg', function(){
it('should return the average for all tests taken', function(){
s1 = new Student({name:'Jim Jones', color:'blue'});
s1.tests = [95, 85, 75, 65, 55];
expect(s1.avg()).to.be.closeTo(75, 0.1);
});
});
describe('#letter', function(){
it('should return A', function(){
s1 = new Student({name:'Jim Jones', color:'blue'});
s1.tests = [95];
expect(s1.letter()).to.equal('A');
});
it('should return B', function(){
s1 = new Student({name:'Jim Jones', color:'blue'});
s1.tests = [85];
expect(s1.letter()).to.equal('B');
});
it('should return C', function(){
s1 = new Student({name:'Jim Jones', color:'blue'});
s1.tests = [75];
expect(s1.letter()).to.equal('C');
});
it('should return D', function(){
s1 = new Student({name:'Jim Jones', color:'blue'});
s1.tests = [65];
expect(s1.letter()).to.equal('D');
});
it('should return F', function(){
s1 = new Student({name:'Jim Jones', color:'blue'});
s1.tests = [55];
expect(s1.letter()).to.equal('F');
});
});
describe('#onHonorRoll', function(){
it('should be true if avg is above 95%', function(){
s1.tests = [98];
expect(s1.onHonorRoll()).to.be.true;
});
it('should be false if avg is below 95%', function(){
s1.tests = [90];
expect(s1.onHonorRoll()).to.be.false;
});
});
describe('#isSuspended', function(){
it('should be true if 3 tests have been failed', function(){
s1.tests = [98, 44, 55, 22, 80];
expect(s1.isSuspended()).to.be.true;
});
it('should be false if less than 3 tests have been failed', function(){
s1.tests = [90, 55, 88, 95];
expect(s1.isSuspended()).to.be.false;
});
});
describe('#save', function(){
it('should save Student into database', function(done){
var bob = new Student({name:'Bob', color:'green'});
Expand Down

0 comments on commit d2b0008

Please sign in to comment.