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

98 lines
2.6 KiB
TypeScript
Raw Normal View History

import sinon, { SinonMock } from 'sinon';
import ActivationState from 'app/services/authFlow/ActivationState';
import CompleteState from 'app/services/authFlow/CompleteState';
import ResendActivationState from 'app/services/authFlow/ResendActivationState';
2020-05-24 04:38:24 +05:30
import { bootstrap, expectState, expectNavigate, expectRun, MockedAuthContext } from './helpers';
describe('ActivationState', () => {
2020-05-24 04:38:24 +05:30
let state: ActivationState;
let context: MockedAuthContext;
let mock: SinonMock;
2020-05-24 04:38:24 +05:30
beforeEach(() => {
state = new ActivationState();
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 /activation', () => {
const expectedPath = '/activation';
context.getState.returns({
user: {
isActive: false,
},
});
2020-05-24 04:38:24 +05:30
context.getRequest.returns({ path: expectedPath });
2020-05-24 04:38:24 +05:30
expectNavigate(mock, '/activation');
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should navigate to /activation/key', () => {
const expectedPath = '/activation/sasx5AS4d61';
context.getState.returns({
user: {
isActive: false,
},
});
2016-06-05 17:46:41 +05:30
2020-05-24 04:38:24 +05:30
context.getRequest.returns({ path: expectedPath });
2020-05-24 04:38:24 +05:30
expectNavigate(mock, expectedPath);
2016-06-05 17:46:41 +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 activate with payload', () => {
const payload = {};
2020-05-24 04:38:24 +05:30
expectRun(mock, 'activate', 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, 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 transition to resend-activation', () => {
expectState(mock, ResendActivationState);
2020-05-24 04:38:24 +05:30
state.reject(context);
});
});
});