Skip to content

Add setComputed method #55

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
eddyerburgh opened this issue Sep 29, 2017 · 16 comments
Closed

Add setComputed method #55

eddyerburgh opened this issue Sep 29, 2017 · 16 comments

Comments

@eddyerburgh
Copy link
Member

We should add a setComputed method to set the value of a computed value.

@blocka
Copy link

blocka commented Sep 29, 2017 via email

@eddyerburgh
Copy link
Member Author

eddyerburgh commented Sep 29, 2017

I agree, most of the time you should test it, but if there's a use case for setting a computed property I think we should add it.

I think @jackmellis described a use case well:

There have been times when a method relies on a computed property that is so complicated it's taken me far outside the purpose of my unit test just to get the right computed value!

@jonnyparris
Copy link

jonnyparris commented Oct 3, 2017

Only just saw this after creating a similar issue on the Avoriaz repo. A setComputed method is exactly what I need.

@Austio
Copy link
Contributor

Austio commented Oct 3, 2017

I can see a pragmatism to this.

I do agree with @blocka though and think that the downside with setComputed is that it is encouraging the use of computed properties in the same way you would use something like props or data. You could end up in a situation where you are unit tested 100% by setting computed and have a component that does not work. Guess it is a black box vs white box test thing.

I'm interested in the scenario/thoughts for needing this and if it would be better to create a separate component that you pass the computed property to as a prop and then test that component?

@jonnyparris
Copy link

@Austio why not make both options available and let people decide for themselves?
Ideally I could have unit level tests for all my computed and method functions (which encourages smaller single-purpose functions) and have a 'component integration test' that checks all of those unit tests playing well together.

@Austio
Copy link
Contributor

Austio commented Oct 4, 2017

@jonnyparris that is a fair point, i think with the feature we should add something to documentation with some potential gotchas with it.

@eddyerburgh
Copy link
Member Author

I made a PR — #84

@lusarz
Copy link
Contributor

lusarz commented Oct 11, 2017

What do you think about adding something like computedMock mounting option:
mount(Component, { computedMock: { someVariable () {return 'mocked value'}}
and use extends inside mount:

const ExtendedComponent = {
  extends: Component,
  computed: params.computed
}
return mount(ExtendedComponent);

It's only skeleton of idea, I'm not sure if it's even possible.

@eddyerburgh
Copy link
Member Author

I think we should add setComputed, because we already have setData, setMethods, and setProps

@kleinjm
Copy link

kleinjm commented Nov 9, 2017

Can you please add this method to docs? Thank you

@eddyerburgh
Copy link
Member Author

eddyerburgh commented Nov 9, 2017

Thanks for reminding me. I'll add it to my list.

If you are able make a PR it would be very helpful!

@jorySsense
Copy link

jorySsense commented Apr 16, 2018

any hope on bringing back this feature,

What if my computed value was set by getters from vuex and im mocking the getters?

And then i want to test a watcher that is listening for changes on that value?

I would want a way to force update the computed value

@blocka
Copy link

blocka commented Apr 17, 2018 via email

@jorySsense
Copy link

jorySsense commented Apr 17, 2018

Sure the works for mounting but you can't trigger the watcher on mount. You need to update the reactive data that the watcher is observing. If it's a computed value, or a getter thats mocked from the store you can't force the computed value to update.

@nathan-kansu
Copy link

Whilst I agree a 'setComputed' method should be added, you actually don't need it.
Just add a 'computed' property along with your other mounting options, like so...

const wrapper = mount(Component, {
  ...
  computed: {
    foo() {
      return 'bar'
    },
  },
})

@jorySsense
Copy link

@nath-codes thats great for mounting, the issue is further explained #331

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

8 participants