Skip to content

Commit

Permalink
test: useBlockMultipleAsyncCalls isError 테스트 코드 추가 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 committed Dec 10, 2024
1 parent a1492b7 commit f0b4d21
Showing 1 changed file with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,14 @@ describe('useBlockMultipleAsyncCalls', () => {
blockMultipleAsyncCalls(mockFn);

await waitFor(async () => {
expect(result.current.isLoading).toBe(true);
expect(result.current.isLoading).toBeTruthy();
expect(mockFn).toHaveBeenCalledTimes(1);
});

vi.advanceTimersByTime(DELAY_TIME);

await waitFor(async () => {
expect(result.current.isLoading).toBeFalsy();
});

vi.advanceTimersByTime(DELAY_TIME);

await waitFor(async () => {
expect(mockFn).toHaveBeenCalledTimes(1);
});
});
Expand All @@ -62,21 +57,24 @@ describe('useBlockMultipleAsyncCalls', () => {
await user.click(button);

await waitFor(async () => {
expect(result.current.isLoading).toBe(true);
expect(result.current.isLoading).toBeTruthy();
expect(mockFn).toHaveBeenCalledTimes(1);
});

vi.advanceTimersByTime(DELAY_TIME);

await waitFor(async () => {
expect(result.current.isLoading).toBeFalsy();
expect(mockFn).toHaveBeenCalledTimes(1);
});
});

it('비동기 작업 중 에러가 발생하면 이후 호출되는 여러 비동기 함수를 차단해야 합니다.', async () => {
const mockFn = vi.fn(async () => {
const errorMockFn = vi.fn(async () => {
throw new Error('비동기 작업 중 에러 발생');
});
const defaultMockFn = vi.fn(async () => await delay(DELAY_TIME));

const { result } = renderHook(useBlockMultipleAsyncCalls);

const { blockMultipleAsyncCalls } = result.current;
Expand All @@ -86,11 +84,27 @@ describe('useBlockMultipleAsyncCalls', () => {

await waitFor(async () => {
try {
await blockMultipleAsyncCalls(mockFn);
await blockMultipleAsyncCalls(errorMockFn); // 에러 비동기 함수 호출
} catch (error) {

Check failure on line 88 in packages/react/src/hooks/useBlockMultipleAsyncCalls/useBlockMultipleAsyncCalls.spec.tsx

View workflow job for this annotation

GitHub Actions / Build-Test

'error' is defined but never used
expect(result.current.isLoading).toBeFalsy();
expect(result.current.isError).toBeTruthy();
}
});

blockMultipleAsyncCalls(defaultMockFn); // 정상 비동기 함수 호출

await waitFor(async () => {
expect(result.current.isLoading).toBeTruthy();
expect(result.current.isError).toBeFalsy();
expect(defaultMockFn).toHaveBeenCalledTimes(1);
});

vi.advanceTimersByTime(DELAY_TIME);

await waitFor(async () => {
expect(result.current.isLoading).toBeFalsy();
expect(result.current.isError).toBeFalsy();
expect(defaultMockFn).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit f0b4d21

Please sign in to comment.