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

91 lines
2.5 KiB
TypeScript
Raw Normal View History

import expect from 'app/test/unexpected';
import sinon, { SinonMock } from 'sinon';
import { Account } from 'app/components/accounts';
import ChooseAccountState from 'app/services/authFlow/ChooseAccountState';
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';
const mockAccount: Account = {
id: 1,
username: '',
email: '',
token: '',
refreshToken: '',
isDeleted: false,
};
describe('ChooseAccountState', () => {
2020-05-24 04:38:24 +05:30
let state: ChooseAccountState;
let context: MockedAuthContext;
let mock: SinonMock;
2020-05-24 04:38:24 +05:30
beforeEach(() => {
state = new ChooseAccountState();
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 /oauth/choose-account', () => {
context.getState.returns({
auth: {
oauth: {},
},
});
2020-05-24 04:38:24 +05:30
expectNavigate(mock, '/oauth/choose-account');
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should navigate to /choose-account if not oauth', () => {
context.getState.returns({
auth: {
oauth: null,
},
});
2020-05-24 04:38:24 +05:30
expectNavigate(mock, '/choose-account');
2020-05-24 04:38:24 +05:30
state.enter(context);
});
});
2020-05-24 04:38:24 +05:30
describe('#resolve', () => {
it('should transition to complete if an existing account was chosen', () => {
expectRun(mock, 'authenticate', sinon.match(mockAccount)).returns(Promise.resolve());
expectRun(mock, 'setAccountSwitcher', false);
2020-05-24 04:38:24 +05:30
expectState(mock, CompleteState);
return expect(state.resolve(context, mockAccount), 'to be fulfilled');
2020-05-24 04:38:24 +05:30
});
2020-05-24 04:38:24 +05:30
it('should transition to login if user wants to add new account', () => {
expectNavigate(mock, '/login');
expectRun(mock, 'setLogin', null);
expectState(mock, LoginState);
// Assert nothing returned
// @ts-ignore
return expect(state.resolve(context, {}), 'to be undefined');
2020-05-24 04:38:24 +05:30
});
});
2020-05-24 04:38:24 +05:30
describe('#reject', () => {
it('should logout', () => {
expectRun(mock, 'logout');
2020-05-24 04:38:24 +05:30
state.reject(context);
});
});
});