mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
test(web/vue): update useAsyncState to allow optional delay parameter and add tests
This commit is contained in:
209
web/vue/src/composables/useAsyncState/index.test.ts
Normal file
209
web/vue/src/composables/useAsyncState/index.test.ts
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
import { isShallow, nextTick, ref } from 'vue';
|
||||||
|
import { it, expect, describe, vi, beforeEach, afterEach } from 'vitest';
|
||||||
|
import { useAsyncState } from '.';
|
||||||
|
|
||||||
|
describe('useAsyncState', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works with a promise', async () => {
|
||||||
|
const { state, isReady, isLoading, error } = useAsyncState(
|
||||||
|
Promise.resolve('data'),
|
||||||
|
'initial',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(state.value).toBe('initial');
|
||||||
|
expect(isReady.value).toBe(false);
|
||||||
|
expect(isLoading.value).toBe(true);
|
||||||
|
expect(error.value).toBe(null);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(state.value).toBe('data');
|
||||||
|
expect(isReady.value).toBe(true);
|
||||||
|
expect(isLoading.value).toBe(false);
|
||||||
|
expect(error.value).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works with a function returning a promise', async () => {
|
||||||
|
const { state, isReady, isLoading, error } = useAsyncState(
|
||||||
|
() => Promise.resolve('data'),
|
||||||
|
'initial',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(state.value).toBe('initial');
|
||||||
|
expect(isReady.value).toBe(false);
|
||||||
|
expect(isLoading.value).toBe(true);
|
||||||
|
expect(error.value).toBe(null);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(state.value).toBe('data');
|
||||||
|
expect(isReady.value).toBe(true);
|
||||||
|
expect(isLoading.value).toBe(false);
|
||||||
|
expect(error.value).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles errors', async () => {
|
||||||
|
const { state, isReady, isLoading, error } = useAsyncState(
|
||||||
|
Promise.reject(new Error('test-error')),
|
||||||
|
'initial',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(state.value).toBe('initial');
|
||||||
|
expect(isReady.value).toBe(false);
|
||||||
|
expect(isLoading.value).toBe(true);
|
||||||
|
expect(error.value).toBe(null);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(state.value).toBe('initial');
|
||||||
|
expect(isReady.value).toBe(false);
|
||||||
|
expect(isLoading.value).toBe(false);
|
||||||
|
expect(error.value).toEqual(new Error('test-error'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('calls onSuccess callback', async () => {
|
||||||
|
const onSuccess = vi.fn();
|
||||||
|
|
||||||
|
useAsyncState(
|
||||||
|
Promise.resolve('data'),
|
||||||
|
'initial',
|
||||||
|
{ onSuccess },
|
||||||
|
);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(onSuccess).toHaveBeenCalledWith('data');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('calls onError callback', async () => {
|
||||||
|
const onError = vi.fn();
|
||||||
|
const error = new Error('test-error');
|
||||||
|
|
||||||
|
useAsyncState(
|
||||||
|
Promise.reject(error),
|
||||||
|
'initial',
|
||||||
|
{ onError },
|
||||||
|
);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(onError).toHaveBeenCalledWith(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws error if throwError is true', async () => {
|
||||||
|
const error = new Error('test-error');
|
||||||
|
|
||||||
|
const { executeImmediately } = useAsyncState(
|
||||||
|
Promise.reject(error),
|
||||||
|
'initial',
|
||||||
|
{ immediate: false, throwError: true },
|
||||||
|
);
|
||||||
|
|
||||||
|
await expect(() => executeImmediately()).rejects.toThrow(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('resets state on execute if resetOnExecute is true', async () => {
|
||||||
|
const { state, executeImmediately } = useAsyncState(
|
||||||
|
(data: string) => Promise.resolve(data),
|
||||||
|
'initial',
|
||||||
|
{ immediate: false, resetOnExecute: true },
|
||||||
|
);
|
||||||
|
|
||||||
|
await executeImmediately('new data');
|
||||||
|
expect(state.value).toBe('new data');
|
||||||
|
|
||||||
|
executeImmediately('another data');
|
||||||
|
expect(state.value).toBe('initial');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('delays execution with default delay', async () => {
|
||||||
|
const { isLoading, execute } = useAsyncState(
|
||||||
|
() => Promise.resolve('data'),
|
||||||
|
'initial',
|
||||||
|
{ delay: 100, immediate: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
const promise = execute();
|
||||||
|
expect(isLoading.value).toBe(true);
|
||||||
|
|
||||||
|
await vi.advanceTimersByTimeAsync(50);
|
||||||
|
expect(isLoading.value).toBe(true);
|
||||||
|
|
||||||
|
await vi.advanceTimersByTimeAsync(50);
|
||||||
|
await promise;
|
||||||
|
expect(isLoading.value).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('is awaitable', async () => {
|
||||||
|
const { state } = await useAsyncState(
|
||||||
|
Promise.resolve('data'),
|
||||||
|
'initial',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(state.value).toBe('data');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works with executeImmediately', async () => {
|
||||||
|
const { state, isReady, isLoading, error, executeImmediately } = useAsyncState(
|
||||||
|
() => Promise.resolve('data'),
|
||||||
|
'initial',
|
||||||
|
{ immediate: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
executeImmediately();
|
||||||
|
|
||||||
|
expect(state.value).toBe('initial');
|
||||||
|
expect(isLoading.value).toBe(true);
|
||||||
|
expect(isReady.value).toBe(false);
|
||||||
|
expect(error.value).toBe(null);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(state.value).toBe('data');
|
||||||
|
expect(isReady.value).toBe(true);
|
||||||
|
expect(isLoading.value).toBe(false);
|
||||||
|
expect(error.value).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('passes params to the function', async () => {
|
||||||
|
const promiseFn = vi.fn((...args: any[]) => Promise.resolve(args.join(' ')));
|
||||||
|
|
||||||
|
const { executeImmediately } = useAsyncState(
|
||||||
|
promiseFn,
|
||||||
|
'initial',
|
||||||
|
{ immediate: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
await executeImmediately('hello', 'world');
|
||||||
|
|
||||||
|
expect(promiseFn).toHaveBeenCalledWith('hello', 'world');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses shallowRef by default', async () => {
|
||||||
|
const { state } = await useAsyncState(
|
||||||
|
Promise.resolve({ a: 1 }),
|
||||||
|
{ a: 0 },
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(state.value.a).toBe(1);
|
||||||
|
expect(isShallow(state)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses ref when shallow is false', async () => {
|
||||||
|
const { state } = await useAsyncState(
|
||||||
|
Promise.resolve({ a: ref(1) }),
|
||||||
|
{ a: ref(0) },
|
||||||
|
{ shallow: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(state.value.a).toBe(1);
|
||||||
|
expect(isShallow(state)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -16,7 +16,7 @@ export interface UseAsyncStateReturnBase<Data, Params extends any[], Shallow ext
|
|||||||
isLoading: Ref<boolean>;
|
isLoading: Ref<boolean>;
|
||||||
isReady: Ref<boolean>;
|
isReady: Ref<boolean>;
|
||||||
error: Ref<unknown | null>;
|
error: Ref<unknown | null>;
|
||||||
execute: (delay: number, ...params: Params) => Promise<Data>;
|
execute: (delay?: number, ...params: Params) => Promise<Data>;
|
||||||
executeImmediately: (...params: Params) => Promise<Data>;
|
executeImmediately: (...params: Params) => Promise<Data>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,21 +34,31 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
initialState: Data,
|
initialState: Data,
|
||||||
options?: UseAsyncStateOptions<Shallow, Data>,
|
options?: UseAsyncStateOptions<Shallow, Data>,
|
||||||
): UseAsyncStateReturn<Data, Params, Shallow> {
|
): UseAsyncStateReturn<Data, Params, Shallow> {
|
||||||
const state = options?.shallow ? shallowRef(initialState) : ref(initialState);
|
const {
|
||||||
|
delay = 0,
|
||||||
|
shallow = true,
|
||||||
|
immediate = true,
|
||||||
|
resetOnExecute = false,
|
||||||
|
throwError = false,
|
||||||
|
onError,
|
||||||
|
onSuccess,
|
||||||
|
} = options ?? {};
|
||||||
|
|
||||||
|
const state = shallow ? shallowRef(initialState) : ref(initialState);
|
||||||
const error = ref<unknown | null>(null);
|
const error = ref<unknown | null>(null);
|
||||||
const isLoading = ref(false);
|
const isLoading = ref(false);
|
||||||
const isReady = ref(false);
|
const isReady = ref(false);
|
||||||
|
|
||||||
const execute = async (delay = options?.delay ?? 0, ...params: any[]) => {
|
const execute = async (actualDelay = delay, ...params: any[]) => {
|
||||||
if (options?.resetOnExecute)
|
if (resetOnExecute)
|
||||||
state.value = initialState;
|
state.value = initialState;
|
||||||
|
|
||||||
isLoading.value = true;
|
isLoading.value = true;
|
||||||
isReady.value = false;
|
isReady.value = false;
|
||||||
error.value = null;
|
error.value = null;
|
||||||
|
|
||||||
if (delay > 0)
|
if (actualDelay > 0)
|
||||||
await sleep(delay);
|
await sleep(actualDelay);
|
||||||
|
|
||||||
const promise = isFunction(maybePromise) ? maybePromise(...params as Params) : maybePromise;
|
const promise = isFunction(maybePromise) ? maybePromise(...params as Params) : maybePromise;
|
||||||
|
|
||||||
@@ -56,14 +66,14 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
const data = await promise;
|
const data = await promise;
|
||||||
state.value = data;
|
state.value = data;
|
||||||
isReady.value = true;
|
isReady.value = true;
|
||||||
options?.onSuccess?.(data);
|
onSuccess?.(data);
|
||||||
}
|
}
|
||||||
catch (e: unknown) {
|
catch (e: unknown) {
|
||||||
error.value = e;
|
error.value = e;
|
||||||
options?.onError?.(e);
|
onError?.(e);
|
||||||
|
|
||||||
if (options?.throwError)
|
if (throwError)
|
||||||
throw error;
|
throw e;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
isLoading.value = false;
|
isLoading.value = false;
|
||||||
@@ -76,7 +86,7 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
return execute(0, ...params);
|
return execute(0, ...params);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options?.immediate)
|
if (immediate)
|
||||||
execute();
|
execute();
|
||||||
|
|
||||||
const shell = {
|
const shell = {
|
||||||
|
|||||||
Reference in New Issue
Block a user