Skip to content

Commit

Permalink
Refactor Google login test
Browse files Browse the repository at this point in the history
* 구글 로그인에 대한 테스트를 작성하고 커버리지를 올린다.
  • Loading branch information
Kwakcena committed Jan 6, 2021
1 parent ab2388b commit b4ea933
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/authSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function requestGoogleSignIn() {
return async (dispatch) => {
try {
dispatch(setIsLoading(true));
const { user } = await postGoogleSignIn();
const user = await postGoogleSignIn();
const { email, displayName, uid } = user;

dispatch(setUser({ email, displayName, uid }));
Expand Down
4 changes: 1 addition & 3 deletions src/authSlice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ describe('actions', () => {

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

await store.dispatch(requestGoogleSignIn());

Expand Down
16 changes: 14 additions & 2 deletions src/services/__mocks__/firebase.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import products from '../../../fixtures/products';
import { logInUser } from '../../../fixtures/user';

const collections = {
products,
};

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

const firebase = {
auth: jest.fn(() => ({
signInWithEmailAndPassword: jest.fn((email, password) => ({
Expand Down Expand Up @@ -34,7 +39,7 @@ const firebase = {
delete: jest.fn(),
})),
add: jest.fn().mockImplementation((item) => item),
where: jest.fn().mockImplementation((fieldName, operator, value) => {
where: jest.fn().mockImplementation((_, operator, value) => {
let result = null;

if (operator === '==') {
Expand All @@ -54,10 +59,17 @@ const firebase = {
})),
})),
storage: jest.fn().mockImplementation(() => ({
refFromURL: jest.fn().mockImplementation((url) => ({
refFromURL: jest.fn().mockImplementation(() => ({
delete: jest.fn(),
})),
ref: jest.fn(() => ({
child: jest.fn(() => ({
put: jest.fn(),
getDownloadURL: jest.fn(() => 'MOCK_IMAGE_URL'),
})),
})),
})),
};

export default firebase;
export { googleAuthLogin };
15 changes: 7 additions & 8 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 from './firebase';
import firebase, { googleAuthLogin } from './firebase';
import { isEmpty } from '../utils';

export async function fetchProducts() {
Expand Down Expand Up @@ -43,10 +43,10 @@ export async function fetchUserProducts({ user }) {

export async function uploadProductImages({ files }) {
async function uploadProductImage(file) {
const uploadTask = firebase.storage()
const reference = firebase.storage()
.ref().child(`${file.name}${uuidv4()}`);
const response = await uploadTask.put(file);
const imageUrl = await response.ref.getDownloadURL();
await reference.put(file);
const imageUrl = await reference.getDownloadURL();
return imageUrl;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ export async function postDeleteProduct({ product }) {
await firebase
.firestore().collection('products').doc(id).delete();

if (isEmpty(productImages || [])) {
if (isEmpty(productImages)) {
return;
}

Expand All @@ -106,10 +106,9 @@ export async function postLogin({ email, password }) {
}

export async function postGoogleSignIn() {
const provider = new firebase.auth.GoogleAuthProvider();
const response = await firebase.auth().signInWithPopup(provider);
const { user } = await googleAuthLogin();

return response;
return user;
}

export async function postSignup({ email, password }) {
Expand Down
42 changes: 39 additions & 3 deletions src/services/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
postLogin,
postSignup,
postLogout,
uploadProductImages,
postGoogleSignIn,
} from './api';

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

describe('uploadProductImages', () => {
it('returns productImages', async () => {
const files = [
new File(['file'], 'productImage1.png', {
type: 'application/json',
}),
new File(['file'], 'productImage2.png', {
type: 'application/json',
}),
new File(['file'], 'productImage3.png', {
type: 'application/json',
}),
];

const data = await uploadProductImages({ files });

const productImages = files.map((file, _) => ({
name: file.name,
imageUrl: 'MOCK_IMAGE_URL',
}));

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

describe('postEditProduct', () => {
it('request product post edit', async () => {
const productId = 1;
Expand Down Expand Up @@ -83,12 +110,13 @@ describe('api', () => {
});

context('productImages is empty', () => {
it('request only product pos delete', async () => {
const emptyImagesProductPost = {
it('request only post delete', async () => {
const emptyImagesInPost = {
...product,
productImages: [],
};
await postDeleteProduct({ product: emptyImagesProductPost });

await postDeleteProduct({ product: emptyImagesInPost });
});
});
});
Expand All @@ -108,6 +136,14 @@ 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
3 changes: 3 additions & 0 deletions src/services/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ const firebaseConfig = {

firebase.initializeApp(firebaseConfig);

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

export default firebase;

0 comments on commit b4ea933

Please sign in to comment.