Tests for AuthFlow.handleRequest callback arg

This commit is contained in:
SleepWalker
2016-06-03 22:10:47 +03:00
parent 6e9ac75184
commit aa833cd495
2 changed files with 54 additions and 5 deletions

View File

@@ -38,7 +38,7 @@ describe('AuthFlow', () => {
expect(flow.state).to.be.equal(state);
});
it('should call `enter` on new state and pass reference to itself', () => {
it('should call #enter() on new state and pass reference to itself', () => {
const state = new AbstractState();
const spy = sinon.spy(state, 'enter');
@@ -62,6 +62,17 @@ describe('AuthFlow', () => {
sinon.assert.notCalled(spy2);
});
it('should return promise, if #enter returns it', () => {
const state = new AbstractState();
const expected = Promise.resolve();
state.enter = () => expected;
const actual = flow.setState(state);
expect(actual).to.be.equal(expected);
});
it('should throw if no state', () => {
expect(() => flow.setState()).to.throw('State is required');
});
@@ -184,6 +195,32 @@ describe('AuthFlow', () => {
sinon.assert.calledWithExactly(flow.run, 'setOAuthRequest', {});
});
it('should call callback', () => {
const callback = sinon.stub();
flow.handleRequest('/', function() {}, callback);
sinon.assert.calledOnce(callback);
});
it('should not call callback till returned from #enter() promise will be resolved', () => {
let resolve;
const promise = {then: (cb) => {resolve = cb}};
const callback = sinon.stub();
const state = new AbstractState();
state.enter = () => promise;
flow.setState = AuthFlow.prototype.setState.bind(flow, state);
flow.handleRequest('/', function() {}, callback);
expect(resolve).to.be.a('function');
sinon.assert.notCalled(callback);
resolve();
sinon.assert.calledOnce(callback);
});
it('throws if unsupported request', () => {
expect(() => flow.handleRequest('/foo/bar')).to.throw('Unsupported request: /foo/bar');
});