Skip to content

Commit

Permalink
Refactor] authSlice
Browse files Browse the repository at this point in the history
* authSlice에서 requestGoogleSignIn 함수에 try catch를 제거함
  • Loading branch information
Kwakcena committed Jan 14, 2021
1 parent 34abbf6 commit dd915ff
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 65 deletions.
24 changes: 11 additions & 13 deletions src/authSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import { saveItem, deleteItem } from './services/storage';

import { setIsLoading } from './commonSlice';

import {
googleAuthLogin,
} from './services/firebase';

import {
postLogin,
postGoogleSignIn,
postSignup,
postLogout,
} from './services/api';
Expand Down Expand Up @@ -75,20 +78,15 @@ export function requestLogin({ loginFields }) {

export function requestGoogleSignIn() {
return async (dispatch) => {
try {
dispatch(setIsLoading(true));
const user = await postGoogleSignIn();
const { email, displayName, uid } = user;
dispatch(setIsLoading(true));
const { user } = await googleAuthLogin();
const { email, displayName, uid } = user;

dispatch(setUser({ email, displayName, uid }));
saveItem('user', { email, displayName, uid });
dispatch(setIsLoading(false));
dispatch(setUser({ email, displayName, uid }));
saveItem('user', { email, displayName, uid });
dispatch(setIsLoading(false));

dispatch(push('/'));
} catch (error) {
dispatch(setError(error.message));
dispatch(setIsLoading(false));
}
dispatch(push('/'));
};
}

Expand Down
37 changes: 8 additions & 29 deletions src/authSlice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import authReducer, {
} from './authSlice';

import {
postGoogleSignIn,
postLogin,
postSignup,
} from './services/api';
Expand All @@ -24,6 +23,7 @@ const middlewares = [...getDefaultMiddleware()];
const mockStore = configureStore(middlewares);

jest.mock('./services/api');
jest.mock('./services/firebase');
jest.mock('connected-react-router');

describe('reducer', () => {
Expand Down Expand Up @@ -131,36 +131,15 @@ describe('actions', () => {
store = mockStore({});
});

context('when request success', () => {
it('returns user and change url path', async () => {
postGoogleSignIn.mockImplementationOnce(() => logInUser);

await store.dispatch(requestGoogleSignIn());
it('returns user and change url path', async () => {
const actions = store.getActions();

const actions = store.getActions();
await store.dispatch(requestGoogleSignIn());

expect(actions[0]).toEqual(setIsLoading(true));
expect(actions[1]).toEqual(setUser(logInUser));
expect(actions[2]).toEqual(setIsLoading(false));
expect(actions[3]).toEqual(push('/'));
});
});

context('when request fail', () => {
it('returns an error', async () => {
postGoogleSignIn.mockImplementationOnce(
() => Promise.reject(
new Error('something bad happened'),
),
);

try {
await store.dispatch(requestGoogleSignIn());
} catch {
const actions = store.getActions();
expect(actions[0].payload.error).toEqual('Something bad happened');
}
});
expect(actions[0]).toEqual(setIsLoading(true));
expect(actions[1]).toEqual(setUser(logInUser));
expect(actions[2]).toEqual(setIsLoading(false));
expect(actions[3]).toEqual(push('/'));
});
});

Expand Down
7 changes: 2 additions & 5 deletions src/services/__mocks__/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ const collections = {
products,
};

const googleAuthLogin = jest.fn().mockImplementation(() => ({
user: logInUser,
}));
const googleAuthLogin = jest.fn().mockImplementation(() => Promise.resolve({ user: logInUser }));

const firebase = {
auth: jest.fn(() => ({
Expand Down Expand Up @@ -71,5 +69,4 @@ const firebase = {
})),
};

export default firebase;
export { googleAuthLogin };
export { firebase, googleAuthLogin };
8 changes: 1 addition & 7 deletions src/services/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { v4 as uuidv4 } from 'uuid';
import firebase, { googleAuthLogin } from './firebase';
import { firebase } from './firebase';
import { isEmpty } from '../utils';

export async function fetchProducts() {
Expand Down Expand Up @@ -105,12 +105,6 @@ export async function postLogin({ email, password }) {
return user;
}

export async function postGoogleSignIn() {
const { user } = await googleAuthLogin();

return user;
}

export async function postSignup({ email, password }) {
const { user } = await firebase
.auth()
Expand Down
9 changes: 0 additions & 9 deletions src/services/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
postSignup,
postLogout,
uploadProductImages,
postGoogleSignIn,
} from './api';

import products, { userProducts } from '../../fixtures/products';
Expand Down Expand Up @@ -136,14 +135,6 @@ describe('api', () => {
});
});

describe('postGoogleSignin', () => {
it('returns user', async () => {
const data = await postGoogleSignIn();

expect(data).toEqual(logInUser);
});
});

describe('postSignup', () => {
it('returns sign up user', async () => {
const email = '[email protected]';
Expand Down
4 changes: 2 additions & 2 deletions src/services/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ const firebaseConfig = {
firebase.initializeApp(firebaseConfig);

const googleAuthProvider = new firebase.auth.GoogleAuthProvider();
export const googleAuthLogin = () => firebase.auth().signInWithPopup(googleAuthProvider);
const googleAuthLogin = () => firebase.auth().signInWithPopup(googleAuthProvider);

export default firebase;
export { firebase, googleAuthLogin };

0 comments on commit dd915ff

Please sign in to comment.