Skip to content

Commit

Permalink
Merge pull request #97 from Kwakcena/Refactor-api-test
Browse files Browse the repository at this point in the history
Refactor api test
  • Loading branch information
Kwakcena authored Jan 14, 2021
2 parents e3099ea + dd915ff commit 305a5e4
Show file tree
Hide file tree
Showing 14 changed files with 362 additions and 261 deletions.
24 changes: 15 additions & 9 deletions fixtures/product.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const product = {
title: '아이패드 4세대 팝니다.',
description: '신형 아이패드 4세대 12.9인치 팝니다. 사용한지 3개월도 안된 제품이에요.',
region: '서울 강남',
price: '990000',
id: 1,
category: '가구/인테리어',
createAt: '168828208635',
description: '해외 이민으로 인해 가전제품 팝니다!010-****-****로 연락주세요!',
price: 350000,
productImages: [
'https://via.placeholder.com/600/810b14',
'https://via.placeholder.com/600/24f355',
'https://via.placeholder.com/600/f66b97',
{ name: '가전1.jpeg', imageUrl: '가전1url' },
{ name: '가전2.jpeg', imageUrl: '가전2url' },
{ name: '가전3.jpeg', imageUrl: '가전3url' },
],
createAt: '12341231242',
creatorId: 'abc123',
region: '서울시 강남구',
title: '해외 이사로 인한 가전제품 팔아요!',
user: {
displayName: '홍길동/연구과제팀/직원',
email: '[email protected]',
uid: 'ghdrlfehd1234',
},
};

export default product;
148 changes: 86 additions & 62 deletions fixtures/products.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,119 @@
const products = [
{
user: {
email: '[email protected]',
displayName: '홍 길동',
uid: '1234abcd',
},
id: 1,
title: '크리넥스 KF-AD 소형 마스크 팝니다.',
category: '기타 중고물품',
category: '가구/인테리어',
createAt: '1608828208635',
description: '해외 이민으로 인해 가전제품 팝니다!010-****-****로 연락주세요!',
price: 350000,
productImages: [
{ name: 'test1', imageUrl: 'testImageUrl1' },
{ name: 'test2', imageUrl: 'testImageUrl2' },
{ name: 'test3', imageUrl: 'testImageUrl3' },
{ name: '가전1.jpeg', imageUrl: '가전1url' },
{ name: '가전2.jpeg', imageUrl: '가전2url' },
{ name: '가전3.jpeg', imageUrl: '가전3url' },
],
region: '미추홀구 용현5동',
price: '25,000',
description: '저 크리넥스 스타일 마스크 대형 3개입\n님 크리넥스 데일리 입체형 황사마스크 KF80 소형 2개 교환 합니다... Or KF80 KFAD 소형 2개 가능합니다\n\n크리넥스 스타일 블랙 3매입 100장(75000) 판매도 해요\n',
},
{
region: '서울시 강남구',
title: '해외 이사로 인한 가전제품 팔아요!',
user: {
email: '[email protected]',
displayName: '홍 길동',
uid: '1234abcd',
displayName: '홍길동/연구과제팀/직원',
email: '[email protected]',
uid: 'ghdrlfehd1234',
},
},
{
id: 2,
title: '알레르망 범퍼침대',
category: '가구/인테리어',
category: '여성의류',
createAt: '1609754642862',
description: '팝니다.220 사이즈에요.',
price: 25000,
productImages: [
{ name: 'test4', imageUrl: 'testImageUrl4' },
{ name: 'test5', imageUrl: 'testImageUrl5' },
{ name: 'test6', imageUrl: 'testImageUrl6' },
{ name: '신발1.jpeg', imageUrl: '신발1url' },
],
region: '연수구 송도동',
price: '60,000',
description: '알레르망 범퍼 침대입니다\n18년도 구입했고 친정에서 주말에 방문할때만 사용해서 상태 좋습니다\n친정 이사로 인해\n처분합니다\n세탁해서 깨끗한 김장봉투에 넣어\n정리해 둔 상태입니다\n',
},
{
region: '인천 남구',
title: '여성 운동화',
user: {
email: '[email protected]',
displayName: '홍 길동',
uid: '1234abcd',
displayName: '김 철수',
email: '[email protected]',
uid: 'rlacjftn1234',
},
},
{
id: 3,
title: '청바지',
category: '남성패션/잡화',
createAt: '1609763267254',
description: '청바지 팝니다.32 사이즈 청바지에요.',
price: 25000,
productImages: [
{ name: 'test7', imageUrl: 'testImageUrl7' },
{ name: 'test8', imageUrl: 'testImageUrl8' },
{ name: 'test9', imageUrl: 'testImageUrl9' },
{ name: '청바지1.jpeg', imageUrl: '청바지1url' },
],
region: '청학동',
price: '10,000',
description: '게스 청바지 사이즈 25입니다\n하자없습니다\n가격내림',

},
{
region: '서울 강남',
title: '청바지 팝니다.',
user: {
email: '[email protected]',
displayName: '홍 길동',
uid: '1234abcd',
displayName: '김 철수',
email: '[email protected]',
uid: 'rlacjftn1234',
},
},
{
id: 4,
title: '아이패드 에어3',
category: '디지털/가전',
createAt: '1608838040530',
description: '아이패드 에어 중고 팔아요64기가 와이파이 버전입니다.',
price: 250000,
productImages: [
{ name: 'test10', imageUrl: 'testImageUrl10' },
{ name: 'test11', imageUrl: 'testImageUrl11' },
{ name: 'test12', imageUrl: 'testImageUrl12' },
{ name: '아이패드1.jpeg', imageUrl: '아이패드1url' },
{ name: '아이패드2.jpeg', imageUrl: '아이패드2url' },
{ name: '아이패드3.jpeg', imageUrl: '아이패드3url' },
],
region: '연수구 연수3동',
price: '650,000',
description: '펜슬 아이패드 파우치 아이패드 케이스 펜슬케이스 펜슬촉 다드립니다',
region: '서울',
title: '아이패드 에어 팔아요.',
user: {
displayName: '홍길동/연구과제팀/직원',
email: '[email protected]',
uid: 'ghdrlfehd1234',
},
},
];

const userProducts = [
{
id: 1,
category: '가구/인테리어',
createAt: '1608828208635',
description: '해외 이민으로 인해 가전제품 팝니다!010-****-****로 연락주세요!',
price: 350000,
productImages: [
{ name: '가전1.jpeg', imageUrl: '가전1url' },
{ name: '가전2.jpeg', imageUrl: '가전2url' },
{ name: '가전3.jpeg', imageUrl: '가전3url' },
],
region: '서울시 강남구',
title: '해외 이사로 인한 가전제품 팔아요!',
user: {
email: '[email protected]',
displayName: '홍 길동',
uid: '1234abcd',
displayName: '홍길동/연구과제팀/직원',
email: '[email protected]',
uid: 'ghdrlfehd1234',
},
id: 5,
title: '겔럭시S7 핑크블로썸 팝니다.',
},
{
id: 4,
category: '디지털/가전',
createAt: '1608838040530',
description: '아이패드 에어 중고 팔아요64기가 와이파이 버전입니다.',
price: 250000,
productImages: [
{ name: 'test13', imageUrl: 'testImageUrl13' },
{ name: 'test14', imageUrl: 'testImageUrl14' },
{ name: 'test15', imageUrl: 'testImageUrl15' },
{ name: '아이패드1.jpeg', imageUrl: '아이패드1url' },
{ name: '아이패드2.jpeg', imageUrl: '아이패드2url' },
{ name: '아이패드3.jpeg', imageUrl: '아이패드3url' },
],
region: '학익1동',
price: '100,000',
description: '✔통신사 sk로 쓰던 폰이며 색깔은 딸기우유색입니다\n✔정상해지 완료 한 제품입니다 😊\n✔32기가 입니다\n✔케이스 생활을 해도 생활기스가 약하게 있는 점 이해부탁드려요\n생활기스는 가까이서 보시면 티가 납니다😊\n✔거래 장소는 학익동 인천향교유림회관 앞입니다!\n✔중고제품이다 보니 환불 안되시는 점 주의해주세요!',
region: '서울',
title: '아이패드 에어 팔아요.',
user: {
displayName: '홍길동/연구과제팀/직원',
email: '[email protected]',
uid: 'ghdrlfehd1234',
},
},
];

export default products;

export { userProducts };
4 changes: 2 additions & 2 deletions fixtures/user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const logInUser = {
uid: '1234abcd',
displayName: '홍 길동',
email: '[email protected]',
email: '[email protected]',
uid: 'ghdrlfehd1234',
};

const logOutUser = {
Expand Down
28 changes: 13 additions & 15 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 @@ -58,7 +61,7 @@ export function requestLogin({ loginFields }) {
return async (dispatch) => {
try {
dispatch(setIsLoading(true));
const { user } = await postLogin({ email, password });
const user = await postLogin({ email, password });
const { displayName, uid } = user;

dispatch(setUser({ email, displayName, uid }));
Expand All @@ -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 All @@ -101,7 +99,7 @@ export function requestSignup({ signupFields }) {
return async (dispatch) => {
try {
dispatch(setIsLoading(true));
const { user } = await postSignup({ email, password });
const user = await postSignup({ email, password });
user.updateProfile({ displayName: userNickname });

const { displayName, uid } = user;
Expand Down
55 changes: 14 additions & 41 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 @@ -87,8 +87,8 @@ describe('actions', () => {
});

const loginFields = {
email: 'tester@example.com',
password: '1234abcd',
email: 'ghdrlfehd@example.com',
password: 'ghdrlfehd1234',
};

describe('requestLogin', () => {
Expand All @@ -97,9 +97,7 @@ describe('actions', () => {
});

it('dispatches requestLogin action and returns user', async () => {
postLogin.mockImplementationOnce(() => ({
user: logInUser,
}));
postLogin.mockImplementationOnce(() => logInUser);

await store.dispatch(requestLogin({ loginFields }));

Expand Down Expand Up @@ -133,46 +131,23 @@ describe('actions', () => {
store = mockStore({});
});

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

await store.dispatch(requestGoogleSignIn());

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

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

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('/'));
});
});

describe('requestSignup', () => {
const signupFields = {
firstName: '홍',
lastName: '길동',
email: 'tester@example.com',
email: 'ghdrlfehd@example.com',
password: '1234abcd',
};

Expand All @@ -184,10 +159,8 @@ describe('actions', () => {
it('returns user and change url path', async () => {
const updateProfile = jest.fn();
postSignup.mockImplementationOnce(() => ({
user: {
...logInUser,
updateProfile,
},
...logInUser,
updateProfile,
}));

await store.dispatch(requestSignup({ signupFields }));
Expand Down
Loading

0 comments on commit 305a5e4

Please sign in to comment.