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 bf0fac9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 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);
} catch (error) {
await blockMultipleAsyncCalls(errorMockFn); // 에러 비동기 함수 호출
} catch {
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);
});
});
});
1 change: 0 additions & 1 deletion packages/react/src/hooks/useLocalStorage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
useMemo,
useSyncExternalStore,
} from 'react';
import { usePreservedState } from '../usePreservedState';
import {
getServerSnapshot,
getSnapshot,
Expand Down
1 change: 0 additions & 1 deletion packages/react/src/hooks/useStepState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from '@modern-kit/utils';
import { UseStepProps, useStep } from '../useStep';
import { SetStateAction, useCallback, useState } from 'react';
import { usePreservedState } from '../usePreservedState';

interface StorageOptions {
key: string;
Expand Down

0 comments on commit bf0fac9

Please sign in to comment.