mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
feat(configs/oxlint): add linter
This commit is contained in:
@@ -2,7 +2,7 @@ import { describe, it, vi, expect } from 'vitest';
|
||||
import { ref, reactive } from 'vue';
|
||||
import { useAppSharedState } from '.';
|
||||
|
||||
describe('useAppSharedState', () => {
|
||||
describe(useAppSharedState, () => {
|
||||
it('initialize state only once', () => {
|
||||
const stateFactory = (initValue?: number) => {
|
||||
const count = ref(initValue ?? 0);
|
||||
|
||||
@@ -2,7 +2,7 @@ import { isShallow, nextTick, ref } from 'vue';
|
||||
import { it, expect, describe, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { useAsyncState } from '.';
|
||||
|
||||
describe('useAsyncState', () => {
|
||||
describe(useAsyncState, () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
@@ -18,15 +18,15 @@ describe('useAsyncState', () => {
|
||||
);
|
||||
|
||||
expect(state.value).toBe('initial');
|
||||
expect(isReady.value).toBe(false);
|
||||
expect(isLoading.value).toBe(true);
|
||||
expect(isReady.value).toBeFalsy();
|
||||
expect(isLoading.value).toBeTruthy();
|
||||
expect(error.value).toBe(null);
|
||||
|
||||
await nextTick();
|
||||
|
||||
expect(state.value).toBe('data');
|
||||
expect(isReady.value).toBe(true);
|
||||
expect(isLoading.value).toBe(false);
|
||||
expect(isReady.value).toBeTruthy();
|
||||
expect(isLoading.value).toBeFalsy();
|
||||
expect(error.value).toBe(null);
|
||||
});
|
||||
|
||||
@@ -37,15 +37,15 @@ describe('useAsyncState', () => {
|
||||
);
|
||||
|
||||
expect(state.value).toBe('initial');
|
||||
expect(isReady.value).toBe(false);
|
||||
expect(isLoading.value).toBe(true);
|
||||
expect(isReady.value).toBeFalsy();
|
||||
expect(isLoading.value).toBeTruthy();
|
||||
expect(error.value).toBe(null);
|
||||
|
||||
await nextTick();
|
||||
|
||||
expect(state.value).toBe('data');
|
||||
expect(isReady.value).toBe(true);
|
||||
expect(isLoading.value).toBe(false);
|
||||
expect(isReady.value).toBeTruthy();
|
||||
expect(isLoading.value).toBeFalsy();
|
||||
expect(error.value).toBe(null);
|
||||
});
|
||||
|
||||
@@ -56,15 +56,15 @@ describe('useAsyncState', () => {
|
||||
);
|
||||
|
||||
expect(state.value).toBe('initial');
|
||||
expect(isReady.value).toBe(false);
|
||||
expect(isLoading.value).toBe(true);
|
||||
expect(isReady.value).toBeFalsy();
|
||||
expect(isLoading.value).toBeTruthy();
|
||||
expect(error.value).toBe(null);
|
||||
|
||||
await nextTick();
|
||||
|
||||
expect(state.value).toBe('initial');
|
||||
expect(isReady.value).toBe(false);
|
||||
expect(isLoading.value).toBe(false);
|
||||
expect(isReady.value).toBeFalsy();
|
||||
expect(isLoading.value).toBeFalsy();
|
||||
expect(error.value).toEqual(new Error('test-error'));
|
||||
});
|
||||
|
||||
@@ -131,14 +131,14 @@ describe('useAsyncState', () => {
|
||||
);
|
||||
|
||||
const promise = execute();
|
||||
expect(isLoading.value).toBe(true);
|
||||
expect(isLoading.value).toBeTruthy();
|
||||
|
||||
await vi.advanceTimersByTimeAsync(50);
|
||||
expect(isLoading.value).toBe(true);
|
||||
expect(isLoading.value).toBeTruthy();
|
||||
|
||||
await vi.advanceTimersByTimeAsync(50);
|
||||
await promise;
|
||||
expect(isLoading.value).toBe(false);
|
||||
expect(isLoading.value).toBeFalsy();
|
||||
});
|
||||
|
||||
it('is awaitable', async () => {
|
||||
@@ -160,15 +160,15 @@ describe('useAsyncState', () => {
|
||||
executeImmediately();
|
||||
|
||||
expect(state.value).toBe('initial');
|
||||
expect(isLoading.value).toBe(true);
|
||||
expect(isReady.value).toBe(false);
|
||||
expect(isLoading.value).toBeTruthy();
|
||||
expect(isReady.value).toBeFalsy();
|
||||
expect(error.value).toBe(null);
|
||||
|
||||
await nextTick();
|
||||
|
||||
expect(state.value).toBe('data');
|
||||
expect(isReady.value).toBe(true);
|
||||
expect(isLoading.value).toBe(false);
|
||||
expect(isReady.value).toBeTruthy();
|
||||
expect(isLoading.value).toBeFalsy();
|
||||
expect(error.value).toBe(null);
|
||||
});
|
||||
|
||||
@@ -193,7 +193,7 @@ describe('useAsyncState', () => {
|
||||
);
|
||||
|
||||
expect(state.value.a).toBe(1);
|
||||
expect(isShallow(state)).toBe(true);
|
||||
expect(isShallow(state)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('uses ref when shallow is false', async () => {
|
||||
@@ -204,6 +204,6 @@ describe('useAsyncState', () => {
|
||||
);
|
||||
|
||||
expect(state.value.a).toBe(1);
|
||||
expect(isShallow(state)).toBe(false);
|
||||
expect(isShallow(state)).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ref, shallowRef, watch, type Ref, type ShallowRef, type UnwrapRef } from 'vue';
|
||||
import { ref, shallowRef, watch } from 'vue';
|
||||
import type { Ref, ShallowRef, UnwrapRef } from 'vue';
|
||||
import { isFunction, sleep } from '@robonen/stdlib';
|
||||
|
||||
export interface UseAsyncStateOptions<Shallow extends boolean, Data = any> {
|
||||
@@ -103,8 +104,12 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
||||
watch(
|
||||
isLoading,
|
||||
(loading) => {
|
||||
if (loading === false)
|
||||
error.value ? reject(error.value) : resolve(shell);
|
||||
if (loading === false) {
|
||||
if (error.value)
|
||||
reject(error.value);
|
||||
else
|
||||
resolve(shell);
|
||||
}
|
||||
},
|
||||
{
|
||||
immediate: true,
|
||||
@@ -117,6 +122,7 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
||||
|
||||
return {
|
||||
...shell,
|
||||
// eslint-disable-next-line unicorn/no-thenable
|
||||
then(onFulfilled, onRejected) {
|
||||
return waitResolve().then(onFulfilled, onRejected);
|
||||
},
|
||||
|
||||
@@ -35,7 +35,7 @@ function testFactory<Data>(
|
||||
|
||||
// TODO: maybe replace template with passing mock functions to setup
|
||||
|
||||
describe('useContextFactory', () => {
|
||||
describe(useContextFactory, () => {
|
||||
it('provide and inject context correctly', () => {
|
||||
const { Parent } = testFactory('test', useContextFactory('TestContext'));
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { inject as vueInject, provide as vueProvide, type InjectionKey, type App } from 'vue';
|
||||
import { inject as vueInject, provide as vueProvide } from 'vue';
|
||||
import type { InjectionKey, App } from 'vue';
|
||||
import { VueToolsError } from '@/utils';
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@ import { it, expect, describe } from 'vitest';
|
||||
import { ref } from 'vue';
|
||||
import { useCounter } from '.';
|
||||
|
||||
describe('useCounter', () => {
|
||||
describe(useCounter, () => {
|
||||
it('initialize count with the provided initial value', () => {
|
||||
const { count } = useCounter(5);
|
||||
expect(count.value).toBe(5);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ref, toValue, type MaybeRefOrGetter, type Ref } from 'vue';
|
||||
import { ref, toValue } from 'vue';
|
||||
import type { MaybeRefOrGetter, Ref } from 'vue';
|
||||
import { clamp } from '@robonen/stdlib';
|
||||
|
||||
export interface UseCounterOptions {
|
||||
@@ -46,14 +47,17 @@ export function useCounter(
|
||||
max = Number.MAX_SAFE_INTEGER,
|
||||
} = options;
|
||||
|
||||
const increment = (delta = 1) =>
|
||||
const increment = (delta = 1) => {
|
||||
count.value = clamp(count.value + delta, min, max);
|
||||
};
|
||||
|
||||
const decrement = (delta = 1) =>
|
||||
const decrement = (delta = 1) => {
|
||||
count.value = clamp(count.value - delta, min, max);
|
||||
};
|
||||
|
||||
const set = (value: number) =>
|
||||
const set = (value: number) => {
|
||||
count.value = clamp(value, min, max);
|
||||
};
|
||||
|
||||
const get = () => count.value;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useContextFactory } from '../useContextFactory';
|
||||
import type { App, InjectionKey } from 'vue';
|
||||
import type { App } from 'vue';
|
||||
|
||||
export interface useInjectionStoreOptions<Return> {
|
||||
injectionName?: string;
|
||||
|
||||
@@ -2,56 +2,56 @@ import { it, expect, describe } from 'vitest';
|
||||
import { ref } from 'vue';
|
||||
import { useToggle } from '.';
|
||||
|
||||
describe('useToggle', () => {
|
||||
describe(useToggle, () => {
|
||||
it('initialize with false by default', () => {
|
||||
const { value } = useToggle();
|
||||
expect(value.value).toBe(false);
|
||||
expect(value.value).toBeFalsy();
|
||||
});
|
||||
|
||||
it('initialize with the provided initial value', () => {
|
||||
const { value } = useToggle(true);
|
||||
expect(value.value).toBe(true);
|
||||
expect(value.value).toBeTruthy();
|
||||
});
|
||||
|
||||
it('initialize with the provided initial value from a ref', () => {
|
||||
const { value } = useToggle(ref(true));
|
||||
expect(value.value).toBe(true);
|
||||
expect(value.value).toBeTruthy();
|
||||
});
|
||||
|
||||
it('toggle from false to true', () => {
|
||||
const { value, toggle } = useToggle(false);
|
||||
toggle();
|
||||
expect(value.value).toBe(true);
|
||||
expect(value.value).toBeTruthy();
|
||||
});
|
||||
|
||||
it('toggle from true to false', () => {
|
||||
const { value, toggle } = useToggle(true);
|
||||
toggle();
|
||||
expect(value.value).toBe(false);
|
||||
expect(value.value).toBeFalsy();
|
||||
});
|
||||
|
||||
it('toggle multiple times', () => {
|
||||
const { value, toggle } = useToggle(false);
|
||||
toggle();
|
||||
expect(value.value).toBe(true);
|
||||
expect(value.value).toBeTruthy();
|
||||
toggle();
|
||||
expect(value.value).toBe(false);
|
||||
expect(value.value).toBeFalsy();
|
||||
toggle();
|
||||
expect(value.value).toBe(true);
|
||||
expect(value.value).toBeTruthy();
|
||||
});
|
||||
|
||||
it('toggle returns the new value', () => {
|
||||
const { toggle } = useToggle(false);
|
||||
expect(toggle()).toBe(true);
|
||||
expect(toggle()).toBe(false);
|
||||
expect(toggle()).toBeTruthy();
|
||||
expect(toggle()).toBeFalsy();
|
||||
});
|
||||
|
||||
it('set a specific value via toggle', () => {
|
||||
const { value, toggle } = useToggle(false);
|
||||
toggle(true);
|
||||
expect(value.value).toBe(true);
|
||||
expect(value.value).toBeTruthy();
|
||||
toggle(true);
|
||||
expect(value.value).toBe(true);
|
||||
expect(value.value).toBeTruthy();
|
||||
});
|
||||
|
||||
it('use custom truthy and falsy values', () => {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ref, toValue, type MaybeRefOrGetter, type MaybeRef, type Ref } from 'vue';
|
||||
import { ref, toValue } from 'vue';
|
||||
import type { MaybeRefOrGetter, MaybeRef, Ref } from 'vue';
|
||||
|
||||
export interface UseToggleOptions<Truthy, Falsy> {
|
||||
truthyValue?: MaybeRefOrGetter<Truthy>,
|
||||
|
||||
Reference in New Issue
Block a user