refactor(typings): migrate 'auth' state slice to TypeScript (#4698)

This commit is contained in:
Vladislav Shkodin
2020-12-13 13:35:07 +02:00
committed by GitHub
parent 5aec65fed5
commit 929ca380bb
6 changed files with 104 additions and 71 deletions

View File

@ -1,33 +0,0 @@
import Immutable from 'immutable';
import { authenticating, authenticate, authError, logout } from 'Actions/auth';
import auth from '../auth';
describe('auth', () => {
it('should handle an empty state', () => {
expect(auth(undefined, {})).toEqual(null);
});
it('should handle an authentication request', () => {
expect(auth(undefined, authenticating())).toEqual(Immutable.Map({ isFetching: true }));
});
it('should handle authentication', () => {
expect(auth(undefined, authenticate({ email: 'joe@example.com' }))).toEqual(
Immutable.fromJS({ user: { email: 'joe@example.com' } }),
);
});
it('should handle an authentication error', () => {
expect(auth(undefined, authError(new Error('Bad credentials')))).toEqual(
Immutable.Map({
error: 'Error: Bad credentials',
}),
);
});
it('should handle logout', () => {
const initialState = Immutable.fromJS({ user: { email: 'joe@example.com' } });
const newState = auth(initialState, logout());
expect(newState.get('user')).toBeUndefined();
});
});

View File

@ -0,0 +1,32 @@
import { fromJS } from 'immutable';
import { authenticating, authenticate, authError, logout } from '../../actions/auth';
import auth, { defaultState } from '../auth';
describe('auth', () => {
it('should handle an empty state', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
expect(auth(undefined, {})).toEqual(defaultState);
});
it('should handle an authentication request', () => {
expect(auth(undefined, authenticating())).toEqual(defaultState.set('isFetching', true));
});
it('should handle authentication', () => {
const user = { name: 'joe', token: 'token' };
expect(auth(undefined, authenticate(user))).toEqual(defaultState.set('user', fromJS(user)));
});
it('should handle an authentication error', () => {
expect(auth(undefined, authError(new Error('Bad credentials')))).toEqual(
defaultState.set('error', 'Error: Bad credentials'),
);
});
it('should handle logout', () => {
const user = { name: 'joe', token: 'token' };
const newState = auth(defaultState.set('user', fromJS(user)), logout());
expect(newState.get('user')).toBeUndefined();
});
});

View File

@ -1,21 +0,0 @@
import Immutable from 'immutable';
import { AUTH_REQUEST, AUTH_SUCCESS, AUTH_FAILURE, AUTH_REQUEST_DONE, LOGOUT } from 'Actions/auth';
const auth = (state = null, action) => {
switch (action.type) {
case AUTH_REQUEST:
return Immutable.Map({ isFetching: true });
case AUTH_SUCCESS:
return Immutable.fromJS({ user: action.payload });
case AUTH_FAILURE:
return Immutable.Map({ error: action.payload && action.payload.toString() });
case AUTH_REQUEST_DONE:
return state.remove('isFetching');
case LOGOUT:
return state.remove('user').remove('isFetching');
default:
return state;
}
};
export default auth;

View File

@ -0,0 +1,42 @@
import { fromJS } from 'immutable';
import { User } from 'netlify-cms-lib-util';
import {
AUTH_REQUEST,
AUTH_SUCCESS,
AUTH_FAILURE,
AUTH_REQUEST_DONE,
LOGOUT,
AuthAction,
} from '../actions/auth';
import { StaticallyTypedRecord } from '../types/immutable';
export type Auth = StaticallyTypedRecord<{
isFetching: boolean;
user: StaticallyTypedRecord<User> | undefined;
error: string | undefined;
}>;
export const defaultState = fromJS({
isFetching: false,
user: undefined,
error: undefined,
});
const auth = (state = defaultState as Auth, action: AuthAction) => {
switch (action.type) {
case AUTH_REQUEST:
return state.set('isFetching', true);
case AUTH_SUCCESS:
return state.set('user', fromJS(action.payload));
case AUTH_FAILURE:
return state.set('error', action.payload && action.payload.toString());
case AUTH_REQUEST_DONE:
return state.set('isFetching', false);
case LOGOUT:
return state.set('user', undefined).set('isFetching', false);
default:
return state;
}
};
export default auth;