-
Notifications
You must be signed in to change notification settings - Fork 5
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
[FE] useError, useToast 테스트코드 작성 #387
Changes from all commits
e316891
dda91e8
a3438d0
dafaa91
8fd839a
399f100
792e458
7588615
23c38d4
3320688
c6193f1
cf9072d
e57509a
7ecd473
422925d
bd26428
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,13 @@ import {renderHook, waitFor} from '@testing-library/react'; | |
import {act} from 'react'; | ||
import {MemoryRouter} from 'react-router-dom'; | ||
|
||
import {useError} from '@hooks/useError/useError'; | ||
|
||
import {PASSWORD_LENGTH} from '@constants/password'; | ||
|
||
import {VALID_PASSWORD_FOR_TEST, VALID_TOKEN_FOR_TEST} from '@mocks/validValueForTest'; | ||
|
||
import {ErrorProvider, useError} from '../../ErrorProvider'; | ||
import {ErrorProvider} from '../useError/ErrorProvider'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
import useAuth from './useAuth'; | ||
|
||
|
@@ -34,7 +36,7 @@ describe('useAuth', () => { | |
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.errorResult.error?.errorCode).toBe('TOKEN_NOT_FOUND'); | ||
expect(result.current.errorResult.errorInfo?.errorCode).toBe('TOKEN_NOT_FOUND'); | ||
}); | ||
}); | ||
|
||
|
@@ -48,7 +50,7 @@ describe('useAuth', () => { | |
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.errorResult.error).toBe(null); | ||
expect(result.current.errorResult.errorInfo).toBe(null); | ||
}); | ||
}); | ||
|
||
|
@@ -62,7 +64,7 @@ describe('useAuth', () => { | |
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.errorResult.error?.errorCode).toBe('TOKEN_INVALID'); | ||
expect(result.current.errorResult.errorInfo?.errorCode).toBe('TOKEN_INVALID'); | ||
}); | ||
}); | ||
|
||
|
@@ -76,7 +78,7 @@ describe('useAuth', () => { | |
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.errorResult.error?.errorCode).toBe('TOKEN_EXPIRED'); | ||
expect(result.current.errorResult.errorInfo?.errorCode).toBe('TOKEN_EXPIRED'); | ||
}); | ||
}); | ||
|
||
|
@@ -90,7 +92,7 @@ describe('useAuth', () => { | |
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.errorResult.error?.errorCode).toBe('FORBIDDEN'); | ||
expect(result.current.errorResult.errorInfo?.errorCode).toBe('FORBIDDEN'); | ||
}); | ||
}); | ||
}); | ||
|
@@ -104,7 +106,7 @@ describe('useAuth', () => { | |
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.errorResult.error).toBe(null); | ||
expect(result.current.errorResult.errorInfo).toBe(null); | ||
}); | ||
}); | ||
|
||
|
@@ -116,7 +118,7 @@ describe('useAuth', () => { | |
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.errorResult.error?.errorCode).toBe('EVENT_PASSWORD_FORMAT_INVALID'); | ||
expect(result.current.errorResult.errorInfo?.errorCode).toBe('EVENT_PASSWORD_FORMAT_INVALID'); | ||
}); | ||
}); | ||
|
||
|
@@ -128,7 +130,7 @@ describe('useAuth', () => { | |
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.errorResult.error?.errorCode).toBe('PASSWORD_INVALID'); | ||
expect(result.current.errorResult.errorInfo?.errorCode).toBe('PASSWORD_INVALID'); | ||
}); | ||
}); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import {createContext, useState, useEffect, ReactNode} from 'react'; | ||
|
||
import {SERVER_ERROR_MESSAGES} from '@constants/errorMessage'; | ||
|
||
// 에러 컨텍스트 생성 | ||
export interface ErrorContextType { | ||
clientErrorMessage: string; | ||
setErrorInfo: (error: ErrorInfo) => void; | ||
clearError: (ms?: number) => void; | ||
errorInfo: ErrorInfo | null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 에러가 없으면 null 좋아요~~ |
||
} | ||
|
||
export const ErrorContext = createContext<ErrorContextType | undefined>(undefined); | ||
|
||
// 에러 컨텍스트를 제공하는 프로바이더 컴포넌트 | ||
interface ErrorProviderProps { | ||
children: ReactNode; | ||
callback?: (message: string) => void; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 콜백은 에러메시지를 받아서 어떤 것을 처리해주는 함수인가요!? |
||
} | ||
|
||
export type ErrorInfo = { | ||
errorCode: string; | ||
message: string; | ||
}; | ||
|
||
export const ErrorProvider = ({children, callback}: ErrorProviderProps) => { | ||
const [clientErrorMessage, setClientErrorMessage] = useState(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 errorMessage가 단순 빈 문자열이 아니라 string | null로 에러가 없을 때는 null로 처리해주는 것이 어떨지 제안드립니다! |
||
const [errorInfo, setErrorState] = useState<ErrorInfo | null>(null); | ||
|
||
useEffect(() => { | ||
if (errorInfo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 스타일이지만 errorInfo가 null일 때는 early return을 해주는 것은 어떨까요? |
||
if (isUnhandledError(errorInfo.errorCode)) { | ||
// 에러바운더리로 보내기 | ||
|
||
throw errorInfo; | ||
} | ||
|
||
const message = SERVER_ERROR_MESSAGES[errorInfo.errorCode]; | ||
setClientErrorMessage(message); | ||
// callback(message); | ||
} | ||
}, [errorInfo, callback]); | ||
|
||
const setErrorInfo = (error: ErrorInfo) => { | ||
setClientErrorMessage(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 새로운 error를 setting 해줄 때 client error message를 빈 문자열로 해주는 이유가 궁금해요! |
||
setErrorState(error); | ||
}; | ||
|
||
const clearError = (ms: number = 0) => { | ||
if (errorInfo === null) return; | ||
|
||
setTimeout(() => { | ||
setClientErrorMessage(''); | ||
setErrorState(null); | ||
}, ms); | ||
}; | ||
|
||
return ( | ||
<ErrorContext.Provider value={{errorInfo, clientErrorMessage, setErrorInfo, clearError}}> | ||
{children} | ||
</ErrorContext.Provider> | ||
); | ||
}; | ||
|
||
const isUnhandledError = (errorCode: string) => { | ||
if (errorCode === 'INTERNAL_SERVER_ERROR') return true; | ||
|
||
return SERVER_ERROR_MESSAGES[errorCode] === undefined; | ||
}; |
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.
지금 당장은 토스트가 한 개만 띄워주어서 크게 상관은 없는데 여러 개를 띄워줄 경우 id보다는 class로 짓는 것이 더 안전해보입니다~