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

100 lines
2.7 KiB
TypeScript
Raw Normal View History

import sinon, { SinonMock } from 'sinon';
import RecoverPasswordState from 'app/services/authFlow/RecoverPasswordState';
import CompleteState from 'app/services/authFlow/CompleteState';
import LoginState from 'app/services/authFlow/LoginState';
2020-05-24 04:38:24 +05:30
import { bootstrap, expectState, expectNavigate, expectRun, MockedAuthContext } from './helpers';
describe('RecoverPasswordState', () => {
2020-05-24 04:38:24 +05:30
let state: RecoverPasswordState;
let context: MockedAuthContext;
let mock: SinonMock;
2020-05-24 04:38:24 +05:30
beforeEach(() => {
state = new RecoverPasswordState();
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 /recover-password', () => {
const expectedPath = '/recover-password';
2020-05-24 04:38:24 +05:30
context.getRequest.returns({ path: expectedPath });
2020-05-24 04:38:24 +05:30
expectNavigate(mock, expectedPath);
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should navigate to /recover-password/key', () => {
const expectedPath = '/recover-password/sasx5AS4d61';
2020-05-24 04:38:24 +05:30
context.getRequest.returns({ path: expectedPath });
2020-05-24 04:38:24 +05:30
expectNavigate(mock, expectedPath);
2020-05-24 04:38:24 +05:30
state.enter(context);
});
});
2020-05-24 04:38:24 +05:30
describe('#resolve', () => {
it('should call recoverPassword with provided payload', () => {
const expectedPayload = {
key: 123,
newPassword: '123',
newRePassword: '123',
};
2020-05-24 04:38:24 +05:30
expectRun(mock, 'recoverPassword', sinon.match(expectedPayload)).returns(new Promise(() => {}));
2020-05-24 04:38:24 +05:30
state.resolve(context, expectedPayload);
});
2020-05-24 04:38:24 +05:30
it('should transition to complete state on success', () => {
const promise = Promise.resolve();
2020-05-24 04:38:24 +05:30
mock.expects('run').returns(promise);
expectState(mock, CompleteState);
2020-05-24 04:38:24 +05:30
state.resolve(context, {});
2020-05-24 04:38:24 +05:30
return promise;
});
2020-05-24 04:38:24 +05:30
it('should NOT transition to complete state on fail', () => {
const promise = Promise.reject();
2020-05-24 04:38:24 +05:30
mock.expects('run').returns(promise);
mock.expects('setState').never();
2020-05-24 04:38:24 +05:30
state.resolve(context, {});
2020-05-24 04:38:24 +05:30
return promise.catch(mock.verify.bind(mock));
});
});
2020-05-24 04:38:24 +05:30
describe('#reject', () => {
it('should run contactUs popup', () => {
expectRun(mock, 'contactUs');
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);
});
});
});