-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from kkkkkSE/feat/signup
[feat] 회원가입 페이지 구현 및 테스트 코드 작성
- Loading branch information
Showing
10 changed files
with
641 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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,61 @@ | ||
import { fireEvent, screen, waitFor } from '@testing-library/react'; | ||
|
||
import { render } from '../../test-helper'; | ||
|
||
import SignUpForm from './SignUpForm'; | ||
|
||
const context = describe; | ||
|
||
const userType = 'company'; | ||
|
||
const store = { | ||
signUp: jest.fn(), | ||
}; | ||
|
||
jest.mock('../../hooks/useSignUpFormStore', () => () => [{}, store]); | ||
|
||
describe('<SignUpForm />', () => { | ||
it('render sign up form', () => { | ||
render(<SignUpForm | ||
userType={userType} | ||
/>); | ||
|
||
screen.getByLabelText(/비밀번호 확인/); | ||
|
||
screen.getByRole('button', { name: /가입하기/ }); | ||
}); | ||
|
||
context('user type is company', () => { | ||
it('name label is "기업명"', () => { | ||
render(<SignUpForm | ||
userType="company" | ||
/>); | ||
|
||
screen.getByLabelText(/기업명/); | ||
}); | ||
}); | ||
|
||
context('user type is customer', () => { | ||
it('name label is "이름"', () => { | ||
render(<SignUpForm | ||
userType="customer" | ||
/>); | ||
|
||
screen.getByLabelText(/이름/); | ||
}); | ||
}); | ||
|
||
context('when submitting form', () => { | ||
it('execute signUp function in store', async () => { | ||
render(<SignUpForm | ||
userType={userType} | ||
/>); | ||
|
||
fireEvent.submit(screen.getByTestId('sign-up-form')); | ||
|
||
await waitFor(() => { | ||
expect(store.signUp).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains 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,137 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { Controller, useForm } from 'react-hook-form'; | ||
|
||
import styled from 'styled-components'; | ||
|
||
import useSignUpFormStore from '../../hooks/useSignUpFormStore'; | ||
|
||
import { STATIC_ROUTES } from '../../constants/routes'; | ||
|
||
import Button from '../ui/Button'; | ||
import TextBox from '../ui/TextBox'; | ||
import ErrorMessage from '../ui/ErrorMessage'; | ||
|
||
interface SignUpFormProps { | ||
userType: string; | ||
} | ||
|
||
export default function SignUpForm({ userType }: SignUpFormProps) { | ||
const navigate = useNavigate(); | ||
|
||
const [{ errorMessage, done }, store] = useSignUpFormStore(); | ||
|
||
interface FormValues { | ||
name: string; | ||
username: string; | ||
password: string; | ||
confirmPassword: string; | ||
} | ||
|
||
const { control, handleSubmit } = useForm<FormValues>(); | ||
|
||
useEffect(() => { | ||
if (done) { | ||
store.reset(); | ||
|
||
navigate(`${STATIC_ROUTES.LOGIN}?type=${userType}`); | ||
} | ||
}, [done]); | ||
|
||
const handleCheckSpace = ( | ||
inputValue: string, | ||
onChange: (value: string) => void, | ||
) => { | ||
const changeValue = inputValue.includes(' ') | ||
? inputValue.replaceAll(' ', '') | ||
: inputValue; | ||
|
||
onChange(changeValue); | ||
}; | ||
|
||
const onSubmit = (data: FormValues) => { | ||
store.signUp({ | ||
type: userType, | ||
...data, | ||
}); | ||
}; | ||
|
||
return ( | ||
<Container> | ||
<form onSubmit={handleSubmit(onSubmit)} data-testid="sign-up-form"> | ||
<Controller | ||
control={control} | ||
name="name" | ||
render={({ field: { value, onChange } }) => ( | ||
<TextBox | ||
label={userType === 'company' ? '기업명' : '이름'} | ||
value={value} | ||
onChange={onChange} | ||
placeholder="20자 이하" | ||
maxLength={20} | ||
/> | ||
)} | ||
/> | ||
|
||
<Controller | ||
control={control} | ||
name="username" | ||
render={({ field: { value, onChange } }) => ( | ||
<TextBox | ||
label="아이디" | ||
value={value} | ||
onChange={(inputValue) => handleCheckSpace(inputValue, onChange)} | ||
placeholder="6~20자 영문 또는 숫자 조합" | ||
maxLength={20} | ||
/> | ||
)} | ||
/> | ||
|
||
<Controller | ||
control={control} | ||
name="password" | ||
render={({ field: { value, onChange } }) => ( | ||
<TextBox | ||
type="password" | ||
label="비밀번호" | ||
value={value} | ||
onChange={(inputValue) => handleCheckSpace(inputValue, onChange)} | ||
placeholder="8~40자 영문, 숫자 포함" | ||
maxLength={40} | ||
/> | ||
)} | ||
/> | ||
|
||
<Controller | ||
control={control} | ||
name="confirmPassword" | ||
render={({ field: { value, onChange } }) => ( | ||
<TextBox | ||
type="password" | ||
label="비밀번호 확인" | ||
value={value} | ||
onChange={(inputValue) => handleCheckSpace(inputValue, onChange)} | ||
maxLength={40} | ||
/> | ||
)} | ||
/> | ||
|
||
<Button marginTop type="submit"> | ||
가입하기 | ||
</Button> | ||
</form> | ||
|
||
{errorMessage && <ErrorMessage>{errorMessage}</ErrorMessage>} | ||
</Container> | ||
); | ||
} | ||
|
||
const Container = styled.div` | ||
width: 100%; | ||
input::placeholder{ | ||
color:black; | ||
} | ||
`; |
This file contains 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 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,13 @@ | ||
import { container } from 'tsyringe'; | ||
|
||
import { useStore } from 'usestore-ts'; | ||
|
||
import SignUpFormStore from '../stores/SignUpFormStore'; | ||
|
||
const useSignUpFormStore = () => { | ||
const store = container.resolve(SignUpFormStore); | ||
|
||
return useStore(store); | ||
}; | ||
|
||
export default useSignUpFormStore; |
This file contains 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,22 +1,29 @@ | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { useEffect } from 'react'; | ||
|
||
import Button from '../components/ui/Button'; | ||
import TextBox from '../components/ui/TextBox'; | ||
import { useNavigate, useSearchParams } from 'react-router-dom'; | ||
|
||
import { STATIC_ROUTES } from '../constants/routes'; | ||
|
||
import SignUpForm from '../components/sign-up/SignUpForm'; | ||
|
||
export default function SignUpPage() { | ||
const navigate = useNavigate(); | ||
|
||
function SignUpPage() { | ||
const [searchParams] = useSearchParams(); | ||
|
||
const userType = searchParams.get('type'); | ||
const userType = searchParams.get('type') || ''; | ||
|
||
useEffect(() => { | ||
const validUserTypes = ['company', 'customer']; | ||
|
||
if (!validUserTypes.includes(userType)) { | ||
navigate(STATIC_ROUTES.HOME); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<TextBox label={userType === 'company' ? '기업명' : '이름'} value="" /> | ||
<TextBox label="아이디" value="" /> | ||
<TextBox label="비밀번호" value="" /> | ||
<TextBox label="비밀번호 확인" value="" /> | ||
<Button marginTop>가입하기</Button> | ||
</div> | ||
<SignUpForm | ||
userType={userType} | ||
/> | ||
); | ||
} | ||
|
||
export default SignUpPage; |
This file contains 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 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
Oops, something went wrong.