-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.test.ts
58 lines (50 loc) · 1.63 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { defineComponent, isVue2, shallowRef } from 'vue-demi'
import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import { useCurrentElement } from '.'
// Manual triggering only works for Vue 3 - https://vueuse.org/shared/computedWithControl/#manual-triggering
describe.skipIf(isVue2)('useCurrentElement', () => {
it('should be defined', () => {
expect(useCurrentElement).toBeDefined()
})
it('should return the root element from the current component', () => {
const wrapper = mount({
template: '<p ref="el">test</p>',
setup() {
const el = shallowRef<HTMLElement>()
const currentElement = useCurrentElement()
return { el, currentElement }
},
})
const vm = wrapper.vm
expect(vm.currentElement).toBe(vm.el)
wrapper.unmount()
})
it('should return the root element from the passed component', () => {
const TestVueComponent = defineComponent({
setup() {
const rootEl = shallowRef<HTMLElement>()
return { rootEl }
},
template: '<div ref="rootEl">Hello world</div>',
})
const wrapper = mount({
components: {
TestVueComponent,
},
template: `<p>
Testing
<TestVueComponent ref="el" />
</p>`,
setup() {
const el = shallowRef()
const currentElementEl = useCurrentElement(el)
return { el, currentElementEl }
},
})
const vm = wrapper.vm
expect(vm.currentElementEl).toBe((vm.el as typeof TestVueComponent).rootEl)
expect((vm.currentElementEl as HTMLElement).textContent).toBe('Hello world')
wrapper.unmount()
})
})