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

110 lines
2.9 KiB
TypeScript
Raw Normal View History

import AcceptRulesState from 'app/services/authFlow/AcceptRulesState';
import CompleteState from 'app/services/authFlow/CompleteState';
import { SinonMock } from 'sinon';
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
import { bootstrap, expectState, expectNavigate, expectRun, MockedAuthContext } from './helpers';
2016-08-03 00:29:29 +05:30
describe('AcceptRulesState', () => {
2020-05-24 04:38:24 +05:30
let state: AcceptRulesState;
let context: MockedAuthContext;
let mock: SinonMock;
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
beforeEach(() => {
state = new AcceptRulesState();
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
const data = bootstrap();
context = data.context;
mock = data.mock;
});
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
afterEach(() => {
mock.verify();
2016-08-03 00:29:29 +05:30
});
2020-05-24 04:38:24 +05:30
describe('#enter', () => {
it('should navigate to /accept-rules', () => {
context.getState.returns({
user: {
shouldAcceptRules: true,
isGuest: false,
},
});
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
expectNavigate(mock, '/accept-rules');
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
it('should transition to complete state if rules accepted', () => {
context.getState.returns({
user: {
shouldAcceptRules: false,
isGuest: false,
},
});
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
expectState(mock, CompleteState);
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
state.enter(context);
});
it('should transition to complete state if account is deleted even if user should accept rules', () => {
context.getState.returns({
user: {
shouldAcceptRules: true,
isGuest: false,
isDeleted: true,
},
});
expectState(mock, CompleteState);
state.enter(context);
});
});
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
describe('#resolve', () => {
it('should call acceptRules', () => {
expectRun(mock, 'acceptRules').returns(new Promise(() => {}));
state.resolve(context);
});
it('should transition to complete state on success', () => {
const promise = Promise.resolve();
mock.expects('run').returns(promise);
expectState(mock, CompleteState);
state.resolve(context);
return promise;
});
it('should NOT transition to complete state on fail', () => {
const promise = Promise.reject();
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
mock.expects('run').returns(promise);
mock.expects('setState').never();
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
state.resolve(context);
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
return promise.catch(mock.verify.bind(mock));
});
2016-08-03 00:29:29 +05:30
});
2020-05-24 04:38:24 +05:30
describe('#reject', () => {
it('should logout', () => {
expectRun(mock, 'logout');
2016-08-03 00:29:29 +05:30
state.reject(context, {});
});
it('should navigate to the account deletion page', () => {
expectNavigate(mock, '/profile/delete');
state.reject(context, { deleteAccount: true });
2020-05-24 04:38:24 +05:30
});
2016-08-03 00:29:29 +05:30
});
});