Last active
January 11, 2018 11:02
-
-
Save i-hardy/19c85b60c2e6cde8a00405664051dba5 to your computer and use it in GitHub Desktop.
Unit-testing Axios CancelToken behaviour
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
const tableCanceller = new Cancellation(); | |
... | |
export const fetchTableData = (store, payload) => { | |
tableCanceller.check('Table API request cancelled'); | |
return axios | |
.post(`${payload.profileId}/report/${payload.reportId}/tabular`, payload.body, { | |
params: { | |
fromDate: payload.body.fromDate, | |
toDate: payload.body.toDate, | |
}, | |
cancelToken: new CancelToken(function executor(cancel) { | |
tableCanceller.create(cancel); | |
}), | |
}) | |
.then(response => { | |
tableCanceller.restore(); | |
return store.commit('SET_REPORT_TABLE_DATA', response.data.result); | |
}) | |
.catch(cancel => { | |
return cancel.message; | |
}); | |
}; |
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
beforeEach(() => { | |
mockStore = { | |
commit: sinon.stub(), | |
dispatch: sinon.stub(), | |
}; | |
fakeResponse = { headers: { authorization: 'Bearer' }, response: { result: {} } }; | |
moxios.install(http); | |
moxios.stubRequest(/.*/, fakeResponse); | |
}); | |
describe('#fetchTableData', () => { | |
... | |
it('will be cancelled if a second request is issued before the request completes', done => { | |
const mockPayload = { | |
profileId: 'realProfile', | |
reportId: 'verySeriousReport', | |
body: { fromDate: 'yesterday', toDate: 'today' }, | |
}; | |
const cancelledRequest = fetchTableData(mockStore, mockPayload); | |
fetchTableData(mockStore, mockPayload).then(async () => { | |
const cancelMessage = await cancelledRequest; | |
expect(cancelMessage).to.equal('Table API request cancelled'); | |
done(); | |
}); | |
}); | |
}); |
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
class Cancellation { | |
constructor() { | |
this.runningCall = null; | |
} | |
create(func) { | |
this.runningCall = func; | |
} | |
restore() { | |
this.runningCall = null; | |
} | |
check(message) { | |
if (this.runningCall) { | |
this.runningCall(message); | |
} | |
} | |
} | |
export { Cancellation as default }; |
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
describe('Cancellation', () => { | |
let dummyFunc; | |
let cancellation; | |
beforeEach(() => { | |
dummyFunc = sinon.stub(); | |
cancellation = new Cancellation(); | |
}); | |
describe('#create', () => { | |
it('sets the passed function to the runningCall property', () => { | |
cancellation.create(dummyFunc); | |
expect(cancellation.runningCall).to.equal(dummyFunc); | |
}); | |
}); | |
describe('#restore', () => { | |
it('sets the runningCall property back to null', () => { | |
cancellation.create(dummyFunc); | |
cancellation.restore(); | |
expect(cancellation.runningCall).to.be.null; | |
}); | |
}); | |
describe('#check', () => { | |
it('executes runningCall if it has been set', () => { | |
cancellation.create(dummyFunc); | |
cancellation.check('Down with this sort of thing'); | |
expect(dummyFunc).calledWith('Down with this sort of thing'); | |
}); | |
it('otherwise does nothing', () => { | |
cancellation.create(dummyFunc); | |
cancellation.restore(); | |
cancellation.check('Down with this sort of thing'); | |
expect(dummyFunc).not.called; | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment