🧪 Refactor unit tests for useAsyncState to improve readability and formatting
This commit is contained in:
@@ -8,7 +8,8 @@ describe('useAsyncState', () => {
|
|||||||
|
|
||||||
describe('initial state', () => {
|
describe('initial state', () => {
|
||||||
it('should have correct initial values', () => {
|
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(loading.value).toBe(false);
|
||||||
expect(error.value).toBe(null);
|
expect(error.value).toBe(null);
|
||||||
@@ -74,9 +75,11 @@ describe('useAsyncState', () => {
|
|||||||
const { execute } = useAsyncState({ onError });
|
const { execute } = useAsyncState({ onError });
|
||||||
const mockError = new Error('Test error');
|
const mockError = new Error('Test error');
|
||||||
|
|
||||||
await expect(execute(async () => {
|
await expect(
|
||||||
|
execute(async () => {
|
||||||
throw mockError;
|
throw mockError;
|
||||||
})).rejects.toThrow();
|
})
|
||||||
|
).rejects.toThrow();
|
||||||
|
|
||||||
expect(onError).toHaveBeenCalledWith(mockError);
|
expect(onError).toHaveBeenCalledWith(mockError);
|
||||||
expect(onError).toHaveBeenCalledTimes(1);
|
expect(onError).toHaveBeenCalledTimes(1);
|
||||||
@@ -86,7 +89,8 @@ describe('useAsyncState', () => {
|
|||||||
describe('retry logic', () => {
|
describe('retry logic', () => {
|
||||||
it('should retry failed operations', async () => {
|
it('should retry failed operations', async () => {
|
||||||
const { execute } = useAsyncState({ maxRetries: 2, retryDelay: 10 });
|
const { execute } = useAsyncState({ maxRetries: 2, retryDelay: 10 });
|
||||||
const asyncFn = vi.fn()
|
const asyncFn = vi
|
||||||
|
.fn()
|
||||||
.mockRejectedValueOnce(new Error('First failure'))
|
.mockRejectedValueOnce(new Error('First failure'))
|
||||||
.mockRejectedValueOnce(new Error('Second failure'))
|
.mockRejectedValueOnce(new Error('Second failure'))
|
||||||
.mockResolvedValueOnce('Success');
|
.mockResolvedValueOnce('Success');
|
||||||
@@ -99,7 +103,9 @@ describe('useAsyncState', () => {
|
|||||||
|
|
||||||
it('should fail after max retries exhausted', async () => {
|
it('should fail after max retries exhausted', async () => {
|
||||||
const { execute } = useAsyncState({ maxRetries: 2, retryDelay: 10 });
|
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');
|
await expect(execute(asyncFn)).rejects.toThrow('Persistent failure');
|
||||||
|
|
||||||
@@ -108,7 +114,8 @@ describe('useAsyncState', () => {
|
|||||||
|
|
||||||
it('should respect custom retry count per execution', async () => {
|
it('should respect custom retry count per execution', async () => {
|
||||||
const { execute } = useAsyncState({ retryDelay: 10 });
|
const { execute } = useAsyncState({ retryDelay: 10 });
|
||||||
const asyncFn = vi.fn()
|
const asyncFn = vi
|
||||||
|
.fn()
|
||||||
.mockRejectedValueOnce(new Error('First'))
|
.mockRejectedValueOnce(new Error('First'))
|
||||||
.mockResolvedValueOnce('Success');
|
.mockResolvedValueOnce('Success');
|
||||||
|
|
||||||
@@ -122,7 +129,7 @@ describe('useAsyncState', () => {
|
|||||||
describe('cancel', () => {
|
describe('cancel', () => {
|
||||||
it('should cancel ongoing operation', async () => {
|
it('should cancel ongoing operation', async () => {
|
||||||
const { execute, cancel, loading } = useAsyncState();
|
const { execute, cancel, loading } = useAsyncState();
|
||||||
const asyncFn = vi.fn(async (signal) => {
|
const asyncFn = vi.fn(async signal => {
|
||||||
await new Promise(resolve => setTimeout(resolve, 100));
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||||||
if (signal?.aborted) throw new Error('Aborted');
|
if (signal?.aborted) throw new Error('Aborted');
|
||||||
return 'Success';
|
return 'Success';
|
||||||
@@ -141,7 +148,9 @@ describe('useAsyncState', () => {
|
|||||||
describe('reset', () => {
|
describe('reset', () => {
|
||||||
it('should reset all state to initial values', async () => {
|
it('should reset all state to initial values', async () => {
|
||||||
const initialData = 'initial';
|
const initialData = 'initial';
|
||||||
const { execute, reset, data, error, loading } = useAsyncState({ initialData });
|
const { execute, reset, data, error, loading } = useAsyncState({
|
||||||
|
initialData
|
||||||
|
});
|
||||||
|
|
||||||
await execute(async () => 'new data');
|
await execute(async () => 'new data');
|
||||||
expect(data.value).toBe('new data');
|
expect(data.value).toBe('new data');
|
||||||
|
|||||||
Reference in New Issue
Block a user