Skip to content

Bug: data callback on mount/shallowmount does not work for composition / class component #1879

Closed as not planned
@AWoelfel

Description

@AWoelfel

Describe the bug
i use <script setup/> composite component api and want to use the data : () => Record<string, unknown> callback in mount/shallowmount

To Reproduce
following test fails if used with the component below

it('should handle setData on composition api component', async () => {
    const wrapper = mount(SimpleData)
    expect(wrapper.html()).toEqual(
      '<div>sArray:[\n  "initialA",\n  "initialB"\n  ]|s:initialC</div>'
    )

    await wrapper.setData({
      dataStringArray: ['setA', 'setB'],
      dataString: 'setC'
    })

    expect(wrapper.html()).toEqual(
      '<div>sArray:[\n  "setA",\n  "setB"\n  ]|s:setC</div>'
    )
  })

Component SimpleData.vue

<template>
  <div>sArray:{{dataStringArray}}|s:{{dataString}}</div>
</template>


<script setup lang="ts">

import {ref} from "vue";

const dataStringArray = ref<string[]>(["initialA","initialB"])
const dataString = ref<string>("initialC")

</script>

while this test (using an inline declaration will not fail)

  it('should handle setData on inline component component', async () => {
    const Component = {
      template: `<div>sArray:{{dataStringArray}}|s:{{dataString}}</div>`,
      data: () => ({
        dataStringArray: ['initialA', 'initialB'],
        dataString: 'initialC'
      })
    }
    const wrapper = mount(Component, {
      data: () => {
        return {
          dataStringArray: ['setA', 'setB'],
          dataString: 'setC'
        }
      }
    })

    expect(wrapper.html()).toEqual(
      '<div>sArray:[\n  "setA",\n  "setB"\n  ]|s:setC</div>'
    )
  })

Expected behavior
both tests should not fail

Related information:

  • @vue/test-utils version: 2.2.4
  • Vue version: 3.2.33
  • node version: 16.18.0
  • npm version: 8.19.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions