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
5 changes: 4 additions & 1 deletion src/App.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('App', () => {

useSelector.mockImplementation((selector) => selector({
groups: STUDY_GROUPS,
group: STUDY_GROUP,
group: given.group,
writeField: {
tags: [],
},
Expand All @@ -37,6 +37,7 @@ describe('App', () => {
));

context('with path /', () => {
given('group', () => (null));
it('renders the study list page', () => {
const { container } = renderApp({ path: '/' });

Expand All @@ -45,6 +46,7 @@ describe('App', () => {
});

context('with path /introduce', () => {
given('group', () => (STUDY_GROUP));
it('renders the study introduce page', () => {
const { container } = renderApp({ path: '/introduce/1' });

Expand All @@ -53,6 +55,7 @@ describe('App', () => {
});

context('with path /write', () => {
given('group', () => (null));
it('renders the study write page', () => {
const { container } = renderApp({ path: '/write' });

Expand Down
19 changes: 19 additions & 0 deletions src/components/write/WriteButtons.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';

import styled from '@emotion/styled';

const WriteButtonsWrapper = styled.div``;

const WriteButtons = ({ onSubmit }) => (
<WriteButtonsWrapper>
<button
type="button"
onClick={onSubmit}
>
๋“ฑ๋กํ•˜๊ธฐ
</button>
<button type="button">์ทจ์†Œ</button>
</WriteButtonsWrapper>
);

export default WriteButtons;
18 changes: 18 additions & 0 deletions src/components/write/WriteButtons.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';

import { render } from '@testing-library/react';

import WriteButtons from './WriteButtons';

describe('WriteButtons', () => {
const renderWriteButtons = () => render((
<WriteButtons />
));

it('render Write buttons', () => {
const { container } = renderWriteButtons();

expect(container).toHaveTextContent('๋“ฑ๋กํ•˜๊ธฐ');
expect(container).toHaveTextContent('์ทจ์†Œ');
});
});
39 changes: 39 additions & 0 deletions src/containers/write/WriteButtonsContainer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { useEffect, useCallback } from 'react';

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

import { useHistory } from 'react-router-dom';
Comment on lines +3 to +5
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์—ฌ๊ธด ๋ถ™์—ฌ์ฃผ๋Š”๊ฒŒ ๋‚˜์•„๋ณด์ธ๋‹ค.


import { get } from '../../util/utils';
import { writeStudyGroup } from '../../reducers/slice';

import WriteButtons from '../../components/write/WriteButtons';

const WriteButtonsContainer = () => {
const history = useHistory();
const dispatch = useDispatch();

const writeField = useSelector(get('writeField'));
const group = useSelector(get('group'));

const onSubmit = useCallback(() => {
// TODO: write form validate ์ฒดํฌ ํ•˜๊ธฐ
dispatch(writeStudyGroup());
}, [dispatch]);
Comment on lines +19 to +22
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  • writeField๊ฐ€ ๋นˆ์นธ์ด๋ฉด ๋“ฑ๋ก๋ชปํ•˜๊ฒŒ ๋ง‰๋Š” validate๋ฅผ ์ฒดํฌํ•ด์ฃผ๋Š” ๋กœ์ง ๊ตฌํ˜„ํ•ด์ฃผ๊ธฐ


useEffect(() => {
if (group) {
const { id } = group;
history.push(`/introduce/${id}`);
}
}, [history, group]);

return (
<WriteButtons
fields={writeField}
onSubmit={onSubmit}
/>
);
};

export default WriteButtonsContainer;
75 changes: 75 additions & 0 deletions src/containers/write/WriteButtonsContainer.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React from 'react';

import { useDispatch, useSelector } from 'react-redux';
import { MemoryRouter } from 'react-router-dom';

import { render, fireEvent } from '@testing-library/react';

import WriteButtonsContainer from './WriteButtonsContainer';

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

const mockPush = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useHistory() {
return { push: mockPush };
},
}));

describe('WriteButtonsContainer', () => {
const dispatch = jest.fn();

beforeEach(() => {
dispatch.mockClear();
mockPush.mockClear();

useDispatch.mockImplementation(() => dispatch);

useSelector.mockImplementation((state) => state({
writeField: WRITE_FORM,
group: given.group,
}));
});

const renderWriteButtonsContainer = () => render((
<MemoryRouter>
<WriteButtonsContainer />
</MemoryRouter>
));

it('render Write buttons', () => {
const { container } = renderWriteButtonsContainer();

expect(container).toHaveTextContent('๋“ฑ๋กํ•˜๊ธฐ');
expect(container).toHaveTextContent('์ทจ์†Œ');
});

describe('when click submit button', () => {
context('with group', () => {
given('group', () => (STUDY_GROUP));
it('dispatch action submit event', () => {
const { getByText } = renderWriteButtonsContainer();

fireEvent.click(getByText('๋“ฑ๋กํ•˜๊ธฐ'));

expect(dispatch).toBeCalledTimes(1);

expect(mockPush).toBeCalledWith('/introduce/1');
});
});

context('without group', () => {
given('group', () => (null));
it('dispatch action submit event', () => {
const { getByText } = renderWriteButtonsContainer();

fireEvent.click(getByText('๋“ฑ๋กํ•˜๊ธฐ'));

expect(mockPush).not.toBeCalled();
});
});
});
});
5 changes: 2 additions & 3 deletions src/pages/WritePage.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import TagFormContainer from '../containers/write/TagsFormContainer';
import WriteButtonsContainer from '../containers/write/WriteButtonsContainer';
import WriteFormContainer from '../containers/write/WriteFormContainer';

import Responsive from '../styles/Responsive';
Expand All @@ -10,9 +11,7 @@ const IntroducePage = () => (
<h1>์Šคํ„ฐ๋”” ๊ทธ๋ฃน ๊ฐœ์„คํ•˜๊ธฐ</h1>
<WriteFormContainer />
<TagFormContainer />
<div>
<button type="button">์ €์žฅ</button>
</div>
<WriteButtonsContainer />
</Responsive>
);
export default IntroducePage;
10 changes: 8 additions & 2 deletions src/pages/WritePage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ describe('WritePage', () => {
});

it('renders write form tag', () => {
const { getByPlaceholderText, getByText } = renderWritePage();
const { getByPlaceholderText } = renderWritePage();

expect(getByPlaceholderText('์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”')).not.toBeNull();
expect(getByPlaceholderText('๋‚ด์šฉ')).not.toBeNull();
expect(getByText('์ €์žฅ')).not.toBeNull();
});

it('renders tag form text', () => {
Expand All @@ -46,5 +45,12 @@ describe('WritePage', () => {
expect(getByPlaceholderText('ํƒœ๊ทธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”')).not.toBeNull();
expect(container).toHaveTextContent('ํƒœ๊ทธ');
});

it('renders buttons', () => {
const { getByText } = renderWritePage();

expect(getByText('๋“ฑ๋กํ•˜๊ธฐ')).not.toBeNull();
expect(getByText('์ทจ์†Œ')).not.toBeNull();
});
});
});
20 changes: 20 additions & 0 deletions src/reducers/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createSlice } from '@reduxjs/toolkit';
import {
getStudyGroup,
getStudyGroups,
postStudyGroup,
} from '../services/api';

const writeInitialState = {
Expand Down Expand Up @@ -52,13 +53,22 @@ const { actions, reducer } = createSlice({
},
};
},
clearWriteFields(state) {
return {
...state,
writeField: {
...writeInitialState,
},
};
},
},
});

export const {
setStudyGroups,
setStudyGroup,
changeWriteField,
clearWriteFields,
} = actions;

export const loadStudyGroups = (tag) => async (dispatch) => {
Expand All @@ -75,4 +85,14 @@ export const loadStudyGroup = (id) => async (dispatch) => {
dispatch(setStudyGroup(group));
};

export const writeStudyGroup = () => async (dispatch, getState) => {
const { writeField } = getState();

// NOTE: ํ˜„์žฌ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์ด ์—†๋Š” ๊ด€๊ณ„๋กœ ์ž„์˜๋กœ ์ž‘์„ฑ์ž(moderatorId)๋ฅผ ๋„ฃ์–ด์คŒ
const group = await postStudyGroup({ ...writeField, moderatorId: 'user1' });
Comment on lines +91 to +92
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  • ์ถ”ํ›„ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ํ›„ ๋ณ€๊ฒจํ•ด์ฃผ๊ธฐ


dispatch(setStudyGroup(group));
dispatch(clearWriteFields());
};

export default reducer;
40 changes: 39 additions & 1 deletion src/reducers/slice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import reducer, {
setStudyGroup,
loadStudyGroup,
changeWriteField,
writeStudyGroup,
clearWriteFields,
} from './slice';

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

const middlewares = [thunk];
const mockStore = configureStore(middlewares);
Expand Down Expand Up @@ -108,6 +111,24 @@ describe('reducer', () => {
expect(state.writeField.tags).toEqual(['JavaScript', 'React']);
});
});

describe('clearWriteFields', () => {
const initialState = {
writeField: {
title: 'ํƒ€์ดํ‹€',
contents: '๋‚ด์šฉ',
},
};

it('clears fields of write', () => {
const state = reducer(initialState, clearWriteFields());

const { writeField: { title, contents } } = state;

expect(title).toBe('');
expect(contents).toBe('');
});
});
});

describe('async actions', () => {
Expand Down Expand Up @@ -141,7 +162,24 @@ describe('async actions', () => {
const actions = store.getActions();

expect(actions[0]).toEqual(setStudyGroup(null));
expect(actions[1]).toEqual(setStudyGroup([]));
expect(actions[1]).toEqual(setStudyGroup(undefined));
});
});

describe('writeStudyGroup', () => {
beforeEach(() => {
store = mockStore({
writeField: WRITE_FORM,
});
});

it('dispatches clearWriteFields', async () => {
await store.dispatch(writeStudyGroup());

const actions = store.getActions();

expect(actions[0]).toEqual(setStudyGroup(undefined));
expect(actions[1]).toEqual(clearWriteFields(undefined));
});
});
});
4 changes: 3 additions & 1 deletion src/services/__mocks__/api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const getStudyGroups = async () => [];

export const getStudyGroup = async () => [];
export const getStudyGroup = async () => {};

export const postStudyGroup = async () => {};
5 changes: 5 additions & 0 deletions src/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ export const getStudyGroup = async (id) => {
const response = await axios.get(`http://localhost:3000/groups/${id}`);
return response.data;
};

export const postStudyGroup = async (post) => {
const response = await axios.post('http://localhost:3000/groups/', post);
return response.data;
};
16 changes: 16 additions & 0 deletions src/services/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios from 'axios';
import {
getStudyGroup,
getStudyGroups,
postStudyGroup,
} from './api';

import STUDY_GROUPS from '../../fixtures/study-groups';
Expand Down Expand Up @@ -39,4 +40,19 @@ describe('api', () => {
);
});
});

describe('postStudyGroup', () => {
beforeEach(() => {
axios.post.mockResolvedValue({ data: STUDY_GROUP });
});

it('returns study group post', async () => {
await expect(postStudyGroup(STUDY_GROUP)).resolves.toEqual(STUDY_GROUP);

expect(axios.post).toHaveBeenCalledWith(
'http://localhost:3000/groups/',
STUDY_GROUP,
);
});
});
});