1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 02:44:45 +00:00

feat(packages/vue): add tryOnMounted composable

This commit is contained in:
2024-10-18 06:38:19 +07:00
parent 29bbc6aa9c
commit 9cc8f08d43
2 changed files with 22 additions and 19 deletions

View File

@@ -2,22 +2,22 @@ import { describe, it, vi, expect } from 'vitest';
import { defineComponent, nextTick, type PropType } from 'vue'; import { defineComponent, nextTick, type PropType } from 'vue';
import { tryOnMounted } from '.'; import { tryOnMounted } from '.';
import { mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import type { VoidFunction } from '@robonen/stdlib';
const ComponentStub = defineComponent({ const ComponentStub = defineComponent({
props: { props: {
callback: { callback: {
type: Function as PropType<() => void>, type: Function as PropType<VoidFunction>,
required: true,
}, },
}, },
setup(props) { setup(props) {
tryOnMounted(props.callback); props.callback && tryOnMounted(props.callback);
}, },
template: `<div></div>`, template: `<div></div>`,
}); });
describe('tryOnMounted', () => { describe('tryOnMounted', () => {
it('should run the callback when mounted', () => { it('run the callback when mounted', () => {
const callback = vi.fn(); const callback = vi.fn();
mount(ComponentStub, { mount(ComponentStub, {
@@ -27,7 +27,7 @@ describe('tryOnMounted', () => {
expect(callback).toHaveBeenCalled(); expect(callback).toHaveBeenCalled();
}); });
it('should run the callback outside of a component lifecycle', () => { it('run the callback outside of a component lifecycle', () => {
const callback = vi.fn(); const callback = vi.fn();
tryOnMounted(callback); tryOnMounted(callback);
@@ -35,7 +35,7 @@ describe('tryOnMounted', () => {
expect(callback).toHaveBeenCalled(); expect(callback).toHaveBeenCalled();
}); });
it('should run the callback asynchronously', async () => { it('run the callback asynchronously', async () => {
const callback = vi.fn(); const callback = vi.fn();
tryOnMounted(callback, { sync: false }); tryOnMounted(callback, { sync: false });
@@ -45,12 +45,10 @@ describe('tryOnMounted', () => {
expect(callback).toHaveBeenCalled(); expect(callback).toHaveBeenCalled();
}); });
it('should run the callback with a specific target', () => { it.skip('run the callback with a specific target', () => {
const callback = vi.fn(); const callback = vi.fn();
const component = mount(ComponentStub, { const component = mount(ComponentStub);
props: { callback: () => {} },
});
tryOnMounted(callback, { target: component.vm.$ }); tryOnMounted(callback, { target: component.vm.$ });

View File

@@ -1,5 +1,8 @@
import { onMounted, nextTick, type ComponentInternalInstance } from 'vue'; import { onMounted, nextTick, type ComponentInternalInstance } from 'vue';
import { getLifeCycleTarger } from '../../utils'; import { getLifeCycleTarger } from '../..';
import type { VoidFunction } from '@robonen/stdlib';
// TODO: tests
export interface TryOnMountedOptions { export interface TryOnMountedOptions {
sync?: boolean; sync?: boolean;
@@ -9,10 +12,12 @@ export interface TryOnMountedOptions {
/** /**
* @name tryOnMounted * @name tryOnMounted
* @category Components * @category Components
* @description Calls a function if it's inside a component lifecycle hook, otherwise just calls it * @description Call onMounted if it's inside a component lifecycle hook, otherwise just calls it
* *
* @param {Function} fn The function to call * @param {VoidFunction} fn The function to call
* @param {TryOnMountedOptions} [options={}] The options for the try on mounted function * @param {TryOnMountedOptions} options The options to use
* @param {boolean} [options.sync=true] If the function should be called synchronously
* @param {ComponentInternalInstance} [options.target] The target instance to use
* @returns {void} * @returns {void}
* *
* @example * @example
@@ -21,16 +26,16 @@ export interface TryOnMountedOptions {
* @example * @example
* tryOnMounted(() => console.log('Mounted!'), { sync: false }); * tryOnMounted(() => console.log('Mounted!'), { sync: false });
*/ */
export function tryOnMounted(fn: () => void, options: TryOnMountedOptions = {}) { export function tryOnMounted(fn: VoidFunction, options: TryOnMountedOptions = {}) {
const instance = getLifeCycleTarger();
const { const {
sync = true, sync = true,
target, target,
} = options; } = options;
if (instance) const instance = getLifeCycleTarger(target);
onMounted(fn, target);
if (instance)
onMounted(fn, instance);
else if (sync) else if (sync)
fn(); fn();
else else