Created
June 1, 2022 23:20
-
-
Save wobsoriano/56239c3d738802c7a4273651ee819677 to your computer and use it in GitHub Desktop.
Test Vue 3 composables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineComponent, h, nextTick as vueNextTick } from "vue"; | |
import { mount } from "@vue/test-utils"; | |
import waitFor from 'p-wait-for'; | |
export interface RenderComposableResult<T> { | |
result: T; | |
waitFor: typeof waitFor, | |
nextTick: typeof vueNextTick; | |
} | |
export function renderComposable<T>( | |
composable: () => T | |
): RenderComposableResult<T> { | |
const Child = defineComponent({ | |
setup() { | |
const result = composable(); | |
const wrapper = () => result; | |
return { wrapper }; | |
}, | |
render: () => null | |
}); | |
const wrapper = mount({ | |
render: () => h(Child, { ref: 'child' }), | |
}); | |
return { | |
result: (wrapper.vm.$refs.child as any).wrapper(), | |
waitFor, | |
nextTick: wrapper.vm.$nextTick, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment