-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feat/#353 유틸함수 테스트 보강 #356
base: main
Are you sure you want to change the base?
Changes from all commits
dcac72a
b7be15b
76781ec
c732e01
3a82107
3031a54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { renderHook, act } from '@testing-library/react'; | ||
import useDebounceEffect from './useDebounceEffect'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('useDebounceEffect 테스트. debounce 제한 딜레이를 0.5초로 둘 경우', () => { | ||
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. 💬 아래 경우에도 통과하고 있는데, 테스트를 어떻게 짜야할지 고민해봐야 할 것 같아요 test('0.499초 지났을 때, 한 번도 실행되지 않는다.', () => {
const fn = jest.fn();
const { rerender } = renderHook(({ deps, delay }) => useDebounceEffect(fn, deps, delay), {
initialProps: { deps: 'dependency1', delay: 500 },
});
expect(fn).not.toBeCalled();
rerender({ deps: 'dependency2', delay: 5000 });
act(() => {
jest.advanceTimersByTime(4000);
});
rerender({ deps: 'dependency4', delay: 5000 });
// 0ms
expect(fn).not.toBeCalled();
act(() => {
jest.advanceTimersByTime(1000);
});
// 1000ms
// fn이 실행되지 않아야 하는데 실행됨
expect(fn).not.toBeCalled();
}); |
||
test('0.499초 지났을 때, 한 번도 실행되지 않는다.', () => { | ||
const fn = jest.fn(); | ||
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. 여기도 useFetch 테스트와 마찬가지로 beforeEach를 활용하면 좋을 것 같아요! |
||
const { rerender } = renderHook(({ deps, delay }) => useDebounceEffect(fn, deps, delay), { | ||
initialProps: { deps: 'dependency1', delay: 500 }, | ||
}); | ||
|
||
expect(fn).not.toBeCalled(); | ||
|
||
rerender({ deps: 'dependency2', delay: 500 }); | ||
rerender({ deps: 'dependency3', delay: 500 }); | ||
rerender({ deps: 'dependency4', delay: 500 }); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(499); | ||
}); | ||
|
||
expect(fn).not.toBeCalled(); | ||
}); | ||
|
||
test('0.5초 지났을 때, 한 번만 실행된다.', () => { | ||
const fn = jest.fn(); | ||
const { rerender } = renderHook(({ deps, delay }) => useDebounceEffect(fn, deps, delay), { | ||
initialProps: { deps: 'dependency1', delay: 500 }, | ||
}); | ||
|
||
expect(fn).not.toBeCalled(); | ||
|
||
rerender({ deps: 'dependency2', delay: 500 }); | ||
rerender({ deps: 'dependency3', delay: 500 }); | ||
rerender({ deps: 'dependency4', delay: 500 }); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(500); | ||
}); | ||
|
||
expect(fn).toBeCalledTimes(1); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import useFetch from './useFetch'; | ||
|
||
describe('useFetch 테스트', () => { | ||
let fetcher: jest.Mock; | ||
|
||
beforeEach(() => { | ||
fetcher = jest.fn(); | ||
}); | ||
|
||
test('fetch가 성공했을 경우, isLoading 은 false, data는 mockData, error은 null이다.', async () => { | ||
const mockData = { key: 'value' }; | ||
fetcher.mockResolvedValueOnce(mockData); | ||
const { result } = renderHook(() => useFetch(fetcher, true)); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
|
||
waitFor(() => { | ||
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. 💬 여기는 await가 안붙고 아래 실패케이스에는 붙는데 어떤 차이인가요? |
||
expect(result.current.isLoading).toBe(false); | ||
expect(result.current.data).toEqual(mockData); | ||
expect(result.current.error).toBe(null); | ||
}); | ||
}); | ||
|
||
test('fetch가 실패했을 경우, isLoading 은 false, datas는 null, error은 mockError다.', async () => { | ||
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. *data |
||
const mockError = { message: 'An error occurred' }; | ||
fetcher.mockRejectedValueOnce(mockError); | ||
const { result } = renderHook(() => useFetch(fetcher, true)); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isLoading).toBe(false); | ||
expect(result.current.data).toBe(null); | ||
expect(result.current.error).toEqual(mockError); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { renderHook, act, waitFor } from '@testing-library/react'; | ||
import { useMutation } from './useMutation'; | ||
|
||
describe('useMutation 테스트', () => { | ||
let mutateFn: jest.Mock; | ||
|
||
beforeEach(() => { | ||
mutateFn = jest.fn(); | ||
}); | ||
|
||
test('fetch가 성공했을 경우, isLoading 은 false, data는 mockData, error은 null이다.', async () => { | ||
const mockData = { key: 'value' }; | ||
mutateFn.mockResolvedValueOnce(mockData); | ||
const { result } = renderHook(() => useMutation(mutateFn)); | ||
|
||
act(() => { | ||
result.current.mutateData(); | ||
}); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isLoading).toBe(false); | ||
expect(result.current.data).toEqual(mockData); | ||
expect(result.current.error).toBe(null); | ||
}); | ||
}); | ||
|
||
test('fetch가 실패했을 경우, isLoading 은 false, datas는 null, error은 mockError다.', async () => { | ||
const mockError = { message: 'An error occurred' }; | ||
mutateFn.mockRejectedValueOnce(mockError); | ||
const { result } = renderHook(() => useMutation(mutateFn)); | ||
|
||
act(() => { | ||
result.current.mutateData(); | ||
}); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isLoading).toBe(false); | ||
expect(result.current.data).toBe(null); | ||
expect(result.current.error).toEqual(mockError); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import formatOrdinals from '@/shared/utils/formatOrdinals'; | ||
|
||
describe('formatOrdinals', () => { | ||
test('숫자 1은 1st를 반환한다.', () => { | ||
expect(formatOrdinals(1)).toBe('1st'); | ||
}); | ||
|
||
test('숫자 2은 1nd를 반환한다.', () => { | ||
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. 테스트명에 오류가 있네요~! ㅎㅎ |
||
expect(formatOrdinals(2)).toBe('2nd'); | ||
}); | ||
|
||
test('숫자 3은 1rd를 반환한다.', () => { | ||
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. 여기도여~ |
||
expect(formatOrdinals(3)).toBe('3rd'); | ||
}); | ||
|
||
test('숫자 4은 4th를 반환한다.', () => { | ||
expect(formatOrdinals(4)).toBe('4th'); | ||
}); | ||
|
||
test('숫자 11은 11th를 반환한다.', () => { | ||
expect(formatOrdinals(11)).toBe('11th'); | ||
}); | ||
}); |
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.
debounce 제한 딜레이를 0.5초로 둘 경우
이 부분은 아래 테스트 명으로 들어가야 될 것 같아요!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.
의도 따로 확인했습니다 ㅎㅎ