-
Notifications
You must be signed in to change notification settings - Fork 264
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
Bug: findAll
returns parent node in result array
#1233
Comments
Hmmm weird, I remember having some PRs around It looks like this line is checking that the parent is not returned, maybe is doesn't work as expected. Are you interested in digging a bit and solving this? |
I think this comes from this line Lines 48 to 50 in d414157
The origin might come from #897 and after some refactoring it has been implemented into But when removing this behavior then this will not work anymore: const parent = wrapper.get('.parent') Because I think this is more a question on how the intention of IMO it should not and it would make it more complicated. Only the edge case in #897 makes sense to me, where you have a |
I'm not a react developer but I looked at enzyme to see what they do and it appears that enzyme's I suppose in their case, they are making a distinction between the Wrapper itself, and the DOM tree within it, and their However, enzyme's approach might make sense for VTU in the case of a multi-root node, ie:
You might want to: // findAll needs to operate on the root for select.a to match anything
expect(wrapper.findAll('select.a option')).toHaveLength(1); Then if you modified the component later to split your selects and you ended up with:
Would the same test still work? If you wanted the behaviour to be the same between single and multi-root nodes, I think the parent would need to be considered part of the search space for the target selector for Unfortunately I think this is a more confusing API. I prefer |
@lmiller1990 I can work on this if you want, but I need your decision. Here are my proposals: 1.
|
I am conflicted. I do not really want to introduce breaking changes, but the example in the OP just seems completely wrong. Your (1) proposal seems to make the most sense. I like to think of
|
@lmiller1990 @freakzlike Actually after some digging I'm not sure we should change current behavior. It is in line with HTML behavior: const root = document.createElement('div')
root.classList.add('parent')
root.innerHTML = `<div>child</div><div>child</div><div>child</div>`
const template = document.createDocumentFragment()
template.appendChild(root)
// This also matches v1 behavior when `find` can return root element
console.log(template.querySelector('div')) // .parent
// This one differs from v1 behavior, but makes sense for me in terms of "unified" logic
console.log(template.querySelectorAll('div').length) // 4 It is a question of mental model - what |
@xanf I think that's still in line with what is being suggested, extending your example to match the OP's example: const parent = template.querySelector('div'); // .parent
console.log(parent.querySelectorAll('div').length); // 3 This should be the expected behaviour, and I think your examples are in line with what's being suggested to treat a component wrapper like a fragment made using the When When wrapper is a DOM element (ie, Or am I mis-understanding something? |
I think we are all on the same page. const template = `
<template>
<div class="parent">
<div>child</div>
<div>child</div>
<div>child</div>
</div>
</template>
`
wrapper.get('.parent').findAll('div') // should be 3 elements
// `.parent` should not return `.parent` when using `findAll` Right? |
Does this happen in rc.17? Was this a regression? I want to release rc.19 but I think we need to fix regressions from rc.18 first. Namely, this (if regression) and #1173. |
@lmiller1990 let's double check if we are on the same page:
Am I correct? If so I could fix that and also update docs to reflect this |
I tested this with rc.16 and both cases are working as you mentioned |
Yes, that seems correct. My fix has a problem as pointed out #1377 (review). We don't have this test which now fails: // https://github.com/vuejs/test-utils/issues/1233
it('finds all divs with findAll', () => {
const wrapper = mount({
template: `
<div class="parent">
<div />
<div />
<div />
</div>
`
})
expect(wrapper.findAll('div').length).toBe(4)
}) I will add a test for this case and see if I can get it passing. |
Describe the bug
findAll
returns the parent node in the result set if it matches the selector when chained fromget
To Reproduce
ParentComponent.vue
ParentComponent.spec.js
result
Expected behavior
findAll
returns only the 3 child nodes in the result array. This is the behaviour ofvue-test-utils@1
Related information:
@vue/test-utils
version:2.0.0-rc.18
Vue
version:3.2.26
node
version:16.13.1
npm
(oryarn
) version:[email protected]
Additional context
I'm aware of the open PR reworking
find
andfindAll
, as well as a few mentions in other issues relating tofindAll
of a contributor currently working on changes tofindAll
. Maybe the open PR or the work in progress fixes this, but I thought it was worth documenting.The text was updated successfully, but these errors were encountered: