Skip to content

Computed props are not reactive when using operation on primitive types, when they are used in template and the data, they are using, are setData() after mount/shallowMount #656

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

Closed
trollepierre opened this issue May 25, 2018 · 1 comment

Comments

@trollepierre
Copy link
Contributor

trollepierre commented May 25, 2018

Version

1.0.0-beta.16

Reproduction link

http://google.com

Steps to reproduce

Computed props are not reactive when using operation on primitive types, when they are used in template and the data, they are using, are setData() after mount/shallowMount

Read the test to understand it

<template>
  <div>
    <child-component
      :workers="computedWorkers"
    />
  </div>
</template>

<script>
  import ChildComponent from './ChildComponent.vue'

  export default {
    name: 'Component',
    components: {
      ChildComponent,
    },
    data: () => ({
      workers: [],
    }),
    computed: {
      computedWorkers() {
        return this.workers.length === 0
      },
    },
  }
</script>

What is expected?

import { mount } from '@vue/test-utils'
import Component from './Component.vue'

describe('components | Component', () => {

  describe('template', () => {

    it('should have a computedWorkers set to false because not empty', () => {
      // Given
      const wrapper = mount(Component)

      // When
      wrapper.setData({ workers: [{}] })

      // Then
      expect(wrapper.vm.$data.workers).toEqual([{}])
      expect(wrapper.vm.computedWorkers).toEqual(false)
    })
  })
})

What is actually happening?

import { mount } from '@vue/test-utils'
import Component from './Component.vue'

describe('components | Component', () => {

  describe('template', () => {

    it('should have a computedWorkers set to false because not empty', () => {
      // Given
      const wrapper = mount(Component)

      // When
      wrapper.setData({ workers: [{}] })

      // Then
      expect(wrapper.vm.$data.workers).toEqual([{}])
      expect(wrapper.vm.computedWorkers).toEqual(true)
    })
  })
})

could you provide a template of CodePen, CodeSandbox if you want a link, please?

@eddyerburgh
Copy link
Member

Thanks for the bug report. This was fixed in #652 and will be release in beta.17. For the moment, you can set the property on the vm directly:

wrapper.vm.workers = [{}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants