Skip to content

Commit

Permalink
test(react): useAsyncProcessQueue 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 committed May 12, 2024
1 parent f004d19 commit 044f74e
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/react/src/hooks/useAsyncProcessQueue/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Nullable } from '@modern-kit/types';
import { useCallback, useState, useRef } from 'react';

type RequestFunction<T> = (requestData?: any) => Promise<T>;
type RequestFunction<T> = (...args: any[]) => Promise<T>;

interface UseAsyncProcessQueueOptions {
keepPreviousData?: boolean;
Expand All @@ -22,12 +22,12 @@ export const useAsyncProcessQueue = <T = unknown, E = unknown>({
}

const requestFunc = requestQueue.current[0];
setIsLoading(true);

try {
setIsLoading(true);
const res = await requestFunc();

setData(res);
setData(res as T);
setError(null);
} catch (err) {
setData(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { Mock } from 'vitest';
import { useAsyncProcessQueue } from '.';
import { act, renderHook } from '@testing-library/react';

beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

const createTestPromiseFunc =
(fn: Mock<any, any>, time: number, value?: any, isError = false) =>
() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
fn();
if (isError) {
reject(value);
} else {
resolve(value);
}
}, time);
});
};

describe('useAsyncProcessQueue', () => {
it('should execute multiple asynchronous functions sequentially, and the resolved data should be assigned to data state', async () => {
const mockFn1 = vi.fn();
const mockFn2 = vi.fn();

const { result } = renderHook(() =>
useAsyncProcessQueue({ keepPreviousData: false })
);
const addToProcessQueue = result.current.addToProcessQueue;

const testPromise1 = createTestPromiseFunc(mockFn1, 200, 'foo');
const testPromise2 = createTestPromiseFunc(mockFn2, 200, 'bar');

act(() => {
addToProcessQueue(testPromise1);
});
act(() => {
addToProcessQueue(testPromise2);
});

await vi.advanceTimersByTimeAsync(200);

expect(mockFn1).toBeCalledTimes(1);
expect(mockFn2).toBeCalledTimes(0);
expect(result.current.isLoading).toBeTruthy();
expect(result.current.data).toBeNull();

await vi.advanceTimersByTimeAsync(200);

expect(result.current.data).toBe('foo');
expect(mockFn2).toBeCalledTimes(1);

await vi.advanceTimersByTimeAsync(200);

expect(result.current.data).toBe('bar');
expect(result.current.isLoading).toBeFalsy();

// Initialize data if keepPreviousData is false
act(() => {
addToProcessQueue(testPromise1);
});
expect(result.current.data).toBeNull();
});

it('should keep the previous data if the keepPreviousData prop is true', async () => {
const mockFn1 = vi.fn();
const { result } = renderHook(() =>
useAsyncProcessQueue({ keepPreviousData: true })
);
const addToProcessQueue = result.current.addToProcessQueue;

const testPromise1 = createTestPromiseFunc(mockFn1, 200, 'foo');

act(() => {
addToProcessQueue(testPromise1);
});

await vi.advanceTimersByTimeAsync(200);

expect(result.current.data).toBeNull();

await vi.advanceTimersByTimeAsync(200);

expect(result.current.data).toBe('foo');

await vi.advanceTimersByTimeAsync(200);

expect(result.current.isLoading).toBeFalsy();

// Keep data if keepPreviousData is true
act(() => {
addToProcessQueue(testPromise1);
});
expect(result.current.data).toBe('foo');
});

it('should execute multiple asynchronous functions sequentially, and assign any errors to the error state', async () => {
const mockFn1 = vi.fn();

const { result } = renderHook(() => useAsyncProcessQueue());
const addToProcessQueue = result.current.addToProcessQueue;

const testPromise = createTestPromiseFunc(mockFn1, 200, 'bar', true);

act(() => {
addToProcessQueue(testPromise);
});

await vi.advanceTimersByTimeAsync(200);

expect(mockFn1).toBeCalledTimes(1);

await vi.advanceTimersByTimeAsync(200);
expect(result.current.error).toBe('bar');
expect(result.current.data).toBeNull();
});
});

0 comments on commit 044f74e

Please sign in to comment.