Skip to content

Commit

Permalink
Merge pull request #76 from swsnu/mypagetesting
Browse files Browse the repository at this point in the history
Mypagetesting
  • Loading branch information
0114kek authored Dec 10, 2022
2 parents 8a29bfd + a7de640 commit 06ca5de
Show file tree
Hide file tree
Showing 8 changed files with 497 additions and 67 deletions.
88 changes: 60 additions & 28 deletions frontend/src/login/SignUp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,67 @@ describe('SignUp', () => {
</Provider>
);
});
it("should render correctly", async() => {
render(signup);
screen.getByText("밥친구");
screen.getByText("아이디");
screen.getByText("비밀번호");
screen.getByText("비밀번호 재확인");
screen.getByText("이름");
screen.getByText("별명");
screen.getByText("성별");
screen.getByText("생년월일");
screen.getByText("MBTI");
screen.getByText("이메일");
const usernameInput = screen.getByPlaceholderText("영문, 숫자 (5~15자 이내)");
const passwordInput = screen.getByPlaceholderText("영문, 숫자, 특수문자 (8~15자 이내)");
it("should signup correctly", async() => {
const signupRender = render(signup);
const usernameInput = screen.getByPlaceholderText("영문, 숫자 (5~20자 이내)");
fireEvent.input(usernameInput,{target: {value: 'a'}});
fireEvent.input(usernameInput,{target: {value: 'a가나다라마바사'}});
jest.spyOn(axios, 'post').mockResolvedValue({
data:{dup:true},
status: 200,
});
fireEvent.input(usernameInput ,{target: {value: 'testuse'}});
await waitFor(() => expect(axios.post).toHaveBeenCalled());
jest.spyOn(axios, 'post').mockResolvedValue({
data:{dup:false},
status: 200,
});
fireEvent.input(usernameInput ,{target: {value: 'testuser'}});
await waitFor(() => expect(axios.post).toHaveBeenCalled());
const passwordInput = screen.getByPlaceholderText("영문, 숫자, 특수문자 (8~20자 이내)");
const passwordconfirmInput = screen.getByPlaceholderText("비밀번호를 재입력해주세요.");
fireEvent.input(passwordInput,{target: {value: 'a'}});
fireEvent.input(passwordconfirmInput,{target: {value: 'a'}});
fireEvent.input(passwordconfirmInput,{target: {value: 'asdf1234'}});
fireEvent.input(passwordInput,{target: {value: '가나다라마바사아'}});
fireEvent.input(passwordInput,{target: {value: 'asdf1234'}});
const nameInput = screen.getByPlaceholderText("본인의 실명입니다.");
fireEvent.input(nameInput,{target: {value: 'a'}});
fireEvent.input(nameInput,{target: {value: '^^^'}});
fireEvent.input(nameInput,{target: {value: '세종대왕'}});
const emailInput = screen.getByPlaceholderText("youremail");
fireEvent.input(emailInput,{target: {value: '세종대왕'}});
fireEvent.input(emailInput,{target: {value: ''}});
fireEvent.input(emailInput,{target: {value: 'testuser'}});
const birthChange = screen.getByPlaceholderText('YYMMDD 형태로 입력해주세요. 예) 970816');
fireEvent.input(birthChange ,{target: {value: '0000'}});
fireEvent.input(birthChange ,{target: {value: '0000xx'}});
fireEvent.input(birthChange ,{target: {value: '000431'}});
fireEvent.input(birthChange ,{target: {value: '000230'}});
fireEvent.input(birthChange ,{target: {value: '010229'}});
fireEvent.input(birthChange ,{target: {value: '020417'}});
const nicknameChange = screen.getByPlaceholderText('다른 이용자에게 보여질 이름입니다.');
fireEvent.input(nicknameChange ,{target: {value: '0'}});
fireEvent.input(nicknameChange ,{target: {value: 'a a'}});
jest.spyOn(axios, 'post').mockResolvedValue({
data:{dup:true},
status: 200,
});
fireEvent.input(nicknameChange ,{target: {value: 'testuse'}});
await waitFor(() => expect(axios.post).toHaveBeenCalled());
jest.spyOn(axios, 'post').mockResolvedValue({
data:{dup:false},
status: 200,
});
fireEvent.input(nicknameChange ,{target: {value: 'testuser'}});
await waitFor(() => expect(axios.post).toHaveBeenCalled());
const genderChange = signupRender.container.querySelector('#genderChoose');
fireEvent.change(genderChange!, { target: { value: 'F' } });
const mbtiChange = signupRender.container.querySelector('#mbtiChoose');
fireEvent.change(mbtiChange!, { target: { value: 'ISTP' } });
const domainChange = signupRender.container.querySelector('#domainChoose');
fireEvent.change(domainChange!, { target: { value: '@snu.ac.kr' } });
const signupButton = screen.getByText("가입하기");
fireEvent.change(usernameInput,{target: {value: "user1"}});
fireEvent.change(passwordInput,{target:{value: "user1password"}});
await screen.findByDisplayValue("user1"); //values displayed on the screen
await screen.findByDisplayValue("user1password"); //values displayed on the screen
fireEvent.click(signupButton!);
await waitFor(()=>expect(mockDispatch).toHaveBeenCalledTimes(1));
expect(window.alert).toHaveBeenCalledWith('회원 가입이 정상적으로 완료되었습니다.')
Expand All @@ -86,14 +128,4 @@ describe('SignUp', () => {
fireEvent.click(loginlink!);
expect(mockNavigate).toHaveBeenCalledWith('/login');
});
/*
it('gender', () => {
const handleMock = jest.fn();
const useStateMock: any = (useState:any) => [useState, handleMock];
jest.spyOn(React,'useState').mockImplementation(useStateMock);
const signupRender = render(signup);
const genderSelect = signupRender.container.querySelector('#gender');
fireEvent.change(genderSelect!, { target: { value: 'M' } });
expect(handleMock).toHaveBeenCalled();
});*/
});
20 changes: 11 additions & 9 deletions frontend/src/login/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export default function SignUp(){
}
})
.catch((err) => {
console.log(err.response.data);
});
}
};
Expand Down Expand Up @@ -202,7 +201,6 @@ export default function SignUp(){
}
})
.catch((err) => {
console.log(err.response.data);
});
}
};
Expand All @@ -226,6 +224,10 @@ export default function SignUp(){
setBirthErrorMessage('올바른 형식으로 입력해주세요.');
setBirthIsValid(false);
}
else if((~~(+e.target.value/100))%100 === 2 && (~~(+e.target.value))%100 === 29 && (~~(+e.target.value/10000))%4 !== 0) {
setBirthErrorMessage('올바른 형식으로 입력해주세요.');
setBirthIsValid(false);
}
else{
setBirthErrorMessage('');
setBirthIsValid(true);
Expand Down Expand Up @@ -258,12 +260,12 @@ export default function SignUp(){
try{dispatch(setSignUp({username: username, password: password, name: signUpStatus.name,
mbti: signUpStatus.mbti, gender: signUpStatus.gender, nickname: signUpStatus.nickname,
birth: signUpStatus.birth, email: (signUpStatus.email+signUpStatus.domain)}));
alert('회원 가입이 정상적으로 완료되었습니다.');
window.alert('회원 가입이 정상적으로 완료되었습니다.');
navigate('/login');

}
catch(err){
alert('예기치 않은 오류가 발생했습니다.');
window.alert('예기치 않은 오류가 발생했습니다.');
}
}

Expand Down Expand Up @@ -293,8 +295,8 @@ export default function SignUp(){
/>
</h3>
</Card.Header>
<Card.Body>
<Form className="rounded p-4 p-sm-3">
<Card.Body className='p-0'>
<Form className="rounded p-4 p-sm-8">
<Form.Group className="mb-3">
<Form.Label>아이디</Form.Label>
<Form.Control type="username" placeholder="영문, 숫자 (5~20자 이내)" onChange={handleUsername} maxLength={20}/>
Expand Down Expand Up @@ -322,7 +324,7 @@ export default function SignUp(){
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>성별</Form.Label>
<Form.Select id="gender" defaultValue={''} onChange={handleGender}>
<Form.Select name='gender' id="genderChoose" defaultValue={''} onChange={handleGender}>
<option value = '' disabled>선택 안함</option>
<option value = 'M'>남자</option>
<option value = 'F'>여자</option>
Expand All @@ -336,7 +338,7 @@ export default function SignUp(){
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>MBTI</Form.Label>
<Form.Select name='mbti' defaultValue={''} onChange={handleMBTI}>
<Form.Select name='mbti' id='mbtiChoose' defaultValue={''} onChange={handleMBTI}>
<option value = '' disabled>선택 안함</option>
<option value = 'ENTJ'>ENTJ</option>
<option value = 'ENTP'>ENTP</option>
Expand Down Expand Up @@ -364,7 +366,7 @@ export default function SignUp(){
{<span className='errormessage'> {emailErrorMessage}</span>}
</Form.Group>
<Form.Group as={Col}>
<Form.Select name='domain' defaultValue={''} onChange={handleDomain}>
<Form.Select name='domain' id='domainChoose' defaultValue={''} onChange={handleDomain}>
<option value = '' disabled>선택 안함</option>
<option value = '@korea.ac.kr' disabled>@korea.ac.kr (고려대학교)</option>
<option value = '@snu.ac.kr'>@snu.ac.kr (서울대학교)</option>
Expand Down
73 changes: 73 additions & 0 deletions frontend/src/mypage/BlockUser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { getMockStore } from '../test-utils/mocks';
import { Provider } from 'react-redux';
import { fireEvent, render, screen } from '@testing-library/react';
import { MemoryRouter, Route, Routes } from 'react-router';
import {
UserInfoType,
} from '../store/slices/user';
import BlockUser from './BlockUser';
import { statusType } from './MyPage';

const initialState: UserInfoType = {
loggedinuser: null,
userlist: [],
menulist: [],
chosenchatroom: null,
};
interface propsType {
status: statusType;
blockSubmit: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>, nickname:string) => any;
unblockSubmit: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>, nickname:string) => any;
}

const blockSumbitMock = jest.fn();
const unblockSumbitMock = jest.fn();

const props: propsType = {
status: {name: '',
mbti: '',
intro: '',
birth: '',
gender: '',
nickname: '',
matched_users: ['한국'],
blocked_users: ['가나'],
temperature: 0.0,},
blockSubmit: blockSumbitMock,
unblockSubmit: unblockSumbitMock,
};

const mockStore = getMockStore({ user: initialState });

describe('BlockUser', () => {
let blockuser: JSX.Element;
beforeEach(() => {
jest.clearAllMocks();
blockuser = (
<Provider store={mockStore}>
<MemoryRouter>
<Routes>
<Route path='/' element={<BlockUser {...props}/>} />
</Routes>
</MemoryRouter>
</Provider>
);
});
it('should render correctly', async () => {
render(blockuser);
screen.getByText("한국");
screen.getByText("가나");
})
it('block correctly', async () => {
render(blockuser);
const blockClick = screen.getByText("차단");
fireEvent.click(blockClick!);
expect(blockSumbitMock).toHaveBeenCalled();
})
it('unblock correctly', async () => {
render(blockuser);
const unblockClick = screen.getByText("차단 해제");
fireEvent.click(unblockClick!);
expect(unblockSumbitMock).toHaveBeenCalled();
})
});
37 changes: 37 additions & 0 deletions frontend/src/mypage/MyManner.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getMockStore } from '../test-utils/mocks';
import { Provider } from 'react-redux';
import { render, screen } from '@testing-library/react';
import { MemoryRouter, Route, Routes } from 'react-router';
import {
UserInfoType,
} from '../store/slices/user';
import MyManner from './MyManner';

const initialState: UserInfoType = {
loggedinuser: null,
userlist: [],
menulist: [],
chosenchatroom: null,
};

const mockStore = getMockStore({ user: initialState });

describe('MyManner', () => {
let mymanner: JSX.Element;
beforeEach(() => {
jest.clearAllMocks();
mymanner = (
<Provider store={mockStore}>
<MemoryRouter>
<Routes>
<Route path='/' element={<MyManner temperature={36.5}/>} />
</Routes>
</MemoryRouter>
</Provider>
);
});
it('should render correctly', async () => {
render(mymanner);
screen.getByText("당신의 매너 온도는 36.5입니다.");
})
});
Loading

0 comments on commit 06ca5de

Please sign in to comment.