accounts-frontend/packages/app/services/authFlow/ForgotPasswordState.test.ts

91 lines
2.6 KiB
TypeScript
Raw Normal View History

import sinon, { SinonMock } from 'sinon';
2017-04-15 12:05:26 +05:30
import ForgotPasswordState from 'app/services/authFlow/ForgotPasswordState';
import RecoverPasswordState from 'app/services/authFlow/RecoverPasswordState';
import LoginState from 'app/services/authFlow/LoginState';
2020-05-24 04:38:24 +05:30
import { bootstrap, expectState, expectNavigate, expectRun, MockedAuthContext } from './helpers';
describe('ForgotPasswordState', () => {
2020-05-24 04:38:24 +05:30
let state: ForgotPasswordState;
let context: MockedAuthContext;
let mock: SinonMock;
2020-05-24 04:38:24 +05:30
beforeEach(() => {
state = new ForgotPasswordState();
2020-05-24 04:38:24 +05:30
const data = bootstrap();
context = data.context;
mock = data.mock;
});
2020-05-24 04:38:24 +05:30
afterEach(() => {
mock.verify();
});
2020-05-24 04:38:24 +05:30
describe('#enter', () => {
it('should navigate to /forgot-password if login set', () => {
expectNavigate(mock, '/forgot-password');
2020-05-24 04:38:24 +05:30
state.enter(context);
});
});
2020-05-24 04:38:24 +05:30
describe('#resolve', () => {
it('should call forgotPassword with email from payload', () => {
const expectedLogin = 'foo@bar.com';
2020-05-24 04:38:24 +05:30
expectRun(
mock,
'forgotPassword',
sinon.match({
login: expectedLogin,
}),
).returns(new Promise(() => {}));
state.resolve(context, { login: expectedLogin, captcha: '' });
2020-05-24 04:38:24 +05:30
});
2020-05-24 04:38:24 +05:30
it('should transition to recoverPassword state on success', () => {
const promise = Promise.resolve();
const expectedLogin = 'foo@bar.com';
2020-05-24 04:38:24 +05:30
mock.expects('run').twice().returns(promise);
expectState(mock, RecoverPasswordState);
state.resolve(context, { login: expectedLogin, captcha: '' });
2020-05-24 04:38:24 +05:30
return promise;
});
2020-05-24 04:38:24 +05:30
it('should run setLogin on success', () => {
const promise = Promise.resolve();
const expectedLogin = 'foo@bar.com';
2020-05-24 04:38:24 +05:30
mock.expects('run').withArgs('forgotPassword').returns(promise);
expectState(mock, RecoverPasswordState);
mock.expects('run').withArgs('setLogin', expectedLogin);
state.resolve(context, { login: expectedLogin, captcha: '' });
2020-05-24 04:38:24 +05:30
return promise;
});
});
2020-05-24 04:38:24 +05:30
describe('#reject', () => {
it('should navigate to /send-message', () => {
expectState(mock, RecoverPasswordState);
2020-05-24 04:38:24 +05:30
state.reject(context);
});
});
2020-05-24 04:38:24 +05:30
describe('#goBack', () => {
it('should transition to login state', () => {
expectState(mock, LoginState);
2020-05-24 04:38:24 +05:30
state.goBack(context);
});
});
});