2016-12-07 02:38:51 +05:30
|
|
|
import sinon from 'sinon';
|
|
|
|
|
2016-03-21 11:46:37 +05:30
|
|
|
import LoginState from 'services/authFlow/LoginState';
|
|
|
|
import PasswordState from 'services/authFlow/PasswordState';
|
|
|
|
|
|
|
|
import { bootstrap, expectState, expectNavigate, expectRun } from './helpers';
|
|
|
|
|
|
|
|
describe('LoginState', () => {
|
|
|
|
let state;
|
|
|
|
let context;
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
state = new LoginState();
|
|
|
|
|
|
|
|
const data = bootstrap();
|
|
|
|
context = data.context;
|
|
|
|
mock = data.mock;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.verify();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#enter', () => {
|
|
|
|
it('should navigate to /login', () => {
|
|
|
|
context.getState.returns({
|
2016-11-13 02:01:44 +05:30
|
|
|
user: {isGuest: true},
|
|
|
|
auth: {login: null}
|
2016-03-21 11:46:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
expectNavigate(mock, '/login');
|
|
|
|
|
|
|
|
state.enter(context);
|
|
|
|
});
|
|
|
|
|
2016-11-13 02:01:44 +05:30
|
|
|
it('should transition to password if login was set', () => {
|
2016-03-21 11:46:37 +05:30
|
|
|
context.getState.returns({
|
2016-11-13 02:01:44 +05:30
|
|
|
user: {isGuest: true},
|
|
|
|
auth: {login: 'foo'}
|
2016-03-21 11:46:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
expectState(mock, PasswordState);
|
|
|
|
|
|
|
|
state.enter(context);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#resolve', () => {
|
|
|
|
it('should call login with email or username', () => {
|
|
|
|
const payload = {};
|
|
|
|
|
|
|
|
expectRun(
|
|
|
|
mock,
|
|
|
|
'login',
|
|
|
|
sinon.match.same(payload)
|
2016-12-07 02:38:51 +05:30
|
|
|
).returns(Promise.resolve());
|
2016-03-21 11:46:37 +05:30
|
|
|
|
|
|
|
state.resolve(context, payload);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should transition to password state on successfull login (first phase)', () => {
|
|
|
|
const promise = Promise.resolve();
|
|
|
|
|
|
|
|
mock.expects('run').returns(promise);
|
|
|
|
expectState(mock, PasswordState);
|
|
|
|
|
|
|
|
state.resolve(context);
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT transition to password state on fail', () => {
|
|
|
|
const promise = Promise.reject();
|
|
|
|
|
|
|
|
mock.expects('run').returns(promise);
|
|
|
|
mock.expects('setState').never();
|
|
|
|
|
|
|
|
state.resolve(context);
|
|
|
|
|
|
|
|
return promise.catch(mock.verify.bind(mock));
|
|
|
|
});
|
|
|
|
});
|
2017-01-29 21:06:21 +05:30
|
|
|
|
|
|
|
describe('#goBack', () => {
|
|
|
|
it('should return to previous page', () => {
|
|
|
|
expectRun(mock, 'goBack', '/');
|
|
|
|
|
|
|
|
state.goBack(context);
|
|
|
|
});
|
|
|
|
});
|
2016-03-21 11:46:37 +05:30
|
|
|
});
|