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

108 lines
2.9 KiB
TypeScript
Raw Normal View History

import sinon, { SinonMock } from 'sinon';
import ResendActivationState from 'app/services/authFlow/ResendActivationState';
import ActivationState from 'app/services/authFlow/ActivationState';
import RegisterState from 'app/services/authFlow/RegisterState';
2020-05-24 04:38:24 +05:30
import { bootstrap, expectState, expectNavigate, expectRun, MockedAuthContext } from './helpers';
describe('ResendActivationState', () => {
2020-05-24 04:38:24 +05:30
let state: ResendActivationState;
let context: MockedAuthContext;
let mock: SinonMock;
2020-05-24 04:38:24 +05:30
beforeEach(() => {
state = new ResendActivationState();
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 /resend-activation', () => {
context.getState.returns({
user: {
isActive: false,
},
});
2020-05-24 04:38:24 +05:30
expectNavigate(mock, '/resend-activation');
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should navigate to /resend-activation for guests', () => {
context.getState.returns({
user: {
isGuest: true,
isActive: false,
},
});
2016-05-23 09:20:10 +05:30
2020-05-24 04:38:24 +05:30
expectNavigate(mock, '/resend-activation');
2016-05-23 09:20:10 +05:30
2020-05-24 04:38:24 +05:30
state.enter(context);
});
});
2020-05-24 04:38:24 +05:30
describe('#resolve', () => {
it('should call resendActivation with payload', () => {
const payload = { email: 'foo@bar.com' };
2020-05-24 04:38:24 +05:30
expectRun(mock, 'resendActivation', sinon.match.same(payload)).returns(new Promise(() => {}));
2020-05-24 04:38:24 +05:30
state.resolve(context, payload);
});
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, ActivationState);
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 transition to activate state on reject', () => {
expectState(mock, ActivationState);
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 activation', () => {
expectState(mock, ActivationState);
2020-05-24 04:38:24 +05:30
state.goBack(context);
});
2016-05-23 09:20:10 +05:30
2020-05-24 04:38:24 +05:30
it('should transition to register if it was active previousely', () => {
expectState(mock, RegisterState);
2016-05-23 09:20:10 +05:30
2020-05-24 04:38:24 +05:30
context.prevState = new RegisterState();
state.goBack(context);
});
});
});