Skip to content

Commit

Permalink
fix(runtime): implement setProps
Browse files Browse the repository at this point in the history
resolves #534
  • Loading branch information
danielroe committed Dec 18, 2023
1 parent 8d79927 commit 522f8bf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ export default {
}
},
setup(props) {
const pre = 'X' + props.myProp
return () => h('div', [
h('h1', 'ExportDefaultReturnsRenderComponent'),
h('pre', props.myProp),
h('pre', 'X' + props.myProp)
h('pre', pre)
])
},
}
Expand Down
8 changes: 3 additions & 5 deletions examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,10 @@ describe.each(Object.entries(formats))(`%s`, (name, component) => {
`.trim())
})

// FIXME: https://github.com/nuxt/test-utils/issues/534
it.todo('can be updated with setProps', async () => {
wrapper.setProps({
title: 'updated title'
it.only('can be updated with setProps', async () => {

Check failure on line 141 in examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts

View workflow job for this annotation

GitHub Actions / lint

it.only not permitted
await wrapper.setProps({
myProp: 'updated title'
})
await nextTick()
expect(wrapper.html()).toEqual(`
<div>
<h1>${name}</h1><pre>updated title</pre><pre>XHello nuxt-vitest</pre>
Expand Down
8 changes: 6 additions & 2 deletions src/runtime-utils/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export async function mountSuspended<T>(

let setupContext: SetupContext
let setupState: any
const setProps = reactive<Record<string, any>>({})

let passedProps: Record<string, any>
const wrappedSetup = async (
Expand Down Expand Up @@ -89,7 +90,10 @@ export async function mountSuspended<T>(
{
onResolve: () =>
nextTick().then(() => {
(vm as any).setupState = setupState
(vm as any).setupState = setupState;
(vm as any).__setProps = (props: Record<string, any>) => {
Object.assign(setProps, props)
}
resolve(vm as any)
}),
},
Expand Down Expand Up @@ -122,7 +126,7 @@ export async function mountSuspended<T>(
setup: setup ? (props: Record<string, any>) => wrappedSetup(props, setupContext) : undefined,
}

return () => h(clonedComponent, { ...props, ...attrs }, slots)
return () => h(clonedComponent, { ...defu(setProps, props) as typeof props, ...attrs }, slots)
},
}),
}
Expand Down

0 comments on commit 522f8bf

Please sign in to comment.