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

test(packages/vue): add test for tryOnMounted

This commit is contained in:
2024-10-06 21:07:29 +07:00
parent 4bbc3b45a2
commit 6a89239a75

View File

@@ -0,0 +1,59 @@
import { describe, it, vi, expect } from 'vitest';
import { defineComponent, nextTick, type PropType } from 'vue';
import { tryOnMounted } from '.';
import { mount } from '@vue/test-utils';
const ComponentStub = defineComponent({
props: {
callback: {
type: Function as PropType<() => void>,
required: true,
},
},
setup(props) {
tryOnMounted(props.callback);
},
template: `<div></div>`,
});
describe('tryOnMounted', () => {
it('should run the callback when mounted', () => {
const callback = vi.fn();
mount(ComponentStub, {
props: { callback },
});
expect(callback).toHaveBeenCalled();
});
it('should run the callback outside of a component lifecycle', () => {
const callback = vi.fn();
tryOnMounted(callback);
expect(callback).toHaveBeenCalled();
});
it('should run the callback asynchronously', async () => {
const callback = vi.fn();
tryOnMounted(callback, { sync: false });
expect(callback).not.toHaveBeenCalled();
await nextTick();
expect(callback).toHaveBeenCalled();
});
it('should run the callback with a specific target', () => {
const callback = vi.fn();
const component = mount(ComponentStub, {
props: { callback: () => {} },
});
tryOnMounted(callback, { target: component.vm.$ });
expect(callback).toHaveBeenCalled();
});
});