From a1aa3ad3fd607c6d93ee3cd4ae4c4b452df8a6d4 Mon Sep 17 00:00:00 2001 From: saseungmin Date: Fri, 9 Apr 2021 01:44:20 +0900 Subject: [PATCH] =?UTF-8?q?[Feature]=20Add=20validity=20for=20study=20revi?= =?UTF-8?q?ew=20-=20=EC=8A=A4=ED=84=B0=EB=94=94=EC=97=90=20=EC=8B=A0?= =?UTF-8?q?=EC=B2=AD=ED=95=9C=20=ED=9A=8C=EC=9B=90=EC=9D=B4=20=ED=95=9C=20?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=EB=A5=BC=20=EC=9E=91=EC=84=B1=ED=95=98?= =?UTF-8?q?=EB=A9=B4=20=EC=9E=91=EC=84=B1=20=EB=B6=88=EA=B0=80=EB=8A=A5?= =?UTF-8?q?=ED=95=98=EA=B2=8C=20=EB=B3=80=EA=B2=BD=20-=20=EC=A6=89,=20?= =?UTF-8?q?=ED=95=98=EB=82=98=EC=9D=98=20=EB=A6=AC=EB=B7=B0=EB=A7=8C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=EA=B0=80=EB=8A=A5=20-=20=EB=A6=AC=EB=B7=B0?= =?UTF-8?q?=EA=B0=80=20=EC=A1=B4=EC=9E=AC=ED=95=98=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EC=9D=84=EC=8B=9C=EC=97=90=20=EB=8C=80=ED=95=9C=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/introduce/ReviewForm.jsx | 18 ++-- src/components/introduce/ReviewForm.test.jsx | 90 ++++++++++++------- src/components/introduce/ReviewList.jsx | 2 +- src/components/introduce/ReviewList.test.jsx | 2 +- src/containers/introduce/ReviewContainer.jsx | 2 +- .../introduce/ReviewContainer.test.jsx | 18 +--- src/reducers/groupSlice.js | 37 ++------ src/reducers/groupSlice.test.js | 37 +++----- 8 files changed, 92 insertions(+), 114 deletions(-) diff --git a/src/components/introduce/ReviewForm.jsx b/src/components/introduce/ReviewForm.jsx index e0f13fe..318c35d 100644 --- a/src/components/introduce/ReviewForm.jsx +++ b/src/components/introduce/ReviewForm.jsx @@ -53,18 +53,18 @@ const StudyReviewFormButton = styled(Button)` margin: 1px 0 0.8rem 0.5rem; `; -const isValidateUserInfo = (user, participants) => !!participants - .find(({ id, confirm }) => id === user && confirm && confirm === true); +const isValidateAboutUser = (user, group) => { + const { participants, reviews } = group; + + return !participants.some(({ id, confirm }) => id === user && confirm && confirm === true) + || reviews.some(({ id }) => id && id === user); +}; const ReviewForm = ({ - participants, user, fields, onChangeReview, onSubmit, + group, user, fields, onChangeReview, onSubmit, }) => { const { rating, content } = fields; - if (!isValidateUserInfo(user, participants)) { - return null; - } - const handleChangeRating = (newRating, name) => { onChangeReview({ name, @@ -81,6 +81,10 @@ const ReviewForm = ({ }); }; + if (isValidateAboutUser(user, group)) { + return null; + } + return ( diff --git a/src/components/introduce/ReviewForm.test.jsx b/src/components/introduce/ReviewForm.test.jsx index e6c16d6..7056cfb 100644 --- a/src/components/introduce/ReviewForm.test.jsx +++ b/src/components/introduce/ReviewForm.test.jsx @@ -15,65 +15,88 @@ describe('ReviewForm', () => { const reviewForm = { rating: 3, content: '' }; const renderReviewForm = ({ - participants, user, fields = reviewForm, + group, user, fields = reviewForm, }) => render(( )); - const userStatusSetting = ({ user, participants }) => ({ - participants, + const userStatusSetting = ({ user, group }) => ({ + group, user, }); context('with user', () => { - describe('When the user is approved applicant and applyEndDate is Deadline', () => { + context('When you have already written a review', () => { const settings = { - participants: [{ id: 'user1', confirm: true }], + group: { + participants: [{ id: 'user1', confirm: true }], + reviews: [{ id: 'user1', content: 'review' }], + }, user: 'user1', }; - it('renders study review form', () => { + it('Should be nothing renders', () => { const { container } = renderReviewForm(userStatusSetting(settings)); - expect(container).toHaveTextContent('스터디 후기를 작성해주세요!'); + expect(container).toBeEmptyDOMElement(); }); - it('call event change review form', () => { - const { getByPlaceholderText } = renderReviewForm(userStatusSetting(settings)); - - const textarea = getByPlaceholderText('후기를 입력해주세요!'); + }); - fireEvent.change(textarea, { - target: { - name: 'content', - value: 'test', + context("When you didn't write a review", () => { + describe('When the user is approved applicant and applyEndDate is Deadline', () => { + const settings = { + group: { + participants: [{ id: 'user1', confirm: true }], + reviews: [], }, + user: 'user1', + }; + + it('renders study review form', () => { + const { container } = renderReviewForm(userStatusSetting(settings)); + expect(container).toHaveTextContent('스터디 후기를 작성해주세요!'); }); + it('call event change review form', () => { + const { getByPlaceholderText } = renderReviewForm(userStatusSetting(settings)); - expect(handleChange).toBeCalled(); - }); + const textarea = getByPlaceholderText('후기를 입력해주세요!'); - it('call event click for review form', () => { - const { getByText } = renderReviewForm(userStatusSetting(settings)); + fireEvent.change(textarea, { + target: { + name: 'content', + value: 'test', + }, + }); - fireEvent.click(getByText('후기 등록하기')); + expect(handleChange).toBeCalled(); + }); - expect(handleSubmit).toBeCalled(); - }); - }); + it('call event click for review form', () => { + const { getByText } = renderReviewForm(userStatusSetting(settings)); - describe('When the user is not approved applicant', () => { - it('nothing renders study review form', () => { - const { container } = renderReviewForm(userStatusSetting({ - participants: [], - user: 'user2', - })); + fireEvent.click(getByText('후기 등록하기')); - expect(container).toBeEmptyDOMElement(); + expect(handleSubmit).toBeCalled(); + }); + }); + + describe('When the user is not approved applicant', () => { + it('nothing renders study review form', () => { + const { container } = renderReviewForm(userStatusSetting({ + group: { + participants: [], + reviews: [], + }, + user: 'user2', + })); + + expect(container).toBeEmptyDOMElement(); + }); }); }); }); @@ -81,7 +104,10 @@ describe('ReviewForm', () => { context('without user', () => { it('nothing renders study review form', () => { const { container } = renderReviewForm(userStatusSetting({ - participants: [], + group: { + participants: [], + reviews: [], + }, user: null, })); diff --git a/src/components/introduce/ReviewList.jsx b/src/components/introduce/ReviewList.jsx index 720baef..515404b 100644 --- a/src/components/introduce/ReviewList.jsx +++ b/src/components/introduce/ReviewList.jsx @@ -31,7 +31,7 @@ const ReviewList = ({ reviews }) => { if (_.isEmpty(reviews)) { return ( - 등록된 리뷰가 존재하지 않습니다! + 등록된 후기가 존재하지 않습니다! ); } diff --git a/src/components/introduce/ReviewList.test.jsx b/src/components/introduce/ReviewList.test.jsx index 346624e..45f4e8c 100644 --- a/src/components/introduce/ReviewList.test.jsx +++ b/src/components/introduce/ReviewList.test.jsx @@ -33,7 +33,7 @@ describe('ReviewList', () => { it('Render nothing review message', () => { const { container } = renderReviewList([]); - expect(container).toHaveTextContent('등록된 리뷰가 존재하지 않습니다!'); + expect(container).toHaveTextContent('등록된 후기가 존재하지 않습니다!'); }); }); }); diff --git a/src/containers/introduce/ReviewContainer.jsx b/src/containers/introduce/ReviewContainer.jsx index 2d9ad34..cdc293f 100644 --- a/src/containers/introduce/ReviewContainer.jsx +++ b/src/containers/introduce/ReviewContainer.jsx @@ -57,7 +57,7 @@ const ReviewFormContainer = () => { { confirm: true, }, ], + reviews: [], applyEndDate: yesterday, })); given('user', () => ('user1')); const { container } = renderReviewContainer(); - expect(container).toHaveTextContent('등록된 리뷰가 존재하지 않습니다!'); + expect(container).toHaveTextContent('등록된 후기가 존재하지 않습니다!'); }); context('with login and group', () => { @@ -103,12 +104,7 @@ describe('ReviewContainer', () => { }, ], applyEndDate: yesterday, - reviews: [{ - id: 'user1', - rating: 3, - content: 'test review', - createdDate: new Date(), - }], + reviews: [], })); describe('When you are an approved applicant', () => { @@ -146,14 +142,6 @@ describe('ReviewContainer', () => { expect(dispatch).toBeCalledTimes(1); }); }); - - describe('Renders Review List', () => { - it('Information about the study review is render', () => { - const { container } = renderReviewContainer(); - - expect(container).toHaveTextContent('test review'); - }); - }); }); }); diff --git a/src/reducers/groupSlice.js b/src/reducers/groupSlice.js index 934c997..00500d4 100644 --- a/src/reducers/groupSlice.js +++ b/src/reducers/groupSlice.js @@ -139,36 +139,12 @@ const { actions, reducer } = createSlice({ }, setGroupReview(state, { payload: review }) { - const { group } = state; - - if (group.reviews) { - return { - ...state, - group: { - ...group, - reviews: [ - { - ...review, - createDate: new Date().toString(), - }, - ...group.reviews, - ], - }, - }; - } - - return { - ...state, - group: { - ...group, - reviews: [ - { - ...review, - createDate: new Date().toString(), - }, - ], - }, - }; + return produce(state, (draft) => { + draft.group.reviews.push({ + ...review, + createDate: new Date().toString(), + }); + }); }, }, }); @@ -217,6 +193,7 @@ export const writeStudyGroup = () => async (dispatch, getState) => { const groupId = await postStudyGroup( produce(groupReducer.writeField, (draft) => { draft.moderatorId = user; + draft.reviews = []; draft.participants.push({ id: user, }); diff --git a/src/reducers/groupSlice.test.js b/src/reducers/groupSlice.test.js index 4aac8b3..c49521c 100644 --- a/src/reducers/groupSlice.test.js +++ b/src/reducers/groupSlice.test.js @@ -277,36 +277,19 @@ describe('reducer', () => { rating: 3, }; - context('When the group reviews field is exists', () => { - const initialState = { - group: { - reviews: [], - }, - }; - - it('Set in the group review field', () => { - const state = reducer(initialState, setGroupReview(review)); - - const { group: { reviews } } = state; - - expect(reviews[0].id).toBe('test'); - expect(reviews[0].rating).toBe(3); - }); - }); - - context("When the group reviews field isn't exists", () => { - const initialState = { - group: {}, - }; + const initialState = { + group: { + reviews: [], + }, + }; - it('Set in the group review field', () => { - const state = reducer(initialState, setGroupReview(review)); + it('Set in the group review field', () => { + const state = reducer(initialState, setGroupReview(review)); - const { group: { reviews } } = state; + const { group: { reviews } } = state; - expect(reviews[0].id).toBe('test'); - expect(reviews[0].rating).toBe(3); - }); + expect(reviews[0].id).toBe('test'); + expect(reviews[0].rating).toBe(3); }); }); });