Last active
August 29, 2015 14:10
-
-
Save jorinvo/ae895c5b60581bba6dc1 to your computer and use it in GitHub Desktop.
example syntax for declarative aync testing style (answer to http://blog.oozou.com/stateful-context-working-with-multiple-values-in-a-promise-chain/)
This file contains 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
describe('following another user', function() { | |
declare({ | |
// get rid of small functions with partials | |
firstUser: partial(createUser, 'foo'), | |
secondUser: partial(createUser, 'bar') | |
// no need for function introspection with this syntax | |
// get knows strings describe dependencies and function are async | |
// getTimeline is a function that accepts a user as arugment now | |
timeline: partial(get, 'firstUser', getTimeline), | |
// alternative syntax with less smart `get` could be: | |
timeline2: function() { | |
return get('firstUser').then(getTimeline); | |
}, | |
// `updateStatus` | |
// is aware of currying and returns as a partial | |
// until it has all arguments it needs | |
// | |
// with a less smart function we need to partial apply it first: | |
// partial(updateStatus, 'My status') | |
status: partial(get, 'secondUser', updateStatus('My status') ), | |
// alternative syntax with less smart `get` could be: | |
status2: function() { | |
return get('secondUser').then( updateStatus('My status') ); | |
} | |
}); | |
beforeEach(function(done) { | |
// `get` can also return promises for chaining | |
// when we want to invoke it right away | |
get('firstUser', 'secondUser') | |
// `follow` takes two users | |
// we can solve this by making `get` | |
// return an array or object of users | |
.then(follow) | |
.then(done); | |
}); | |
it('should display the status of B in A\'s timeline when A follows B', function(done) { | |
get('timeline', 'status') | |
.then(expectTimelineToContainStatus) | |
.then(done); | |
}); | |
}); |
This file contains 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
describe('following another user', function() { | |
declare({ | |
firstUser: partial(createUser, 'foo'), | |
secondUser: partial(createUser, 'bar') | |
timeline: partial(get, 'firstUser', getTimeline), | |
status: partial(get, 'secondUser', updateStatus('My status') ), | |
}); | |
beforeEach(function(done) { | |
get('firstUser', 'secondUser') | |
.then(follow) | |
.then(done); | |
}); | |
it('should display the status of B in A\'s timeline when A follows B', function(done) { | |
get('timeline', 'status') | |
.then(expectTimelineToContainStatus) | |
.then(done); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment