Skip to content

Instantly share code, notes, and snippets.

@dakk
Last active April 9, 2016 10:29
Show Gist options
  • Save dakk/c701743a6df931210af1571efe5ade35 to your computer and use it in GitHub Desktop.
Save dakk/c701743a6df931210af1571efe5ade35 to your computer and use it in GitHub Desktop.
An utility to avoid waterfalling of frisby tests throught middleware chains
var frisby = require ('frisby');
/* FrisbyChain */
var frisbyChain = function (baseData, callChain) {
callChain.push (function (data) {});
var nextPrepare = function (data, i) {
if (i < callChain.length)
return function (data, next) {
return callChain[i] (data, nextPrepare (data, i+1));
};
else
return function (data) {
return callChain[i] (data);
};
};
return nextPrepare (baseData, 0)(baseData);
};
/* Example usage */
frisbyChain ({username: 'frisbytest_gianni1', usertype: 'singleuser', email: 'frisbytest_gianni1@gmail.com',
password: 'frisbytest_gianni1'}, [
function (data, next) {
frisby.create ('/signup - create account')
.post ('http://localhost:3000/api/v1/signup', {
username: data['username'],
email: data['email'],
password: data['password'],
terms: true,
newsletter: false,
usertype: data['usertype']
}, {json: true})
.expectStatus (200)
.afterJSON ( function (json) {
next (data);
}).toss ();
},
function (data, next) {
frisby.create ('/login - correct login')
.post ('http://localhost:3000/api/v1/login', {
user: data['username'],
password: data['password'],
}, {json: true})
.expectStatus (200)
.expectJSON ({
username: data['username']
})
.expectJSONTypes({
token: String,
username: String,
expiration: Number
}).after(function(err, res, body) {
data ['token'] = body.token;
next (data);
}).toss ();
},
function (data) {
console.log (data);
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment