Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/components/introduce/StudyReviewForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const isValidateUserInfo = (user) => (participants) => !!participants
.find(({ id, confirm }) => id === user && confirm && confirm === true);

const StudyReviewForm = ({
group, user, time, fields, onChangeReview,
group, user, time, fields, onChangeReview, onSubmit,
}) => {
const {
participants, personnel, applyEndDate,
Expand Down Expand Up @@ -103,7 +103,10 @@ const StudyReviewForm = ({
placeholder="후기를 입력해주세요!"
onChange={handleChangeReview}
/>
<StudyReviewFormButton success>
<StudyReviewFormButton
success
onClick={onSubmit}
>
후기 등록하기
</StudyReviewFormButton>
</StudyReviewFormBody>
Expand Down
33 changes: 23 additions & 10 deletions src/components/introduce/StudyReviewForm.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ import { yesterday } from '../../util/utils';
import STUDY_GROUP from '../../../fixtures/study-group';

describe('StudyReviewForm', () => {
beforeEach(() => {
jest.clearAllMocks();
});

const handleChange = jest.fn();
const handleSubmit = jest.fn();

const reviewForm = { rating: 3, review: '' };

const renderStudyReviewForm = ({
group, time, user, fields = reviewForm,
}) => render((
<StudyReviewForm
group={group}
time={time}
user={user}
time={time}
group={group}
fields={fields}
onSubmit={handleSubmit}
onChangeReview={handleChange}
/>
));
Expand All @@ -36,18 +42,17 @@ describe('StudyReviewForm', () => {

context('with user', () => {
describe('When the user is approved applicant and applyEndDate is Deadline', () => {
const settings = {
participants: [{ id: 'user1', confirm: true }],
user: 'user1',
};

it('renders study review form', () => {
const { container } = renderStudyReviewForm(userStatusSetting({
participants: [{ id: 'user1', confirm: true }],
user: 'user1',
}));
const { container } = renderStudyReviewForm(userStatusSetting(settings));
expect(container).toHaveTextContent('스터디 후기를 작성해주세요!');
});
it('call event change review form', () => {
const { getByPlaceholderText } = renderStudyReviewForm(userStatusSetting({
participants: [{ id: 'user1', confirm: true }],
user: 'user1',
}));
const { getByPlaceholderText } = renderStudyReviewForm(userStatusSetting(settings));

const textarea = getByPlaceholderText('후기를 입력해주세요!');

Expand All @@ -60,6 +65,14 @@ describe('StudyReviewForm', () => {

expect(handleChange).toBeCalled();
});

it('call event click for review form', () => {
const { getByText } = renderStudyReviewForm(userStatusSetting(settings));

fireEvent.click(getByText('후기 등록하기'));

expect(handleSubmit).toBeCalled();
});
});

describe('When the user is not approved applicant', () => {
Expand Down
12 changes: 10 additions & 2 deletions src/containers/introduce/StudyReviewContainer.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useState, useCallback } from 'react';

import { useSelector, useDispatch } from 'react-redux';
import { useInterval } from 'react-use';
import { useSelector, useDispatch } from 'react-redux';

import { getAuth, getGroup } from '../../util/utils';
import { changeStudyReviewFields, setStudyReview } from '../../reducers/groupSlice';

import StudyReviewForm from '../../components/introduce/StudyReviewForm';
import { changeStudyReviewFields } from '../../reducers/groupSlice';

const StudyReviewContainer = () => {
const [realTime, setRealTime] = useState(Date.now());
Expand All @@ -25,6 +25,13 @@ const StudyReviewContainer = () => {
dispatch(changeStudyReviewFields({ name, value }));
}, [dispatch]);

const onSubmitReview = useCallback(() => {
dispatch(setStudyReview({
id: user,
...studyReviewFields,
}));
}, [dispatch, user, studyReviewFields]);

if (!group) {
return null;
}
Expand All @@ -36,6 +43,7 @@ const StudyReviewContainer = () => {
time={realTime}
fields={studyReviewFields}
onChangeReview={onChangeReviewFields}
onSubmit={onSubmitReview}
/>
);
};
Expand Down
10 changes: 10 additions & 0 deletions src/containers/introduce/StudyReviewContainer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ describe('StudyReviewContainer', () => {
payload: form,
});
});

describe('Click the button to submit for study review', () => {
it('dispatch actions call setStudyReview', () => {
const { getByText } = renderStudyReviewContainer();

fireEvent.click(getByText('후기 등록하기'));

expect(dispatch).toBeCalledTimes(1);
});
});
});

context('without login and group', () => {
Expand Down
22 changes: 22 additions & 0 deletions src/reducers/groupSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
updateConfirmPostParticipant,
deletePostGroup,
editPostStudyGroup,
postUpdateStudyReview,
} from '../services/api';

const writeInitialState = {
Expand All @@ -21,6 +22,7 @@ const writeInitialState = {
participants: [],
personnel: '1',
tags: [],
reviews: [],
};

const applyInitialState = {
Expand Down Expand Up @@ -127,6 +129,13 @@ const { actions, reducer } = createSlice({
draft.studyReviewFields[name] = value;
});
},

clearStudyReviewFields(state) {
return {
...state,
studyReviewFields: studyReviewInitialState,
};
},
},
});

Expand All @@ -141,6 +150,7 @@ export const {
clearApplyFields,
setOriginalArticle,
changeStudyReviewFields,
clearStudyReviewFields,
} = actions;

export const loadStudyGroups = (tag) => async (dispatch) => {
Expand Down Expand Up @@ -276,4 +286,16 @@ export const deleteGroup = (groupId) => async (dispatch) => {
dispatch(loadStudyGroups());
};

export const setStudyReview = (review) => async (dispatch, getState) => {
const { groupReducer: { group } } = getState();
const { id } = group;

await postUpdateStudyReview({
id,
review,
});

dispatch(clearStudyReviewFields());
};

export default reducer;
46 changes: 46 additions & 0 deletions src/reducers/groupSlice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import reducer, {
editStudyGroup,
setGroupError,
changeStudyReviewFields,
clearStudyReviewFields,
setStudyReview,
} from './groupSlice';

import STUDY_GROUPS from '../../fixtures/study-groups';
import STUDY_GROUP from '../../fixtures/study-group';
import WRITE_FORM from '../../fixtures/write-form';

import { editPostStudyGroup, postStudyGroup } from '../services/api';

const middlewares = [thunk];
Expand All @@ -50,6 +53,7 @@ describe('reducer', () => {
participants: [],
personnel: '1',
tags: [],
reviews: [],
},
applyFields: {
reason: '',
Expand Down Expand Up @@ -244,6 +248,24 @@ describe('reducer', () => {
expect(state.studyReviewFields.rating).toBe(5);
});
});

describe('clearStudyReviewFields', () => {
const initialState = {
studyReviewFields: {
rating: 5,
review: 'test',
},
};

it('clears fields of study review form', () => {
const state = reducer(initialState, clearStudyReviewFields());

const { studyReviewFields: { rating, review } } = state;

expect(rating).toBe(3);
expect(review).toBe('');
});
});
});

describe('async actions', () => {
Expand Down Expand Up @@ -524,4 +546,28 @@ describe('async actions', () => {
});
});
});

describe('setStudyReview', () => {
beforeEach(() => {
store = mockStore({
groupReducer: {
group: { id: '1' },
},
});
});

it('dispatches clearStudyReviewFields', async () => {
await store.dispatch(setStudyReview({
user: 'user',
review: 'test',
rating: 5,
}));

const actions = store.getActions();

expect(actions[0]).toEqual({
type: 'group/clearStudyReviewFields',
});
});
});
});
2 changes: 2 additions & 0 deletions src/services/__mocks__/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export const updateConfirmPostParticipant = jest.fn();
export const deletePostGroup = jest.fn();

export const editPostStudyGroup = jest.fn();

export const postUpdateStudyReview = jest.fn();
28 changes: 18 additions & 10 deletions src/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ export const postStudyGroup = async (group) => {
export const editPostStudyGroup = async ({
title, applyEndDate, contents, tags, personnel, id,
}) => {
const groups = db.collection('groups').doc(id);
const group = db.collection('groups').doc(id);

const timeStamp = fireStore.Timestamp.fromDate(new Date(applyEndDate));

await groups.update({
await group.update({
title,
contents,
applyEndDate: timeStamp,
Expand All @@ -79,32 +79,40 @@ export const editPostStudyGroup = async ({
});
};

export const postUpdateStudyReview = async ({ id, review }) => {
const group = db.collection('groups').doc(id);

await group.set({
reviews: fireStore.FieldValue.arrayUnion(review),
}, { merge: true });
};
Comment on lines +82 to +88
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

firebase api 테스트에 대한 고민 좀 해보자.


export const updatePostParticipant = async ({ id, user }) => {
const groups = db.collection('groups').doc(id);
const group = db.collection('groups').doc(id);

await groups.update({
await group.update({
participants: fireStore.FieldValue.arrayUnion(user),
});
};

export const deletePostParticipant = async ({ id, participants }) => {
const groups = db.collection('groups').doc(id);
const group = db.collection('groups').doc(id);

await groups.update({
await group.update({
participants,
});
};

export const deletePostGroup = async (id) => {
const groups = db.collection('groups').doc(id);
const group = db.collection('groups').doc(id);

await groups.delete();
await group.delete();
};

export const updateConfirmPostParticipant = async ({ id, participants }) => {
const groups = db.collection('groups').doc(id);
const group = db.collection('groups').doc(id);

await groups.update({
await group.update({
participants,
});
};
Expand Down