-
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(useForwardExpose): forward native element (#1632)
- Loading branch information
Showing
2 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { flushPromises, mount } from '@vue/test-utils' | ||
import { describe, expect, it } from 'vitest' | ||
import { Fragment, computed, defineComponent, h, ref } from 'vue' | ||
|
||
import { useForwardExpose } from './useForwardExpose' | ||
|
||
describe('useForwardRef', async () => { | ||
it('should forward plain DOM element ref', async () => { | ||
const comp = defineComponent({ | ||
setup() { | ||
const { forwardRef } = useForwardExpose() | ||
return { forwardRef } | ||
}, | ||
template: ` | ||
<div> | ||
<span :ref="forwardRef"> inner element </span> | ||
</div> | ||
`, | ||
}) | ||
const wrapper = mount( | ||
defineComponent(() => { | ||
const el = ref() | ||
const forwardedRef = computed(() => el.value?.$el) | ||
return () => | ||
h('div', { test: forwardedRef.value }, [h(comp, { ref: el })]) | ||
}), | ||
) | ||
await flushPromises() | ||
expect(wrapper.attributes('test')).toBe('[object HTMLSpanElement]') | ||
}) | ||
it('should forward plain DOM element ref - 2', async () => { | ||
const comp = defineComponent({ | ||
setup() { | ||
const { forwardRef } = useForwardExpose() | ||
return { forwardRef } | ||
}, | ||
template: ` | ||
<div> | ||
<span :ref="_=>forwardRef({$el: _})"> inner element </span> | ||
</div> | ||
`, | ||
}) | ||
const wrapper = mount( | ||
defineComponent(() => { | ||
const el = ref() | ||
const forwardedRef = computed(() => el.value?.$el) | ||
return () => | ||
h('div', { test: forwardedRef.value }, [h(comp, { ref: el })]) | ||
}), | ||
) | ||
await flushPromises() | ||
expect(wrapper.attributes('test')).toBe('[object HTMLSpanElement]') | ||
}) | ||
it('should forward plain DOM element ref - fragment', async () => { | ||
const comp = defineComponent({ | ||
setup() { | ||
const { forwardRef } = useForwardExpose() | ||
return { forwardRef } | ||
}, | ||
template: ` | ||
<div>multiple node root</div> | ||
<div> | ||
<span :ref="forwardRef"> inner element </span> | ||
</div> | ||
`, | ||
}) | ||
const wrapper = mount( | ||
defineComponent(() => { | ||
const el = ref() | ||
const forwardedRef = computed(() => el.value?.$el) | ||
return () => | ||
h('div', { test: forwardedRef.value }, [h(comp, { ref: el })]) | ||
}), | ||
) | ||
await flushPromises() | ||
expect(wrapper.attributes('test')).toBe('[object HTMLSpanElement]') | ||
}) | ||
it('should forward plain DOM element ref - fragment - 2', async () => { | ||
const Frag = defineComponent( | ||
(props, { slots }) => | ||
() => | ||
h(Fragment, {}, [slots.default?.()]), | ||
) | ||
const comp = defineComponent({ | ||
components: { Frag }, | ||
setup() { | ||
const { forwardRef } = useForwardExpose() | ||
return { forwardRef } | ||
}, | ||
template: ` | ||
<Frag> | ||
<Frag> | ||
<div> | ||
<span :ref="forwardRef"> inner element </span> | ||
</div> | ||
</Frag> | ||
</Frag> | ||
`, | ||
}) | ||
const wrapper = mount( | ||
defineComponent(() => { | ||
const el = ref() | ||
const forwardedRef = computed(() => el.value?.$el) | ||
return () => | ||
h('div', { test: forwardedRef.value }, [h(comp, { ref: el })]) | ||
}), | ||
) | ||
await flushPromises() | ||
expect(wrapper.attributes('test')).toBe('[object HTMLSpanElement]') | ||
}) | ||
it('should forward plain DOM element ref - fragment - 3', async () => { | ||
const comp = defineComponent({ | ||
setup() { | ||
const { forwardRef } = useForwardExpose() | ||
return { forwardRef } | ||
}, | ||
template: ` | ||
<div> | ||
<template> | ||
<div> | ||
<span :ref="forwardRef"> inner element </span> | ||
</div> | ||
</template> | ||
</div> | ||
`, | ||
}) | ||
const wrapper = mount( | ||
defineComponent(() => { | ||
const el = ref() | ||
const forwardedRef = computed(() => el.value?.$el) | ||
return () => | ||
h('div', { test: forwardedRef.value }, [h(comp, { ref: el })]) | ||
}), | ||
) | ||
await flushPromises() | ||
expect(wrapper.attributes('test')).toBe('[object HTMLSpanElement]') | ||
}) | ||
it('should forward component instance for component', async () => { | ||
const InnerComp = defineComponent(() => { | ||
return () => h('span', {}, 'inner component') | ||
}) | ||
const comp = defineComponent({ | ||
setup() { | ||
const { forwardRef } = useForwardExpose() | ||
return { forwardRef } | ||
}, | ||
components: { InnerComp }, | ||
template: ` | ||
<div> | ||
<InnerComp :ref="forwardRef" /> | ||
</div> | ||
`, | ||
}) | ||
const wrapper = mount( | ||
defineComponent(() => { | ||
const el = ref() | ||
const forwardedRef = computed(() => el.value?.$el) | ||
return () => | ||
h('div', { test: forwardedRef.value }, [h(comp, { ref: el })]) | ||
}), | ||
) | ||
await flushPromises() | ||
expect(wrapper.attributes('test')).toBe('[object HTMLSpanElement]') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters