1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 10:54:44 +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 { tryOnMounted } from '.';
import { mount } from '@vue/test-utils';
import type { VoidFunction } from '@robonen/stdlib';
const ComponentStub = defineComponent({
props: {
callback: {
type: Function as PropType<() => void>,
required: true,
type: Function as PropType<VoidFunction>,
},
},
setup(props) {
tryOnMounted(props.callback);
props.callback && tryOnMounted(props.callback);
},
template: `<div></div>`,
});
describe('tryOnMounted', () => {
it('should run the callback when mounted', () => {
it('run the callback when mounted', () => {
const callback = vi.fn();
mount(ComponentStub, {
@@ -27,7 +27,7 @@ describe('tryOnMounted', () => {
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();
tryOnMounted(callback);
@@ -35,7 +35,7 @@ describe('tryOnMounted', () => {
expect(callback).toHaveBeenCalled();
});
it('should run the callback asynchronously', async () => {
it('run the callback asynchronously', async () => {
const callback = vi.fn();
tryOnMounted(callback, { sync: false });
@@ -45,12 +45,10 @@ describe('tryOnMounted', () => {
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 component = mount(ComponentStub, {
props: { callback: () => {} },
});
const component = mount(ComponentStub);
tryOnMounted(callback, { target: component.vm.$ });