Skip to content

Commit

Permalink
Refactoring api test
Browse files Browse the repository at this point in the history
* api 테스트 파일 리팩토링
  • Loading branch information
Kwakcena committed Jan 6, 2021
1 parent 655c178 commit ab2388b
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 89 deletions.
63 changes: 63 additions & 0 deletions src/services/__mocks__/firebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import products from '../../../fixtures/products';

const collections = {
products,
};

const firebase = {
auth: jest.fn(() => ({
signInWithEmailAndPassword: jest.fn((email, password) => ({
user: {
displayName: undefined, email, password,
},
})),
createUserWithEmailAndPassword: jest.fn((email, password) => ({
user: {
displayName: undefined, email, password,
},
})),
signOut: jest.fn(),
})),
firestore: jest.fn(() => ({
collection: jest.fn().mockImplementation((path) => ({
get: jest.fn().mockResolvedValue({
docs: collections[path].map((item) => ({
id: item.id,
data: () => item,
})),
}),
doc: jest.fn().mockImplementation((docId) => ({
get: jest.fn().mockResolvedValue({
data: () => products.find(({ id }) => id === docId),
}),
update: jest.fn(),
delete: jest.fn(),
})),
add: jest.fn().mockImplementation((item) => item),
where: jest.fn().mockImplementation((fieldName, operator, value) => {
let result = null;

if (operator === '==') {
result = ({
get: jest.fn().mockResolvedValue({
docs: collections[path]
.filter((doc) => doc.user.uid === value)
.map((doc) => ({
id: doc.uid,
data: () => doc,
})),
}),
});
}
return result;
}),
})),
})),
storage: jest.fn().mockImplementation(() => ({
refFromURL: jest.fn().mockImplementation((url) => ({
delete: jest.fn(),
})),
})),
};

export default firebase;
182 changes: 93 additions & 89 deletions src/services/api.test.js
Original file line number Diff line number Diff line change
@@ -1,127 +1,131 @@
import * as firebase from 'firebase';

import {
fetchProduct,
fetchProducts,
fetchUserProducts,
postProduct,
postDeleteProduct,
postEditProduct,
deleteAllImages,
postLogin,
postGoogleSignIn,
postSignup,
postLogout,
postProduct,
} from './api';

describe('firebase services', () => {
describe('postLogin', () => {
const mockPostLogin = ({ email, password }) => {
firebase.auth()
.signInWithEmailAndPassword = jest.fn().mockResolvedValue({
email, password,
});
};

const email = '[email protected]';
const password = '123456';

beforeEach(() => {
mockPostLogin({ email, password });
});
import products, { userProducts } from '../../fixtures/products';
import product from '../../fixtures/product';
import { logInUser } from '../../fixtures/user';

jest.mock('./firebase.js');

it('returns uid', async () => {
const user = await postLogin({ email, password });
describe('api', () => {
describe('fetchProducts', () => {
it('returns products', async () => {
const data = await fetchProducts();

expect(user).toEqual({ email, password });
expect(data).toEqual(products);
});
});

describe('postGoogleSignIn', () => {
const mockGoogleSignIn = ({ user }) => {
firebase.auth()
.signInWithPopup = jest.fn().mockResolvedValue(user);
};
describe('fetchProduct', () => {
it('returns product', async () => {
const productId = 1;

const user = {
displayName: 'tester',
uid: 'abc1234',
};
const data = await fetchProduct(productId);

beforeEach(() => {
mockGoogleSignIn({ user });
expect(data).toEqual(
products.find(({ id }) => id === productId),
);
});
});

it('returns uid', async () => {
const mockUser = await postGoogleSignIn();
describe('fetchUserProducts', () => {
it('returns userProducts', async () => {
const user = logInUser;

expect(mockUser).toEqual({
displayName: 'tester',
uid: 'abc1234',
});
const data = await fetchUserProducts({ user });

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

describe('postLogout', () => {
const mockLogout = () => {
firebase.auth()
.signOut = jest.fn().mockResolvedValue(true);
};
describe('postProduct', () => {
it('return post id', async () => {
const data = await postProduct(product);

beforeEach(() => {
mockLogout();
expect(data).toEqual(product.id);
});
});

it('returns promise', async () => {
const logout = await postLogout();
describe('postEditProduct', () => {
it('request product post edit', async () => {
const productId = 1;
const editedProduct = {
...product,
title: '가전제품 팜',
price: 400000,
};

expect(logout).toBe(true);
await postEditProduct({ productId, editedProduct });
});
});

describe('postSignup', () => {
const mockFirebaseSignup = ({ email, password }) => {
firebase.auth()
.createUserWithEmailAndPassword = jest.fn().mockResolvedValue({
displayName: '',
email,
password,
uid: '',
});
};

const email = '[email protected]';
const password = '123456';

beforeEach(() => {
mockFirebaseSignup({ email, password });
describe('postDeleteProduct', () => {
context('productImages is not empty', () => {
it('request delete all images and product post delete', async () => {
const { productImages } = product;

await postDeleteProduct({ product });

const deleteUrls = productImages.map(({ imageUrl }) => imageUrl);

await deleteAllImages(deleteUrls);
});
});

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

describe('postLogin', () => {
it('returns user', async () => {
const email = '[email protected]';
const password = '123456';

it('returns new account', async () => {
const newUser = await postSignup({ email, password });
const data = await postLogin({ email, password });

expect(newUser.email).toBe(email);
expect(data).toEqual({
displayName: undefined,
email,
password,
});
});
});

describe('postProduct', () => {
const add = jest.fn((product) => product);
const collection = jest.spyOn(
firebase.firestore(), 'collection',
).mockReturnValue({ add });

it('post new product', async () => {
const newProduct = {
title: 'test title',
description: 'test description',
productImages: [],
createdAt: Date.now(),
user: {
uid: 'test1234',
displayName: '홍 길동',
email: '[email protected]',
},
};
describe('postSignup', () => {
it('returns sign up user', async () => {
const email = '[email protected]';
const password = '123456';

await postProduct(newProduct);
const data = await postSignup({ email, password });

expect(collection).toHaveBeenCalledWith('products');
expect(data).toEqual({
displayName: undefined,
email,
password,
});
});
});

expect(add).toHaveBeenCalledWith(newProduct);
describe('postLogout', () => {
it('request logout', async () => {
await postLogout();
});
});
});

0 comments on commit ab2388b

Please sign in to comment.