2018-10-02 06:21:44 +08:00
|
|
|
// using DOM renderer because this case is mostly DOM-specific
|
2019-08-23 05:12:39 +08:00
|
|
|
import {
|
2019-08-24 03:27:17 +08:00
|
|
|
h,
|
2019-08-23 05:12:39 +08:00
|
|
|
render,
|
|
|
|
nextTick,
|
|
|
|
mergeProps,
|
|
|
|
ref,
|
2019-08-23 10:07:51 +08:00
|
|
|
onUpdated,
|
|
|
|
createComponent
|
2019-08-23 05:12:39 +08:00
|
|
|
} from '@vue/runtime-dom'
|
2018-09-25 09:13:06 +08:00
|
|
|
|
|
|
|
describe('attribute fallthrough', () => {
|
2018-10-10 09:10:30 +08:00
|
|
|
it('everything should be in props when component has no declared props', async () => {
|
|
|
|
const click = jest.fn()
|
2018-09-25 09:13:06 +08:00
|
|
|
const childUpdated = jest.fn()
|
|
|
|
|
2019-08-23 05:12:39 +08:00
|
|
|
const Hello = {
|
|
|
|
setup() {
|
|
|
|
const count = ref(0)
|
|
|
|
|
|
|
|
function inc() {
|
|
|
|
count.value++
|
|
|
|
click()
|
|
|
|
}
|
|
|
|
|
|
|
|
return () =>
|
|
|
|
h(Child, {
|
|
|
|
foo: 1,
|
|
|
|
id: 'test',
|
|
|
|
class: 'c' + count.value,
|
|
|
|
style: { color: count.value ? 'red' : 'green' },
|
|
|
|
onClick: inc
|
|
|
|
})
|
2018-09-25 09:13:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 05:12:39 +08:00
|
|
|
const Child = {
|
|
|
|
setup(props: any) {
|
|
|
|
onUpdated(childUpdated)
|
|
|
|
return () =>
|
2018-10-10 09:10:30 +08:00
|
|
|
h(
|
|
|
|
'div',
|
2019-08-23 05:12:39 +08:00
|
|
|
mergeProps(
|
|
|
|
{
|
|
|
|
class: 'c2',
|
|
|
|
style: { fontWeight: 'bold' }
|
|
|
|
},
|
|
|
|
props
|
|
|
|
),
|
2018-10-10 09:10:30 +08:00
|
|
|
props.foo
|
2019-08-23 05:12:39 +08:00
|
|
|
)
|
2018-09-25 09:13:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const root = document.createElement('div')
|
|
|
|
document.body.appendChild(root)
|
2019-08-23 05:12:39 +08:00
|
|
|
render(h(Hello), root)
|
2018-09-25 09:13:06 +08:00
|
|
|
|
|
|
|
const node = root.children[0] as HTMLElement
|
|
|
|
|
2018-10-10 09:10:30 +08:00
|
|
|
expect(node.getAttribute('id')).toBe('test')
|
|
|
|
expect(node.getAttribute('foo')).toBe('1')
|
2018-09-25 09:13:06 +08:00
|
|
|
expect(node.getAttribute('class')).toBe('c2 c0')
|
|
|
|
expect(node.style.color).toBe('green')
|
|
|
|
expect(node.style.fontWeight).toBe('bold')
|
|
|
|
node.dispatchEvent(new CustomEvent('click'))
|
2018-10-10 09:10:30 +08:00
|
|
|
expect(click).toHaveBeenCalled()
|
2018-09-25 09:13:06 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
expect(childUpdated).toHaveBeenCalled()
|
2018-10-10 09:10:30 +08:00
|
|
|
expect(node.getAttribute('id')).toBe('test')
|
|
|
|
expect(node.getAttribute('foo')).toBe('1')
|
2018-09-25 09:13:06 +08:00
|
|
|
expect(node.getAttribute('class')).toBe('c2 c1')
|
|
|
|
expect(node.style.color).toBe('red')
|
|
|
|
expect(node.style.fontWeight).toBe('bold')
|
|
|
|
})
|
|
|
|
|
2019-08-23 10:07:51 +08:00
|
|
|
it('should separate in attrs when component has declared props', async () => {
|
|
|
|
const click = jest.fn()
|
|
|
|
const childUpdated = jest.fn()
|
|
|
|
|
|
|
|
const Hello = {
|
|
|
|
setup() {
|
|
|
|
const count = ref(0)
|
|
|
|
|
|
|
|
function inc() {
|
|
|
|
count.value++
|
|
|
|
click()
|
|
|
|
}
|
|
|
|
|
|
|
|
return () =>
|
|
|
|
h(Child, {
|
|
|
|
foo: 1,
|
|
|
|
id: 'test',
|
|
|
|
class: 'c' + count.value,
|
|
|
|
style: { color: count.value ? 'red' : 'green' },
|
|
|
|
onClick: inc
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Child = createComponent({
|
|
|
|
props: {
|
|
|
|
foo: Number
|
|
|
|
},
|
|
|
|
setup(props, { attrs }) {
|
|
|
|
onUpdated(childUpdated)
|
|
|
|
return () =>
|
|
|
|
h(
|
|
|
|
'div',
|
|
|
|
mergeProps(
|
|
|
|
{
|
|
|
|
class: 'c2',
|
|
|
|
style: { fontWeight: 'bold' }
|
|
|
|
},
|
|
|
|
attrs
|
|
|
|
),
|
|
|
|
props.foo
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const root = document.createElement('div')
|
|
|
|
document.body.appendChild(root)
|
|
|
|
render(h(Hello), root)
|
|
|
|
|
|
|
|
const node = root.children[0] as HTMLElement
|
|
|
|
|
|
|
|
// with declared props, any parent attr that isn't a prop falls through
|
|
|
|
expect(node.getAttribute('id')).toBe('test')
|
|
|
|
expect(node.getAttribute('class')).toBe('c2 c0')
|
|
|
|
expect(node.style.color).toBe('green')
|
|
|
|
expect(node.style.fontWeight).toBe('bold')
|
|
|
|
node.dispatchEvent(new CustomEvent('click'))
|
|
|
|
expect(click).toHaveBeenCalled()
|
|
|
|
|
|
|
|
// ...while declared ones remain props
|
|
|
|
expect(node.hasAttribute('foo')).toBe(false)
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
expect(childUpdated).toHaveBeenCalled()
|
|
|
|
expect(node.getAttribute('id')).toBe('test')
|
|
|
|
expect(node.getAttribute('class')).toBe('c2 c1')
|
|
|
|
expect(node.style.color).toBe('red')
|
|
|
|
expect(node.style.fontWeight).toBe('bold')
|
|
|
|
|
|
|
|
expect(node.hasAttribute('foo')).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fallthrough on multi-nested components', async () => {
|
|
|
|
const click = jest.fn()
|
|
|
|
const childUpdated = jest.fn()
|
|
|
|
const grandChildUpdated = jest.fn()
|
|
|
|
|
|
|
|
const Hello = {
|
|
|
|
setup() {
|
|
|
|
const count = ref(0)
|
|
|
|
|
|
|
|
function inc() {
|
|
|
|
count.value++
|
|
|
|
click()
|
|
|
|
}
|
|
|
|
|
|
|
|
return () =>
|
|
|
|
h(Child, {
|
|
|
|
foo: 1,
|
|
|
|
id: 'test',
|
|
|
|
class: 'c' + count.value,
|
|
|
|
style: { color: count.value ? 'red' : 'green' },
|
|
|
|
onClick: inc
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Child = {
|
|
|
|
setup(props: any) {
|
|
|
|
onUpdated(childUpdated)
|
|
|
|
return () => h(GrandChild, props)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const GrandChild = createComponent({
|
|
|
|
props: {
|
|
|
|
foo: Number
|
|
|
|
},
|
|
|
|
setup(props, { attrs }) {
|
|
|
|
onUpdated(grandChildUpdated)
|
|
|
|
return () =>
|
|
|
|
h(
|
|
|
|
'div',
|
|
|
|
mergeProps(
|
|
|
|
{
|
|
|
|
class: 'c2',
|
|
|
|
style: { fontWeight: 'bold' }
|
|
|
|
},
|
|
|
|
attrs
|
|
|
|
),
|
|
|
|
props.foo
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const root = document.createElement('div')
|
|
|
|
document.body.appendChild(root)
|
|
|
|
render(h(Hello), root)
|
|
|
|
|
|
|
|
const node = root.children[0] as HTMLElement
|
|
|
|
|
|
|
|
// with declared props, any parent attr that isn't a prop falls through
|
|
|
|
expect(node.getAttribute('id')).toBe('test')
|
|
|
|
expect(node.getAttribute('class')).toBe('c2 c0')
|
|
|
|
expect(node.style.color).toBe('green')
|
|
|
|
expect(node.style.fontWeight).toBe('bold')
|
|
|
|
node.dispatchEvent(new CustomEvent('click'))
|
|
|
|
expect(click).toHaveBeenCalled()
|
|
|
|
|
|
|
|
// ...while declared ones remain props
|
|
|
|
expect(node.hasAttribute('foo')).toBe(false)
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
expect(childUpdated).toHaveBeenCalled()
|
|
|
|
expect(grandChildUpdated).toHaveBeenCalled()
|
|
|
|
expect(node.getAttribute('id')).toBe('test')
|
|
|
|
expect(node.getAttribute('class')).toBe('c2 c1')
|
|
|
|
expect(node.style.color).toBe('red')
|
|
|
|
expect(node.style.fontWeight).toBe('bold')
|
|
|
|
|
|
|
|
expect(node.hasAttribute('foo')).toBe(false)
|
|
|
|
})
|
2018-09-25 09:13:06 +08:00
|
|
|
})
|