-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rebase: github login * chore: update snapshots & add [thunk] to TopBar.test.js * chore: update snapshots & add [thunk] to TopBar.test.js * chore: add auth.reducer tests * chore: auth.actions specs, part I * Fix tests and packege-lock * Bugfix
- Loading branch information
Showing
19 changed files
with
1,065 additions
and
50 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import configureMockStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import { LOGIN_REQUEST, LOGIN_REQUEST_ERROR, LOGIN_REQUEST_SUCCESS } from '../action_types'; | ||
import { initialState } from '../../reducers/auth.reducer'; | ||
import { loginError, loginPending, loginSuccess, requestLogin } from '../auth.actions'; | ||
import { GithubUserModel } from '../../shared/models/GithubUserModel'; | ||
import { AuthService } from '../../shared/services/AuthService'; | ||
|
||
const middlewares = [thunk]; | ||
const mockStore = configureMockStore(middlewares); | ||
|
||
describe('auth action creators', () => { | ||
|
||
describe('when login request is called', () => { | ||
const expectedAction = { | ||
type: LOGIN_REQUEST | ||
}; | ||
it('should create action', () => { | ||
const action = loginPending(); | ||
expect(action).toEqual(expectedAction); | ||
}); | ||
}); | ||
|
||
describe('when "user logged" action is called', () => { | ||
const user = GithubUserModel.fromOAuth0({}); | ||
const expectedAction = { | ||
type: LOGIN_REQUEST_SUCCESS, | ||
payload: user | ||
}; | ||
it('should create action', () => { | ||
const action = loginSuccess(user); | ||
expect(action).toEqual(expectedAction); | ||
}); | ||
}); | ||
|
||
describe('when "user logged: finished with error', () => { | ||
const error = new Error('Error'); | ||
const expectedAction = { | ||
type: LOGIN_REQUEST_ERROR, | ||
payload: error | ||
}; | ||
it('should create action', () => { | ||
const action = loginError(error); | ||
expect(action).toEqual(expectedAction); | ||
}); | ||
}); | ||
|
||
describe('when login request action is called', () => { | ||
|
||
let store; | ||
let serviceMock; | ||
|
||
beforeEach(() => { | ||
store = mockStore({ auth: initialState }); | ||
}); | ||
|
||
describe('when AuthService returns an error', () => { | ||
const fakeError = new Error('Error'); | ||
|
||
beforeEach(() => { | ||
serviceMock = jest.spyOn(AuthService, 'signIn').mockImplementation(() => Promise.reject(fakeError)); | ||
store.dispatch(requestLogin()); | ||
}); | ||
|
||
afterEach(() => { | ||
serviceMock.mockRestore(); | ||
}); | ||
|
||
it('should dispatch LOGIN_REQUEST', () => { | ||
expect(store.getActions()[0].type).toBe(LOGIN_REQUEST); | ||
}); | ||
|
||
it('should create LOGIN_REQUEST_ERROR action', () => { | ||
expect(store.getActions()[1].type).toBe(LOGIN_REQUEST_ERROR); | ||
expect(store.getActions()[1].payload).toBe(fakeError); | ||
}); | ||
}); | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,87 @@ | ||
import { LOGIN, LOGOUT } from '../actions/action_types'; | ||
import { AuthService } from '../shared/services/AuthService'; | ||
import { | ||
LOGIN_REQUEST, LOGIN_REQUEST_ERROR, LOGIN_REQUEST_SUCCESS, LOGIN_RESTORE_SESSION_REQUEST, LOGOUT_REQUEST, | ||
LOGOUT_REQUEST_ERROR, | ||
LOGOUT_REQUEST_SUCCESS | ||
} from './action_types'; | ||
|
||
export const login = () => { | ||
export const requestLogin = () => { | ||
return (dispatch) => { | ||
dispatch(loginPending()); | ||
AuthService.signIn().then(result => { | ||
dispatch(loginSuccess(result)); | ||
}).catch(error => { | ||
dispatch(loginError(error)); | ||
}); | ||
}; | ||
}; | ||
|
||
export const loginPending = () => { | ||
return { | ||
type: LOGIN_REQUEST | ||
}; | ||
}; | ||
|
||
export const loginError = (error) => { | ||
return { | ||
type: LOGIN_REQUEST_ERROR, | ||
payload: error | ||
}; | ||
}; | ||
|
||
export const loginSuccess = (user) => { | ||
return { | ||
type: LOGIN_REQUEST_SUCCESS, | ||
payload: user | ||
}; | ||
}; | ||
|
||
export const requestLogout = () => { | ||
return (dispatch) => { | ||
dispatch(logoutPending()); | ||
AuthService.signOut().then(() => { | ||
dispatch(logoutSuccess()); | ||
}).catch(error => { | ||
dispatch(logoutError(error)); | ||
}); | ||
}; | ||
}; | ||
|
||
export const logoutPending = () => { | ||
return { | ||
type: LOGOUT_REQUEST | ||
}; | ||
}; | ||
|
||
export const logoutError = (error) => { | ||
return { | ||
type: LOGIN | ||
type: LOGOUT_REQUEST_ERROR, | ||
payload: error | ||
}; | ||
}; | ||
export const logout = () => { | ||
|
||
export const logoutSuccess = () => { | ||
return { | ||
type: LOGOUT | ||
type: LOGOUT_REQUEST_SUCCESS | ||
}; | ||
}; | ||
|
||
export const restoringSession = () => { | ||
return { | ||
type: LOGIN_RESTORE_SESSION_REQUEST | ||
}; | ||
}; | ||
|
||
export const restoreSession = () => { | ||
return async (dispatch) => { | ||
dispatch(restoringSession()); | ||
try { | ||
const logged = await AuthService.isLogged(); | ||
if (logged) { | ||
dispatch(loginSuccess(await AuthService.getUser())); | ||
} //else if just 'not logged' | ||
} catch (err) { | ||
dispatch(requestLogout());//force clean up | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.