Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/app-vitest-full/components/WrapperElement.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script setup lang="ts">
defineProps<{
as: string
}>()
</script>

<template>
<component
:is="as"
v-bind="$attrs"
/>
</template>
11 changes: 11 additions & 0 deletions examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ComponentWithImports from '~/components/ComponentWithImports.vue'
import { BoundAttrs } from '#components'
import DirectiveComponent from '~/components/DirectiveComponent.vue'
import CustomComponent from '~/components/CustomComponent.vue'
import WrapperElement from '~/components/WrapperElement.vue'

const formats = {
ExportDefaultComponent,
Expand Down Expand Up @@ -297,3 +298,13 @@ it('renders links correctly', async () => {

expect(component.html()).toMatchInlineSnapshot(`"<div><a href="/test"> Link with string to prop </a><a href="/test"> Link with object to prop </a></div>"`)
})

it('element should be changed', async () => {
const component = await mountSuspended(WrapperElement, { props: { as: 'div' } })

expect(component.element.tagName).toBe('DIV')

await component.setProps({ as: 'span' })

expect(component.element.tagName).toBe('SPAN')
})
33 changes: 26 additions & 7 deletions src/runtime-utils/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,7 @@ export async function mountSuspended<T>(
(vm as unknown as AugmentedVueInstance).__setProps = (props: Record<string, unknown>) => {
Object.assign(setProps, props)
}
vm.props = new Proxy(vm.props, {
apply: (target, thisValue, args) => {
const component = thisValue.findComponent({ name: 'MountSuspendedComponent' })
return component.props(...args)
},
})
resolve(vm as ReturnType<typeof mount<T>> & { setupState: Record<string, unknown> })
resolve(wrappedMountedWrapper(vm as ReturnType<typeof mount<T>> & { setupState: Record<string, unknown> }))
}),
},
{
Expand Down Expand Up @@ -214,3 +208,28 @@ function cloneProps(props: Record<string, unknown>) {
}
return newProps
}

function wrappedMountedWrapper<T>(wrapper: ReturnType<typeof mount<T>> & { setupState: Record<string, unknown> }) {
const proxy = new Proxy(wrapper, {
get: (target, prop, receiver) => {
if (prop === 'element') {
const component = target.findComponent({ name: 'MountSuspendedComponent' })
return component[prop]
}
else {
return Reflect.get(target, prop, receiver)
}
},
})

for (const key of ['props'] as const) {
Copy link
Collaborator Author

@wattanx wattanx Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using an array so that other methods can also be added.
However, I think isVisible does not need to be proxied. (html and classes might be okay to proxy.

proxy[key] = new Proxy(wrapper[key], {
apply: (target, thisArg, args) => {
const component = thisArg.findComponent({ name: 'MountSuspendedComponent' })
return component[key](...args)
},
})
}

return proxy
}
Loading