Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regression: #1476 reactive prop object compare #1479

Merged
merged 4 commits into from May 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/mount.ts
Expand Up @@ -2,7 +2,9 @@ import {
h,
createApp,
defineComponent,
reactive,
shallowReactive,
isRef,
FunctionalComponent,
ComponentPublicInstance,
ComponentOptionsWithObjectProps,
Expand Down Expand Up @@ -379,12 +381,22 @@ export function mount(
const MOUNT_COMPONENT_REF = 'VTU_COMPONENT'
// we define props as reactive so that way when we update them with `setProps`
// Vue's reactivity system will cause a rerender.
const props = shallowReactive({
const refs = shallowReactive<Record<string, unknown>>({})
const props = reactive<Record<string, unknown>>({})

Object.entries({
...options?.attrs,
...options?.propsData,
...options?.props,
ref: MOUNT_COMPONENT_REF
}).forEach(([k, v]) => {
if (isRef(v)) {
refs[k] = v
} else {
props[k] = v
}
})

const global = mergeGlobalProperties(options?.global)
if (isObjectComponent(component)) {
component.components = { ...component.components, ...global.components }
Expand All @@ -394,7 +406,7 @@ export function mount(
const Parent = defineComponent({
name: 'VTU_ROOT',
render() {
return h(component, props, slots)
return h(component as ComponentOptions, { ...props, ...refs }, slots)
}
})

Expand Down
26 changes: 26 additions & 0 deletions tests/components/Issue1476.vue
@@ -0,0 +1,26 @@
<template>
<div>
<template v-for="field of availableFields" :key="field.name">
<button class="field" @click="selectedField = field">
{{ field.name }}
</button>
<div
v-if="selectedField === field"
class="selectedField"
>
{{ field.name }}
</div>
</template>
</div>
</template>

<script lang="ts">
export default {
props: {
availableFields: { type: Array, required: true }
},
data: () => ({
selectedField: ''
})
}
</script>
22 changes: 22 additions & 0 deletions tests/props.spec.ts
Expand Up @@ -4,6 +4,7 @@ import PropWithSymbol from './components/PropWithSymbol.vue'
import Hello from './components/Hello.vue'
import { defineComponent, h, isRef, ref } from 'vue'
import Title from './components/FunctionComponent'
import Issue1476 from './components/Issue1476.vue'

describe('props', () => {
it('returns a single prop applied to a component', () => {
Expand Down Expand Up @@ -182,6 +183,27 @@ describe('props', () => {
expect(wrapper.find('span').text()).toBe('Some value')
})

it('should keep props as same object', async () => {
// https://github.com/vuejs/test-utils/issues/1476
const wrapper = mount(Issue1476, {
props: {
availableFields: [{ name: 'Animals' }, { name: 'Cities' }]
}
})

expect(wrapper.find('.subField').exists()).toBe(false)

await wrapper.findAll('.field')[0].trigger('click')

expect(wrapper.find('.selectedField').exists()).toBe(true)
expect(wrapper.find('.selectedField').text()).toBe('Animals')

await wrapper.findAll('.field')[1].trigger('click')

expect(wrapper.find('.selectedField').exists()).toBe(true)
expect(wrapper.find('.selectedField').text()).toBe('Cities')
})

it('returns reactive props on a stubbed component shallow case', async () => {
const Foo = {
name: 'Foo',
Expand Down