🧪 Refactor unit tests for useAsyncState to improve readability and formatting

This commit is contained in:
2026-01-28 22:15:39 +00:00
parent 84ea99c219
commit c91843d828

View File

@@ -8,7 +8,8 @@ describe('useAsyncState', () => {
describe('initial state', () => {
it('should have correct initial values', () => {
const { loading, error, data, isSuccess, isError, isIdle } = useAsyncState();
const { loading, error, data, isSuccess, isError, isIdle } =
useAsyncState();
expect(loading.value).toBe(false);
expect(error.value).toBe(null);
@@ -74,9 +75,11 @@ describe('useAsyncState', () => {
const { execute } = useAsyncState({ onError });
const mockError = new Error('Test error');
await expect(execute(async () => {
throw mockError;
})).rejects.toThrow();
await expect(
execute(async () => {
throw mockError;
})
).rejects.toThrow();
expect(onError).toHaveBeenCalledWith(mockError);
expect(onError).toHaveBeenCalledTimes(1);
@@ -86,7 +89,8 @@ describe('useAsyncState', () => {
describe('retry logic', () => {
it('should retry failed operations', async () => {
const { execute } = useAsyncState({ maxRetries: 2, retryDelay: 10 });
const asyncFn = vi.fn()
const asyncFn = vi
.fn()
.mockRejectedValueOnce(new Error('First failure'))
.mockRejectedValueOnce(new Error('Second failure'))
.mockResolvedValueOnce('Success');
@@ -99,7 +103,9 @@ describe('useAsyncState', () => {
it('should fail after max retries exhausted', async () => {
const { execute } = useAsyncState({ maxRetries: 2, retryDelay: 10 });
const asyncFn = vi.fn().mockRejectedValue(new Error('Persistent failure'));
const asyncFn = vi
.fn()
.mockRejectedValue(new Error('Persistent failure'));
await expect(execute(asyncFn)).rejects.toThrow('Persistent failure');
@@ -108,7 +114,8 @@ describe('useAsyncState', () => {
it('should respect custom retry count per execution', async () => {
const { execute } = useAsyncState({ retryDelay: 10 });
const asyncFn = vi.fn()
const asyncFn = vi
.fn()
.mockRejectedValueOnce(new Error('First'))
.mockResolvedValueOnce('Success');
@@ -122,7 +129,7 @@ describe('useAsyncState', () => {
describe('cancel', () => {
it('should cancel ongoing operation', async () => {
const { execute, cancel, loading } = useAsyncState();
const asyncFn = vi.fn(async (signal) => {
const asyncFn = vi.fn(async signal => {
await new Promise(resolve => setTimeout(resolve, 100));
if (signal?.aborted) throw new Error('Aborted');
return 'Success';
@@ -141,7 +148,9 @@ describe('useAsyncState', () => {
describe('reset', () => {
it('should reset all state to initial values', async () => {
const initialData = 'initial';
const { execute, reset, data, error, loading } = useAsyncState({ initialData });
const { execute, reset, data, error, loading } = useAsyncState({
initialData
});
await execute(async () => 'new data');
expect(data.value).toBe('new data');