Last active
August 29, 2015 14:11
-
-
Save efleming969/dd816a20ce3323c849f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var expect = chai.expect; | |
describe( 'test double examples', function () { | |
it ( 'should calculate the proper value', function () { | |
var ds = new DataService(); | |
var x = new Foo( ds ); | |
sinon.stub( ds, "lookupMultiplier" ).returns( 2 ); | |
var rst = x.calculateValueBasedOnLookup( 5 ); | |
expect( rst ).to.equal( 10 ); | |
} ); | |
it ( 'should send an email', function () { | |
var es = new EmailService(); | |
var bar = new Bar( es ); | |
var expectation = sinon.mock( es ) | |
.expects( "send" ).withArgs( "hello" ); | |
bar.somethingThatSendsAnEmail(); | |
expectation.verify(); | |
} ); | |
}); | |
describe( 'referntial transparency', function () { | |
var highScopedValue = 0; | |
it ( 'may NOT return the same value given the same input', function () { | |
var plusHighScopedValue = function ( i ) { | |
return highScopedValue + i; | |
}; | |
var rst1 = plusHighScopedValue( 1 ); | |
expect( rst1 ).to.equal( 1 ); | |
highScopedValue = 1; | |
var rst2 = plusHighScopedValue( 1 ); | |
expect( rst2 ).to.equal( 2 ); | |
} ); | |
it ( 'will always return the same value given the same input', function () { | |
var plusOne = function ( i ) { | |
return i + 1; | |
}; | |
var rst = plusOne( 1 ); | |
expect( rst ).to.equal( 2 ); | |
} ); | |
}); | |
describe( 'higher order functions', function () { | |
it ( 'can pass a function to another function', function () { | |
var plusOne = function ( i ) { | |
return i + 1; | |
}; | |
var higherOrderFunction = function ( fn, value ) { | |
return fn ( value ) * 2 | |
}; | |
var rst = higherOrderFunction ( plusOne, 2 ); | |
expect( rst ).to.equal( 6 ); | |
} ); | |
it ( 'can count items based on any boolean function', function () { | |
var countIf = function ( array, condition ) { | |
var total = 0; | |
for ( var i = 0; i < array.length; i++ ) { | |
if ( condition ( array[ i ] ) ) { | |
total++; | |
} | |
} | |
return total; | |
}; | |
var startsWithTheLeterN = function ( x ) { | |
return x.substring( 0, 1 ) === "N"; | |
}; | |
var names = [ "Nicole", "Niko", "Zola", "Erick" ]; | |
var rst = countIf ( names, startsWithTheLeterN ); | |
expect( rst ).to.equal( 2 ); | |
} ); | |
}); | |
describe( 'functional array operations', function () { | |
it ( 'should filter and map a list of names', function () { | |
var names = ["nicole", "niko", "zola", "erick"]; | |
var startsWith = function ( x ) { | |
return function ( y ) { return y.substring(0,1) === x } | |
}; | |
var toUpperCase = function (x) { | |
return x.toUpperCase() | |
}; | |
var rst = names.filter(startsWith("n")).map(toUpperCase) | |
expect(rst).to.deep.equal(["NICOLE", "NIKO"]) | |
} ) | |
it ( 'can reduce a list of values into one', function () { | |
var numbers = [ 1, 2, 3 ]; | |
var plusOne = function ( x ) { return x + 1; }; | |
var sum = function ( a, b ) { return a + b; }; | |
var rst = numbers.map( plusOne ).reduce( sum ); | |
expect( rst ).to.equal( 9 ); | |
} ) | |
} ); | |
describe( 'function composition', function () { | |
it ( 'combines multiple functions to create on one', function () { | |
var greet = function( name ){ return "whatup, " + name; } | |
var exclaim = function( statement ) { | |
return statement.toUpperCase() + "!"; | |
}; | |
var exclaimedGreet = compose(greet, exclaim); | |
var rst = exclaimedGreet("moe") | |
expect(rst).to.equal("whatup, MOE!") | |
} ); | |
function compose ( f, g ) { | |
return function ( value ) { return f ( g ( value ) ) }; | |
} | |
}); | |
function DataService () { | |
} | |
DataService.prototype.lookupMultiplier = function () {}; | |
function Foo ( dataService ) { | |
this.dataService = dataService; | |
} | |
Foo.prototype.calculateValueBasedOnLookup = function ( value ) { | |
return value * this.dataService.lookupMultiplier(); | |
} | |
function EmailService () { | |
} | |
EmailService.prototype.send = function () {}; | |
function Bar( emailService ) { | |
this.emailService = emailService; | |
} | |
Bar.prototype.somethingThatSendsAnEmail = function ( ) { | |
this.emailService.send( 'hello' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment