import { expect } from 'vitest' import { defineComponent, h, nextTick, type Component } from 'vue' import { flushPromises } from '@vue/test-utils' import { mountSuspended } from '@nuxt/test-utils/runtime' import { UApp } from '#components' /** Mounts a component inside , which modals and toasts need. */ export const mountInApp = (component: Component) => mountSuspended(defineComponent({ render: () => h(UApp, null, { default: () => h(component) }) })) /** Lets promises, reactivity and (async) form validation settle. */ export const settle = async () => { for (let i = 0; i < 4; i++) { await flushPromises() await nextTick() } } /** Modals render in a portal, so query the whole document. */ export const inBody = (selector: string) => document.body.querySelector(selector) export const setInput = async (input: HTMLInputElement, value: string) => { input.value = value input.dispatchEvent(new Event('input', { bubbles: true })) await settle() } export const submitForm = async (form: HTMLFormElement) => { form.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })) await settle() } export const click = async (el: Element | null) => { expect(el, 'element to click').not.toBeNull() ;(el as HTMLElement).click() await settle() }