2022-02-21 14:28:22 +08:00
|
|
|
import { defineComponent } from 'vue'
|
2020-09-19 20:44:07 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2022-03-25 15:35:56 +08:00
|
|
|
import { afterEach, describe, expect, fn, it, vi } from 'vitest'
|
2021-11-29 15:58:44 +08:00
|
|
|
import { useAttrs } from '../use-attrs'
|
2020-09-19 20:44:07 +08:00
|
|
|
|
|
|
|
const CLASS = 'a'
|
|
|
|
const WIDTH = '50px'
|
|
|
|
const TEST_KEY = 'test'
|
|
|
|
const TEST_VALUE = 'fake'
|
2020-12-07 13:21:23 +08:00
|
|
|
const ANOTHER_TEST_VALUE = 'fake1'
|
2020-09-19 20:44:07 +08:00
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
const handleClick = fn()
|
2020-09-19 20:44:07 +08:00
|
|
|
|
|
|
|
const genComp = (
|
|
|
|
inheritAttrs = true,
|
|
|
|
excludeListeners = false,
|
2021-09-04 19:29:28 +08:00
|
|
|
excludeKeys: string[] = []
|
2020-09-19 20:44:07 +08:00
|
|
|
) => {
|
2022-02-21 14:28:22 +08:00
|
|
|
return defineComponent({
|
2020-09-19 20:44:07 +08:00
|
|
|
inheritAttrs,
|
|
|
|
props: {},
|
|
|
|
setup() {
|
2020-12-07 10:57:48 +08:00
|
|
|
const attrs = useAttrs({ excludeListeners, excludeKeys })
|
2022-02-21 14:28:22 +08:00
|
|
|
return () => (
|
|
|
|
<div>
|
|
|
|
<span {...attrs.value} />
|
|
|
|
</div>
|
|
|
|
)
|
2020-09-19 20:44:07 +08:00
|
|
|
},
|
2022-02-21 14:28:22 +08:00
|
|
|
})
|
2020-09-19 20:44:07 +08:00
|
|
|
}
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
const _mount = (Comp: ReturnType<typeof genComp>) => {
|
2020-09-19 20:44:07 +08:00
|
|
|
return mount({
|
2022-02-21 14:28:22 +08:00
|
|
|
setup() {
|
|
|
|
return () => (
|
|
|
|
<Comp
|
|
|
|
class={CLASS}
|
|
|
|
style={{ width: WIDTH }}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-expect-error
|
|
|
|
onClick={handleClick}
|
|
|
|
{...{ [TEST_KEY]: TEST_VALUE }}
|
|
|
|
/>
|
|
|
|
)
|
2020-09-19 20:44:07 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('useAttrs', () => {
|
2022-02-21 14:28:22 +08:00
|
|
|
afterEach(() => {
|
|
|
|
vi.restoreAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('class and style should not bind to child node', async () => {
|
2020-12-07 13:21:23 +08:00
|
|
|
const wrapper = _mount(genComp())
|
2020-09-19 20:44:07 +08:00
|
|
|
const container = wrapper.element as HTMLDivElement
|
|
|
|
const span = wrapper.find('span')
|
|
|
|
|
|
|
|
expect(wrapper.classes(CLASS)).toBe(true)
|
|
|
|
expect(container.style.width).toBe(WIDTH)
|
|
|
|
expect(span.classes(CLASS)).toBe(false)
|
|
|
|
expect(span.element.style.width).toBe('')
|
|
|
|
expect(span.attributes(TEST_KEY)).toBe(TEST_VALUE)
|
|
|
|
|
|
|
|
await span.trigger('click')
|
|
|
|
|
|
|
|
expect(handleClick).toBeCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
it("child node's attributes should update when prop change", async () => {
|
2020-12-07 13:21:23 +08:00
|
|
|
const wrapper = _mount(genComp())
|
|
|
|
const span = wrapper.find('span')
|
|
|
|
|
|
|
|
await wrapper.setProps({ [TEST_KEY]: ANOTHER_TEST_VALUE })
|
|
|
|
expect(span.attributes(TEST_KEY)).toBe(ANOTHER_TEST_VALUE)
|
|
|
|
})
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
it('excluded listeners should not bind to child node', async () => {
|
2020-09-19 20:44:07 +08:00
|
|
|
const wrapper = _mount(genComp(true, true))
|
|
|
|
const span = wrapper.find('span')
|
|
|
|
|
|
|
|
await span.trigger('click')
|
|
|
|
|
|
|
|
expect(handleClick).toBeCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
it('excluded attributes should not bind to child node', async () => {
|
2020-09-19 20:44:07 +08:00
|
|
|
const wrapper = _mount(genComp(true, false, [TEST_KEY]))
|
|
|
|
const span = wrapper.find('span')
|
|
|
|
|
|
|
|
expect(span.attributes(TEST_KEY)).toBeUndefined()
|
|
|
|
})
|
|
|
|
})
|