-
Notifications
You must be signed in to change notification settings - Fork 667
Remove WrapperArray #354
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
Comments
I think we should keep WrapperArray as part of the library. The reason for a WrapperArray is that it improves error handling, which makes the library more user friendly. With an array, if you try to access a wrapper that doesn't exist: findAll('div')[1].props() // throws error Cannot read property 'props' of undefined With a WrapperArray: findAll('div').at(1).props() // throws error [vue-test-utils]: no item exists at 1 We could improve the error message to include the selector that wasn't found: findAll('div').at(1).props() // throws error [vue-test-utils]: no div exists at index 1 I think the WrapperArray is useful, just for this reason. |
Thank you for reply. IMHO, Since there is WrapperArray.wrappers, findAll() returning an array of Wrapper is not absolutely necessary. I think it is difficult to change this, after version 1.0 released. |
I tend to agree with @38elements. Personally, I don't find much use of the |
An error message thrown by vue-test-utils lets you know there isn't an error in your test code. This makes the process of writing tests much quicker. Instead of wondering if you used |
I'm not sure if
is not clear enough. Looking at the current implementation of WrapperArray, I see two main issues:
Where I come from (backend frameworks), |
If the return value of |
I'm strongly against returning an array from Let me put down my thoughts. Benefits of WrapperArray
I'll go into this in more detail. Downsides of WrapperArray
The API can be a problem for new users. The second issue, that it's difficult to filter, can be fixed by adding a Note an array exists on a WrapperArray object for users who want to use arrays rather than the WrapperArray object Improved error handlingWhen you use If you follow TDD, you write the test before you right the code. Say I'm testing that all the second const pTags = wrapper.findAll('p')
expect(pTags.at(1).visible()).toBe(true) When I run my test I'll get the error tags to my component. If we returned an array from const pTags = wrapper.findAll('p')
expect(pTags[1].visible()).toBe(true) When I run my test I'll get the error That custom error saves me from having to check my code is correct after I run the test. When the custom error is thrown, I know it's failing because the component code is incorrect, not because the test code is incorrect. Imagine you're refactoring a component. You remove an tag without realizing, and you get some failing tests with a message of If Similar APIsenzyme wrapper.find('div').at(2).hasClass('some-class') jQuery $wrapper.find('div')[3].hasClass('some-class')
element.querySelectorAll('div').item(3).classList.contains('some-class')
element.querySelectorAll('div')[3].classList.contains('some-class') Methods that throw errorsThe methods on a WrapperArray that throw errors are there to make users aware of methods that can't be called on a WrapperArray, and to show them how to access a node using the A user might be using Utility methodsApart from These are methods like I'm not sure how useful these methods are, and we can discuss whether we should keep them or not. The reasoning behind adding them, instead of throwing an error that told users to access the wrapper with ConclusionIn my opinion, we should continue to make We should add more details to the docs to make the reasoning behind a WrapperArray clear. To improve confusion with the API we could rename WrapperArray to WrapperCollection. To improve usability we could add Wrapper methods like |
Thanks for the very comprehensive argument @eddyerburgh. I'll go through each of them and add my counter-argument 😄 Improved error handlingThe only benefit of WrapperArray as you listed, "improved error handling", is subjective. In fact, your examples are not really convincing, at least to me. Let's take a look at the first example: const pTags = wrapper.findAll('p')
expect(pTags[1].visible()).toBe(true) You're saying:
Not really. To me it suggests the same thing as that thrown by Similar APIsI'm not familiar with enzyme, but from what I see, it only has The same can still be said about Methods that throw errorsI don't think the purpose of these methods needs any explanation. The problems here are API chaos, inconsistency, and most importantly, maintenance, which remain untackled. Utility methodsAgain, these are few, and mostly only one-liner wrappers around the ConclusionIf we decide to keep WrapperArray, or WrapperCollection, we'll need to make it much, much more useful than it currently is. I'm talking about adding the load of array methods, to make it more array-like, and maybe to merge its API with |
My preference—apart from keeping That means it always returns a collection. If there's one node in the collection, the methods will be called on that node. If there are multiple nodes, it will throw an error and tell you to access the node with the |
Mine as well. In short, let's make it behave similarly to other proven solutions, or don't have it at all. |
IMHO, I seem I think changing |
class WrapperArray extends Array {
at() {
// ...
}
}
let wrapperArray = WrapperArray.from(wrappers) |
I agree with @38elements and @phanan that it would be nice to remove WrapperArray and return Array.
or
? It's also not possible to chain, like:
|
I am very sorry. |
I've just run into this. |
I'm also missing the |
You can access the wrappers on the const { wrappers } = wrapper.findAll('div')
const textArr = wrappers.map(w => w.text()) |
Solution like:
seems to be more natural, but not perfect. |
@eddyerburgh What about customization of
|
What problem does this feature solve?
IMHO,
Since WrapperArray has only methods which is the small wrapper method of Array,
it is not absolutly necessary.
I think that the Array of Wrapper is easier to understand than WrapperArray.
I do not think the methods of WrapperArray are often used.
By removing WrapperArray, it become simple and small maintenance cost.
What does the proposed API look like?
findAll() returns Array of Wrapper and WrapperArray is removed.
The text was updated successfully, but these errors were encountered: