Skip to content

Instantly share code, notes, and snippets.

@rkotze
Last active August 5, 2019 17:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkotze/77aba69955dd6d97abf5 to your computer and use it in GitHub Desktop.
Save rkotze/77aba69955dd6d97abf5 to your computer and use it in GitHub Desktop.
First example on how to unit test RefluxJS and superagent with SinonJS
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');
});
});
'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;
@natdm
Copy link

natdm commented May 11, 2017

Is this out of date? I'm getting postReq.returns is not a function

var postReq;

beforeEach(() => {
    postReq = sinon.spy(superagent, 'post');

    window.localStorage = storageMock();
    window.localStorage.setItem('id_token', 'test-token');
});
it('should test addEventAccessAsync', () => {
        postReq.returns({
            end: cb => {
                cb('error');
            },
        });
        var cb = sinon.spy();
        r.addEventAccessAsync(1, 'test', {})(cb);
        console.log(cb.args);
    });

@natdm
Copy link

natdm commented May 11, 2017

Nevermind, I was using spy when I should be using stub.

@BaronMing1
Copy link

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