Skip to content

setComputed does not set computed values #273

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
charliekassel opened this issue Dec 16, 2017 · 6 comments
Closed

setComputed does not set computed values #273

charliekassel opened this issue Dec 16, 2017 · 6 comments
Labels

Comments

@charliekassel
Copy link

Seems to be a bug, I've not tested using Jest.

Using vue-cli webpack tempate.
vue-test-utils@1.0.0-beta.8
Vue 2.5
Karma 1.4.1
Mocha 3.2.0

import {shallow} from 'vue-test-utils'
const cmp = {
  data () {
    return {
      a: 1
    }
  },
  computed: {
    b () {
      return this.a * 2
    }
  }
}

describe('testing vue-test-utils', () => {
  it('has default properties', () => {
    const wrapper = shallow(cmp)
    expect(wrapper.vm.a).to.equal(1)
    expect(wrapper.vm.b).to.equal(2)
  })

  it('sets data', () => {
    const wrapper = shallow(cmp)
    wrapper.setData({a: 2})
    expect(wrapper.vm.a).to.equal(2)
    expect(wrapper.vm.b).to.equal(4)
  })

  it('sets computed', () => {
    const wrapper = shallow(cmp)
    wrapper.setComputed({b: 3})
    expect(wrapper.vm.b).to.equal(3)
  })
})
  testing vue-test-utils
    ✓ has default properties
    ✓ sets data
    ✗ sets computed
	expected 2 to equal 3
@lmiller1990
Copy link
Member

lmiller1990 commented Dec 17, 2017

I put some console.logs inside of the setComputed function in vue test utils, and the value does appear to set -- I guess it's a bug elsewhere. Will keep playing around. I wonder if this is the same problem as #176 ...

edit: some progress, if you do

const wrapper = shallow(comp, {
  computed: { b: () => 3 }
}

It works fine, but not outside the constructor. Hmm...

edit again: this works fine. It seems that referencing a data value messes things up? Weird.

@eddyerburgh
Copy link
Member

Should setComputed make it so the computed function always returns the value that was set until setComputed is called again. Or do you think the computed function in your example return a different value after a changes?

I have a fix if we think calling setComputed should permanently change the computed value returned.

@charliekassel
Copy link
Author

I think if you are setting it for the test, then it should then not be mutable by the original computer getter

@charliekassel
Copy link
Author

So that’s a yes to the first question in plain English :)

@lmiller1990
Copy link
Member

If I do setComputed, I expect it to be the value I set it to. However I think it should reset each test - eg, every it statement, I would expect the component back in it's initial state.

@charliekassel
Copy link
Author

Yes, agreed.

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

No branches or pull requests

3 participants