Last active
August 5, 2019 17:29
-
-
Save rkotze/77aba69955dd6d97abf5 to your computer and use it in GitHub Desktop.
First example on how to unit test RefluxJS and superagent with SinonJS
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
import myStore from './my.store'; | |
import myAction from './my.action'; | |
import superagent from 'superagent'; | |
import sinon from 'sinon'; | |
import should from 'should'; | |
import shouldSinon from 'should-sinon'; | |
describe('Api that does stuff', () => { | |
let putRequest, | |
successTrigger, | |
requestId = 'fake-guid'; | |
const URL = `api/call/${requestId}`; | |
// setup spy/stub before running tests | |
before(() => { | |
successTrigger = sinon.spy(myStore, 'trigger'); | |
putRequest = sinon.stub(superagent, 'put'); | |
}); | |
// clean up your sinon spys/stubs/mocks | |
after(() => { | |
putRequest.restore(); | |
successTrigger.restore(); | |
}); | |
// Reset to put state back for next test | |
afterEach(() => { | |
successTrigger.reset(); | |
}); | |
it('handle successful response from api', () => { | |
// stub returns a fake response by replacing the end function callback | |
putRequest.returns({ | |
end: (cb) => { | |
cb(null, {ok: true, body: { "status" : "OK" }}); | |
} | |
}); | |
myStore.onServiceCall(requestId); | |
// get the params of callback containing the fake response body | |
let response = myStore.trigger.getCall(0).args[0]; | |
// ensure code follows the correct path indicated by trigger being called | |
successTrigger.should.be.calledOnce(); | |
// url constructed correctly and query expected endpoint | |
putRequest.should.be.calledWith(URL); | |
// check expected value for response body status | |
response.status.should.eql('OK'); | |
}); | |
}); |
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
'use strict'; | |
import Reflux from 'reflux'; | |
import myActions from './my.action'; | |
import request from 'superagent'; | |
let myStore = Reflux.createStore({ | |
listenables: myActions, | |
// function for action defined in my actions | |
onServiceCall(requestId) { | |
const URL = `api/call/${requestId}`; | |
request | |
.put(URL) | |
.end((error, response) => { | |
//successfull api response will send data to component | |
if(response && response.ok) { | |
this.trigger(response.body); | |
}else{ | |
//trigger a failed callback if expected response it no OK | |
myActions.serviceCall.failed('error message'); | |
} | |
}); | |
} | |
}); | |
export default myStore; |
Nevermind, I was using spy when I should be using stub.
I have a problem if I have a "setting" but not a callback. My request is :
const request: _request.SuperAgentRequest = _request .get(route) .set('Accept', 'application/json') .send();
I try 2 different stub :
my #1 stub is:
getRequest = sinon.stub(superagent, 'get');
getRequest.returns({ set: () => '' });
Here's the error:
TypeError: _request.get(...).set(...).send is not a function
my #2 stub is:
getRequest = sinon.stub(superagent, 'get');
getRequest.returns({ ' });
Here's the error:
TypeError: _request.get(...) is not a function
How can I add my "SET" in this stub ?
thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this out of date? I'm getting
postReq.returns is not a function