Skip to content

Commit

Permalink
fix: using find after using findAllComponents does not match expectat…
Browse files Browse the repository at this point in the history
…ions

Fixes #484
  • Loading branch information
llllllllllx authored and cexbrayat committed Mar 29, 2021
1 parent 63ea373 commit f6dd259
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ export class VueWrapper<T extends ComponentPublicInstance>
): DOMWrapper<SVGElementTagNameMap[K]>
find<T extends Element>(selector: string): DOMWrapper<T>
find(selector: string): DOMWrapper<Element> {
// force using the parentElement to allow finding the root element
const result = this.parentElement.querySelector(selector)
const result = this.parentElement['__vue_app__']
? // force using the parentElement to allow finding the root element
this.parentElement.querySelector(selector)
: this.element.querySelector(selector)

if (result) {
return new DOMWrapper(result)
}
Expand Down Expand Up @@ -202,7 +205,10 @@ export class VueWrapper<T extends ComponentPublicInstance>
): DOMWrapper<SVGElementTagNameMap[K]>[]
findAll<T extends Element>(selector: string): DOMWrapper<T>[]
findAll(selector: string): DOMWrapper<Element>[] {
const results = this.parentElement.querySelectorAll(selector)
const results = this.parentElement['__vue_app__']
? this.parentElement.querySelectorAll(selector)
: this.element.querySelectorAll(selector)

return Array.from(results).map((element) => new DOMWrapper(element))
}

Expand Down
52 changes: 52 additions & 0 deletions tests/find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ describe('find', () => {
expect(wrapper.find('#my-span').exists()).toBe(true)
})

test('find using current node after findAllComponents', () => {
const ComponentB = defineComponent({
name: 'ComponentB',
template: '<div><slot></slot></div>'
})

const ComponentA = defineComponent({
name: 'ComponentA',
template: `
<div>
<component-b v-for="item in [1, 2]" :key="item">
<input type="text" :value="item">
</component-b>
</div>
`,
components: { ComponentB }
})

const wrapper = mount(ComponentA)
const firstCompB = wrapper.findComponent({ name: 'ComponentB' })
const lastCompB = wrapper.findAllComponents({ name: 'ComponentB' })[1]

expect(firstCompB.find('input').element.value).toBe('1')
expect(lastCompB.find('input').element.value).toBe('2')
})

it('returns the root element in single root element', () => {
const Component = defineComponent({
render() {
Expand Down Expand Up @@ -85,6 +111,32 @@ describe('findAll', () => {
expect(wrapper.findAll('.span')).toHaveLength(2)
})

test('findAll using current node after findAllComponents', () => {
const ComponentB = defineComponent({
name: 'ComponentB',
template: '<div><slot></slot></div>'
})

const ComponentA = defineComponent({
name: 'ComponentA',
template: `
<div>
<component-b v-for="item in [1, 2]" :key="item">
<input type="text" :value="item">
</component-b>
</div>
`,
components: { ComponentB }
})

const wrapper = mount(ComponentA)
const firstCompB = wrapper.findComponent({ name: 'ComponentB' })
const lastCompB = wrapper.findAllComponents({ name: 'ComponentB' })[1]

expect(firstCompB.findAll('input')[0].element.value).toBe('1')
expect(lastCompB.findAll('input')[0].element.value).toBe('2')
})

test('works with suspense', async () => {
const wrapper = mount(SuspenseComponent)

Expand Down

0 comments on commit f6dd259

Please sign in to comment.