-
Notifications
You must be signed in to change notification settings - Fork 3
[Feature] Change form action setting of login page and register page #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
saseungmin
merged 3 commits into
CodeSoom:main
from
saseungmin:auth-form-change-input-fields
Nov 28, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,58 @@ | ||
| import React from 'react'; | ||
|
|
||
| import styled from '@emotion/styled'; | ||
|
|
||
| import Responsive from '../../styles/Responsive'; | ||
|
|
||
| const AuthFormWrapper = styled(Responsive)``; | ||
|
|
||
| const AuthForm = ({ type }) => ( | ||
| <AuthFormWrapper> | ||
| <h2>{type}</h2> | ||
| <input type="text" placeholder="이메일" /> | ||
| <input type="password" placeholder="비밀번호" /> | ||
| {type === 'Register' && ( | ||
| <input type="password" placeholder="비밀번호 확인" /> | ||
| )} | ||
|
|
||
| </AuthFormWrapper> | ||
| ); | ||
| const FORM_TYPE = { | ||
| login: '로그인', | ||
| register: '회원가입', | ||
| }; | ||
|
|
||
| const AuthForm = ({ type, onChange, fields }) => { | ||
| const formType = FORM_TYPE[type]; | ||
|
|
||
| const { userEmail, password } = fields; | ||
|
|
||
| const handleChange = (e) => { | ||
| const { name, value } = e.target; | ||
|
|
||
| onChange({ name, value }); | ||
| }; | ||
|
|
||
| return ( | ||
| <AuthFormWrapper> | ||
| <h2>{formType}</h2> | ||
| <input | ||
| type="text" | ||
| value={userEmail} | ||
| name="userEmail" | ||
| placeholder="이메일" | ||
| autoComplete="email" | ||
| onChange={handleChange} | ||
| /> | ||
| <input | ||
| type="password" | ||
| value={password} | ||
| name="password" | ||
| placeholder="비밀번호" | ||
| autoComplete="password" | ||
| onChange={handleChange} | ||
| /> | ||
| {type === 'register' && ( | ||
| <input | ||
| type="password" | ||
| value={fields.passwordConfirm} | ||
| name="passwordConfirm" | ||
| placeholder="비밀번호 확인" | ||
| autoComplete="new-password" | ||
| onChange={handleChange} | ||
| /> | ||
| )} | ||
| </AuthFormWrapper> | ||
| ); | ||
| }; | ||
|
|
||
| export default AuthForm; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,98 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { render } from '@testing-library/react'; | ||
| import { render, fireEvent } from '@testing-library/react'; | ||
|
|
||
| import AuthForm from './AuthForm'; | ||
|
|
||
| describe('AuthForm', () => { | ||
| const renderAuthForm = ({ type }) => render(( | ||
| <AuthForm type={type} /> | ||
| const handleChange = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| handleChange.mockClear(); | ||
| }); | ||
|
|
||
| const renderAuthForm = ({ type, fields }) => render(( | ||
| <AuthForm | ||
| type={type} | ||
| fields={fields} | ||
| onChange={handleChange} | ||
| /> | ||
| )); | ||
|
|
||
| context('when type is login', () => { | ||
| const login = { | ||
| type: 'Login', | ||
| type: 'login', | ||
| fields: { | ||
| userEmail: 'tktmdals@naver.com', | ||
| password: '1234', | ||
| }, | ||
| }; | ||
|
|
||
| it('renders login form text', () => { | ||
| const { container, getByPlaceholderText } = renderAuthForm(login); | ||
|
|
||
| expect(container).toHaveTextContent('Login'); | ||
| expect(container).toHaveTextContent('로그인'); | ||
| expect(getByPlaceholderText('이메일')).not.toBeNull(); | ||
| expect(getByPlaceholderText('비밀번호')).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('listens event call change', () => { | ||
| const { getByPlaceholderText } = renderAuthForm(login); | ||
|
|
||
| const inputs = [ | ||
| { value: 'seungmin@naver.com', name: 'userEmail', placeholder: '이메일' }, | ||
| { value: '345', name: 'password', placeholder: '비밀번호' }, | ||
| ]; | ||
|
|
||
| inputs.forEach(({ name, value, placeholder }) => { | ||
| const field = getByPlaceholderText(placeholder); | ||
|
|
||
| expect(field).not.toBeNull(); | ||
|
|
||
| fireEvent.change(field, { target: { value, name } }); | ||
|
|
||
| expect(handleChange).toBeCalled(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| context('when type is register', () => { | ||
| const register = { | ||
| type: 'Register', | ||
| type: 'register', | ||
| fields: { | ||
| userEmail: 'tktmdals@naver.com', | ||
| password: '1234', | ||
| passwordConfirm: '1234', | ||
| }, | ||
| }; | ||
|
|
||
| it('renders register form text', () => { | ||
| const { container, getByPlaceholderText } = renderAuthForm(register); | ||
|
|
||
| expect(container).toHaveTextContent('Register'); | ||
| expect(container).toHaveTextContent('회원가입'); | ||
| expect(getByPlaceholderText('이메일')).not.toBeNull(); | ||
| expect(getByPlaceholderText('비밀번호')).not.toBeNull(); | ||
| expect(getByPlaceholderText('비밀번호 확인')).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('listens event call change', () => { | ||
| const { getByPlaceholderText } = renderAuthForm(register); | ||
|
|
||
| const inputs = [ | ||
| { value: 'seungmin@naver.com', name: 'userEmail', placeholder: '이메일' }, | ||
| { value: '345', name: 'password', placeholder: '비밀번호' }, | ||
| { value: '345', name: 'passwordConfirm', placeholder: '비밀번호 확인' }, | ||
| ]; | ||
|
|
||
| inputs.forEach(({ name, value, placeholder }) => { | ||
| const field = getByPlaceholderText(placeholder); | ||
|
|
||
| expect(field).not.toBeNull(); | ||
|
|
||
| fireEvent.change(field, { target: { value, name } }); | ||
|
|
||
| expect(handleChange).toBeCalled(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import React, { useCallback } from 'react'; | ||
|
|
||
| import { useSelector, useDispatch } from 'react-redux'; | ||
|
|
||
| import { get } from '../../util/utils'; | ||
| import { changeAuthField } from '../../reducers/slice'; | ||
|
|
||
| import AuthForm from '../../components/auth/AuthForm'; | ||
|
|
||
| const LoginFormContainer = () => { | ||
| const dispatch = useDispatch(); | ||
|
|
||
| const login = useSelector(get('login')); | ||
|
|
||
| const onChangeLoginField = useCallback(({ name, value }) => { | ||
| dispatch( | ||
| changeAuthField({ | ||
| form: 'login', | ||
| name, | ||
| value, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| return ( | ||
| <AuthForm | ||
| type="login" | ||
| fields={login} | ||
| onChange={onChangeLoginField} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default LoginFormContainer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { useDispatch, useSelector } from 'react-redux'; | ||
|
|
||
| import { render, fireEvent } from '@testing-library/react'; | ||
|
|
||
| import LoginFormContainer from './LoginFormContainer'; | ||
|
|
||
| describe('LoginFormContainer', () => { | ||
| const dispatch = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| dispatch.mockClear(); | ||
| useDispatch.mockImplementation(() => dispatch); | ||
|
|
||
| useSelector.mockImplementation((selector) => selector({ | ||
| login: { | ||
| userEmail: '', | ||
| password: '', | ||
| }, | ||
| })); | ||
| }); | ||
|
|
||
| const renderLoginFormContainer = () => render(( | ||
| <LoginFormContainer /> | ||
| )); | ||
|
|
||
| it('renders login form text', () => { | ||
| const { container, getByPlaceholderText } = renderLoginFormContainer(); | ||
|
|
||
| expect(container).toHaveTextContent('로그인'); | ||
| expect(getByPlaceholderText('이메일')).not.toBeNull(); | ||
| expect(getByPlaceholderText('비밀번호')).not.toBeNull(); | ||
| }); | ||
|
|
||
| describe('action dispatch in login page', () => { | ||
| it('change event calls dispatch', () => { | ||
| const { getByPlaceholderText } = renderLoginFormContainer(); | ||
|
|
||
| const inputs = [ | ||
| { value: 'seungmin@naver.com', name: 'userEmail', placeholder: '이메일' }, | ||
| { value: '345', name: 'password', placeholder: '비밀번호' }, | ||
| ]; | ||
|
|
||
| inputs.forEach(({ name, value, placeholder }) => { | ||
| const field = getByPlaceholderText(placeholder); | ||
|
|
||
| expect(field).not.toBeNull(); | ||
|
|
||
| fireEvent.change(field, { target: { value, name } }); | ||
|
|
||
| expect(dispatch).toBeCalledWith({ | ||
| type: 'application/changeAuthField', | ||
| payload: { | ||
| form: 'login', | ||
| name, | ||
| value, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.