diff --git a/src/authSlice.js b/src/authSlice.js index 1da67ea..63f9f6b 100644 --- a/src/authSlice.js +++ b/src/authSlice.js @@ -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 })); diff --git a/src/authSlice.test.js b/src/authSlice.test.js index e4add60..7f1fb42 100644 --- a/src/authSlice.test.js +++ b/src/authSlice.test.js @@ -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()); diff --git a/src/services/__mocks__/firebase.js b/src/services/__mocks__/firebase.js index c90211c..582f413 100644 --- a/src/services/__mocks__/firebase.js +++ b/src/services/__mocks__/firebase.js @@ -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) => ({ @@ -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 === '==') { @@ -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 }; diff --git a/src/services/api.js b/src/services/api.js index 7b913b0..ed592dd 100644 --- a/src/services/api.js +++ b/src/services/api.js @@ -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() { @@ -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; } @@ -89,7 +89,7 @@ export async function postDeleteProduct({ product }) { await firebase .firestore().collection('products').doc(id).delete(); - if (isEmpty(productImages || [])) { + if (isEmpty(productImages)) { return; } @@ -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 }) { diff --git a/src/services/api.test.js b/src/services/api.test.js index 9c27292..970419e 100644 --- a/src/services/api.test.js +++ b/src/services/api.test.js @@ -9,6 +9,8 @@ import { postLogin, postSignup, postLogout, + uploadProductImages, + postGoogleSignIn, } from './api'; import products, { userProducts } from '../../fixtures/products'; @@ -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; @@ -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 }); }); }); }); @@ -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 = 'ghdrlfehd@test.com'; diff --git a/src/services/firebase.js b/src/services/firebase.js index 514156d..6a495ee 100644 --- a/src/services/firebase.js +++ b/src/services/firebase.js @@ -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;