Skip to content

feat: export createWrapper and add createWrapperArray functions #328

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
wants to merge 1 commit into from

Conversation

Toilal
Copy link
Contributor

@Toilal Toilal commented Jan 4, 2018

In some use case, it can be handy to manually create Wrapper and WrapperArray instances.

For example, you can find elements with findAll method, then filter them manually with wrappers property and Array.filter method, and finally create a new filtered WrapperArray instance.

const wrapper = mount(SomeComponent)
const filteredSubComponents = createWrapperArray(wrapper.findAll(SubComponent).wrappers.filter(w => !!w.vm.v))
...

@Toilal Toilal force-pushed the create-wrapper-export branch from 8f20b34 to 67a4ba0 Compare January 4, 2018 14:33
@Toilal Toilal force-pushed the create-wrapper-export branch from 67a4ba0 to 284fbb9 Compare January 4, 2018 14:44
@emileber
Copy link
Contributor

emileber commented Jan 5, 2018

Could we export the types as well? Wrapper and WrapperArray

@eddyerburgh
Copy link
Member

eddyerburgh commented Jan 6, 2018

@emileber we could definitely export the types, would you like to make a PR?

Personally I'm hesitant to export these methods. I appreciate it could be useful to create a wrapper array for some users, but it's a low-level function. Most users won't be aware of how to create WrapperArrays, and I don't think we should make them aware.

We only expose a few methods as methods to use:

  • mount
  • shallow
  • createLocalVue

(We also expose components, but they aren't featured in the top-level API in the docs).

I feel like createWrapper and createWrapperArray add a lot of noise, for a niche use-case.

That's my thoughts. If others feel like this is a really valuable feature then we could add, but I'm against adding it to the API.

@Toilal
Copy link
Contributor Author

Toilal commented Jan 8, 2018

By exporting types, do you mean exporting classes ? Because types are already exported for TypeScript users.

If classes Wrapper and WrapperArray are exported it will be just same as exporting createArray and createWrapperArray, but user will have to use new keyword and constructor parameters are less obvious than those factory methods.

But anyway, instead of this pull request, would you consider adding a filtering feature on all methods involving a selector (find/findAll/...) by adding an optional predicate function parameter ? It would really help for my use case (filtering out non-visible elements: #327)

@Toilal
Copy link
Contributor Author

Toilal commented Jan 8, 2018

and maybe a filter method on WrapperArray ?

const wrapper = mount(SomeComponent)
const filteredSubComponents = wrapper.findAll(SubComponent).filter(w => !!w.vm.v)

@38elements
Copy link
Contributor

How about changing wrapperArray.length from a fixed value to getter ?

Currently, wrapperArray.length is a fixed value.

export default class WrapperArray implements BaseWrapper {
wrappers: Array<Wrapper | VueWrapper>;
length: number;
constructor (wrappers: Array<Wrapper | VueWrapper>) {
this.wrappers = wrappers || []
this.length = this.wrappers.length
}

wrapperArray.length becomes getter, as below.

export default class WrapperArray implements BaseWrapper {
  wrappers: Array<Wrapper | VueWrapper>;
  length: number;

  constructor (wrappers: Array<Wrapper | VueWrapper>) {
    this.wrappers = wrappers || []
  }
  
  get length() {
    return this.wrappers.length
  }
  
  // ...
}
const wrapper = mount(SomeComponent)
const wrapperArray = wrapper.findAll(SubComponent)
wrapperArray.wrappers = wrapperArray.wrappers.filter(w => !!w.vm.v)

@Toilal
Copy link
Contributor Author

Toilal commented Jan 8, 2018

You may have a look to #334 as an alternative. I can't see any other use case than "filtering" for manually creating WrapperArray.

@emileber
Copy link
Contributor

emileber commented Jan 8, 2018

@Toilal By exporting the types, I just meant exposing the Wrapper and WrapperArray classes outside of this lib. Making it possible for devs to manually wrap an existing Vue instance and use the wrapper utilities.

Unless it's already possible and I just didn't see where in the doc?

@eddyerburgh
Copy link
Member

@emileber I thought you meant exposing the flow types. I don't think we should expose the Wrapper and WrapperArray classes. What is your use case? Also, please make an issue so we can discuss this further

@eddyerburgh
Copy link
Member

@38elements I like your suggestion 😀

@38elements
Copy link
Contributor

The following code works.

const wrapper = mount(ComponentWithProps)
const foo = wrapper.findAll('div') 
const WrapperArray = foo.constructor
const bar = new WrapperArray(foo.wrappers)
expect(bar.length).to.equal(3)

@emileber
Copy link
Contributor

@38elements While it could work, it could eventually fail if the return value of findAll was to change. Since it's not in the public API, we should avoid such hacks.

@eddyerburgh
Copy link
Member

Thinking about it, I think this could be the best solution to the problems you've had (we could also add the filter method as you suggested @Toilal )

@phanan
Copy link
Member

phanan commented Jan 17, 2018

Chiming in here. Exported Wrapper and WrapperArray can be useful to create additional helpers. Think plugins/utils for vue-test-utils.

@emileber
Copy link
Contributor

@phanan What do you have in mind?

@phanan
Copy link
Member

phanan commented Jan 17, 2018

@emileber I'm writing a helper library on top of vue-test-utils which provides additional methods or wrappers – those may come in handy but don't belong to the core.

@emileber
Copy link
Contributor

emileber commented Jan 17, 2018

@phanan Something like vue-test-utils-utils? hehe See #337 which initially was about exposing Wrapper and WrapperArray.

Copy link
Member

@eddyerburgh eddyerburgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add validation to createWrapper and createWrapperArray when we expose them. Can you add tests that check createWrapper and createWrapperArray work correctly, and that they validate the input. The tests should be top-level (e.g. test/unit/specs/createWrapper.spec.js)

import type Wrapper from './wrapper'
import type VueWrapper from './vue-wrapper'

export default function createWrapperArray (wrappers: Array<Wrapper | VueWrapper>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a few tests for this method and add validation (so check that Wrappers is an array of wrappers and throw an error if not)

@eddyerburgh
Copy link
Member

@Toilal are you able to make the changes?

@38elements
Copy link
Contributor

38elements commented Jan 27, 2018

#328 (comment)

Chiming in here. Exported Wrapper and WrapperArray can be useful to create additional helpers. Think plugins/utils for vue-test-utils.

I agree with this comment.
I think providing Wrapper class and WrapperArray class is better than createWrapper().

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

Successfully merging this pull request may close these issues.

5 participants