-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Chore: Migrate redux module login to typescript #3647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
22feb7b
f1f5a92
17ebeb7
a529e8b
c5c9765
ee9a57f
b1605a0
0249d1d
839f406
7b69a7b
e1e1312
3588caf
df69c3a
4dec86d
3cf12fe
d931685
e297ae2
cc8f684
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { Action } from 'redux'; | ||
|
|
||
| import { IUserLogin } from '../reducers/login'; | ||
| import * as types from './actionsTypes'; | ||
|
|
||
| interface ICredentials { | ||
| resume: string; | ||
| user: string; | ||
| password: string; | ||
| } | ||
|
|
||
| interface ILoginRequest extends Action { | ||
| credentials: any; | ||
| logoutOnError?: boolean; | ||
| isFromWebView?: boolean; | ||
| } | ||
|
|
||
| interface ILoginSuccess extends Action { | ||
| user: Partial<IUserLogin>; | ||
| } | ||
|
|
||
| interface ILoginFailure extends Action { | ||
| err: Partial<IUserLogin>; | ||
| } | ||
|
|
||
| interface ILogout extends Action { | ||
| forcedByServer: boolean; | ||
| } | ||
|
|
||
| interface ISetUser extends Action { | ||
| user: Partial<IUserLogin>; | ||
| } | ||
|
|
||
| interface ISetServices extends Action { | ||
| data: Record<string, string>; | ||
| } | ||
|
|
||
| interface ISetPreference extends Action { | ||
| preference: Record<string, any>; | ||
| } | ||
|
|
||
| interface ISetLocalAuthenticated extends Action { | ||
| isLocalAuthenticated: boolean; | ||
| } | ||
|
|
||
| export type TActionsLogin = ILoginRequest & | ||
| ILoginSuccess & | ||
| ILoginFailure & | ||
| ILogout & | ||
| ISetUser & | ||
| ISetServices & | ||
| ISetPreference & | ||
| ISetLocalAuthenticated; | ||
|
|
||
| export function loginRequest( | ||
| credentials: Partial<ICredentials>, | ||
| logoutOnError?: boolean, | ||
| isFromWebView?: boolean | ||
| ): ILoginRequest { | ||
| return { | ||
| type: types.LOGIN.REQUEST, | ||
| credentials, | ||
| logoutOnError, | ||
| isFromWebView | ||
| }; | ||
| } | ||
|
|
||
| export function loginSuccess(user: Partial<IUserLogin>): ILoginSuccess { | ||
| return { | ||
| type: types.LOGIN.SUCCESS, | ||
| user | ||
| }; | ||
| } | ||
|
|
||
| export function loginFailure(err: Record<string, any>): ILoginFailure { | ||
| return { | ||
| type: types.LOGIN.FAILURE, | ||
| err | ||
| }; | ||
| } | ||
|
|
||
| export function logout(forcedByServer = false): ILogout { | ||
| return { | ||
| type: types.LOGOUT, | ||
| forcedByServer | ||
| }; | ||
| } | ||
|
|
||
| export function setUser(user: Partial<IUserLogin>): ISetUser { | ||
| return { | ||
| type: types.USER.SET, | ||
| user | ||
| }; | ||
| } | ||
|
|
||
| export function setLoginServices(data: Record<string, any>): ISetServices { | ||
| return { | ||
| type: types.LOGIN.SET_SERVICES, | ||
| data | ||
| }; | ||
| } | ||
|
|
||
| export function setPreference(preference: Record<string, any>): ISetPreference { | ||
| return { | ||
| type: types.LOGIN.SET_PREFERENCE, | ||
| preference | ||
| }; | ||
| } | ||
|
|
||
| export function setLocalAuthenticated(isLocalAuthenticated: boolean): ISetLocalAuthenticated { | ||
| return { | ||
| type: types.LOGIN.SET_LOCAL_AUTHENTICATED, | ||
| isLocalAuthenticated | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { | ||
| loginFailure, | ||
| loginRequest, | ||
| loginSuccess, | ||
| logout, | ||
| setLocalAuthenticated, | ||
| setLoginServices, | ||
| setUser | ||
| } from '../actions/login'; | ||
| import { initialState } from './login'; | ||
| import { mockedStore } from './mockedStore'; | ||
|
|
||
| describe('test selectedUsers reducer', () => { | ||
| it('should return initial state', () => { | ||
| const state = mockedStore.getState().login; | ||
| expect(state).toEqual(initialState); | ||
| }); | ||
|
|
||
| it('should return modified store after loginRequest', () => { | ||
| mockedStore.dispatch(loginRequest({ user: 'carlitos@email.com', password: '123456' })); | ||
| const state = mockedStore.getState().login; | ||
| expect(state).toEqual({ ...initialState, isFetching: true, isAuthenticated: false, failure: false, error: {} }); | ||
| }); | ||
|
|
||
| it('should return modified store after loginFailure', () => { | ||
| mockedStore.dispatch(loginFailure({ error: 'error' })); | ||
| const state = mockedStore.getState().login.error.error; | ||
| expect(state).toEqual('error'); | ||
| }); | ||
|
|
||
| it('should return modified store after loginSuccess', () => { | ||
| const user = { | ||
| id: 'ajhsiahsa', | ||
| token: 'asdasdasdas', | ||
| username: 'carlitos', | ||
| name: 'Carlitos', | ||
| customFields: { | ||
| phonenumber: '' | ||
| }, | ||
| emails: [ | ||
| { | ||
| address: 'carlitos@email.com', | ||
| verified: true | ||
| } | ||
| ], | ||
| roles: ['user'], | ||
| isFromWebView: false, | ||
| showMessageInMainThread: false, | ||
| enableMessageParserEarlyAdoption: false | ||
| }; | ||
| mockedStore.dispatch(loginSuccess(user)); | ||
| const state = mockedStore.getState().login.user; | ||
| expect(state).toEqual(user); | ||
| }); | ||
|
|
||
| it('should return modified store after setUser', () => { | ||
| const user = { | ||
| id: 'ajhsiahsa', | ||
| token: 'asdasdasdas', | ||
| username: 'carlito', | ||
| name: 'Carlitos', | ||
| customFields: { | ||
| phonenumber: '' | ||
| }, | ||
| emails: [ | ||
| { | ||
| address: 'carlitos@email.com', | ||
| verified: true | ||
| } | ||
| ], | ||
| roles: ['user'], | ||
| isFromWebView: false, | ||
| showMessageInMainThread: false, | ||
| enableMessageParserEarlyAdoption: false | ||
| }; | ||
| mockedStore.dispatch(setUser(user)); | ||
| const state = mockedStore.getState().login.user.username; | ||
| expect(state).toEqual(user.username); | ||
| }); | ||
|
|
||
| // TODO PREFERENCE REDUCER WITH EMPTY PREFERENCE - NON USED? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whats wrong here? Is missing something or we can remove this block of code?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This action doesn't work, but I'm not sure if it's because at login this object is set or if it doesn't work at all. |
||
| // it('should return modified store after setPreference', () => { | ||
| // mockedStore.dispatch(setPreference({ showAvatar: true })); | ||
| // const state = mockedStore.getState().login; | ||
| // console.log(state); | ||
| // expect(state).toEqual('error'); | ||
| // }); | ||
|
|
||
| it('should return modified store after setLocalAuthenticated', () => { | ||
| mockedStore.dispatch(setLocalAuthenticated(true)); | ||
| const state = mockedStore.getState().login.isLocalAuthenticated; | ||
| expect(state).toEqual(true); | ||
| }); | ||
|
|
||
| it('should return modified store after setLoginServices', () => { | ||
| mockedStore.dispatch(setLoginServices({ facebook: { clientId: 'xxx' } })); | ||
| const state = mockedStore.getState().login.services.facebook.clientId; | ||
| expect(state).toEqual('xxx'); | ||
| }); | ||
|
|
||
| it('should return modified store after logout', () => { | ||
| mockedStore.dispatch(logout()); | ||
| const state = mockedStore.getState().login; | ||
| expect(state).toEqual(initialState); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.