-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(react): useAsyncProcessQueue 테스트 코드 추가
- Loading branch information
Showing
2 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/react/src/hooks/useAsyncProcessQueue/useAsyncProcessQueue.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |