2018-10-02 06:21:44 +08:00
|
|
|
// using DOM renderer because this case is mostly DOM-specific
|
2018-10-27 03:44:50 +08:00
|
|
|
import { h, render, Component, nextTick, cloneVNode } 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()
|
|
|
|
|
2018-10-13 07:49:41 +08:00
|
|
|
class Hello extends Component {
|
|
|
|
count: number = 0
|
2018-09-25 09:13:06 +08:00
|
|
|
inc() {
|
|
|
|
this.count++
|
2018-10-10 09:10:30 +08:00
|
|
|
click()
|
2018-09-25 09:13:06 +08:00
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return h(Child, {
|
|
|
|
foo: 1,
|
|
|
|
id: 'test',
|
|
|
|
class: 'c' + this.count,
|
|
|
|
style: { color: this.count ? 'red' : 'green' },
|
2018-10-10 09:10:30 +08:00
|
|
|
onClick: this.inc
|
2018-09-25 09:13:06 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 07:49:41 +08:00
|
|
|
class Child extends Component<{ [key: string]: any }> {
|
2018-09-25 09:13:06 +08:00
|
|
|
updated() {
|
|
|
|
childUpdated()
|
|
|
|
}
|
2018-10-10 09:10:30 +08:00
|
|
|
render(props: any) {
|
|
|
|
return cloneVNode(
|
|
|
|
h(
|
|
|
|
'div',
|
|
|
|
{
|
|
|
|
class: 'c2',
|
|
|
|
style: { fontWeight: 'bold' }
|
|
|
|
},
|
|
|
|
props.foo
|
|
|
|
),
|
|
|
|
props
|
2018-09-25 09:13:06 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const root = document.createElement('div')
|
|
|
|
document.body.appendChild(root)
|
2018-11-02 05:08:33 +08:00
|
|
|
await 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')
|
|
|
|
})
|
|
|
|
|
2018-10-10 09:10:30 +08:00
|
|
|
it('should separate in attrs when component has declared props', async () => {
|
|
|
|
const click = jest.fn()
|
2018-09-25 09:13:06 +08:00
|
|
|
const childUpdated = jest.fn()
|
|
|
|
|
2018-10-13 07:49:41 +08:00
|
|
|
class Hello extends Component {
|
|
|
|
count = 0
|
2018-09-25 09:13:06 +08:00
|
|
|
inc() {
|
|
|
|
this.count++
|
2018-10-10 09:10:30 +08:00
|
|
|
click()
|
2018-09-25 09:13:06 +08:00
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return h(Child, {
|
2018-10-13 07:49:41 +08:00
|
|
|
foo: 123,
|
2018-09-25 09:13:06 +08:00
|
|
|
id: 'test',
|
|
|
|
class: 'c' + this.count,
|
|
|
|
style: { color: this.count ? 'red' : 'green' },
|
2018-10-10 09:10:30 +08:00
|
|
|
onClick: this.inc
|
2018-09-25 09:13:06 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 07:49:41 +08:00
|
|
|
class Child extends Component<{ [key: string]: any; foo: number }> {
|
2018-10-10 08:22:29 +08:00
|
|
|
static props = {
|
|
|
|
foo: Number
|
2018-09-25 09:13:06 +08:00
|
|
|
}
|
|
|
|
updated() {
|
|
|
|
childUpdated()
|
|
|
|
}
|
2018-10-04 01:00:13 +08:00
|
|
|
render() {
|
2018-10-10 09:10:30 +08:00
|
|
|
return cloneVNode(
|
|
|
|
h(
|
|
|
|
'div',
|
|
|
|
{
|
|
|
|
class: 'c2',
|
|
|
|
style: { fontWeight: 'bold' }
|
|
|
|
},
|
|
|
|
this.$props.foo
|
|
|
|
),
|
|
|
|
this.$attrs
|
2018-09-25 09:13:06 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const root = document.createElement('div')
|
|
|
|
document.body.appendChild(root)
|
2018-11-02 05:08:33 +08:00
|
|
|
await render(h(Hello), root)
|
2018-09-25 09:13:06 +08:00
|
|
|
|
|
|
|
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'))
|
2018-10-10 09:10:30 +08:00
|
|
|
expect(click).toHaveBeenCalled()
|
|
|
|
|
|
|
|
// ...while declared ones remain props
|
|
|
|
expect(node.hasAttribute('foo')).toBe(false)
|
2018-09-25 09:13:06 +08:00
|
|
|
|
|
|
|
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')
|
2018-10-10 09:10:30 +08:00
|
|
|
|
|
|
|
expect(node.hasAttribute('foo')).toBe(false)
|
2018-09-25 09:13:06 +08:00
|
|
|
})
|
2018-10-05 04:52:52 +08:00
|
|
|
|
|
|
|
it('should fallthrough on multi-nested components', async () => {
|
2018-10-10 09:10:30 +08:00
|
|
|
const click = jest.fn()
|
2018-10-05 04:52:52 +08:00
|
|
|
const childUpdated = jest.fn()
|
|
|
|
const grandChildUpdated = jest.fn()
|
|
|
|
|
2018-10-13 07:49:41 +08:00
|
|
|
class Hello extends Component {
|
|
|
|
count = 0
|
2018-10-05 04:52:52 +08:00
|
|
|
inc() {
|
|
|
|
this.count++
|
2018-10-10 09:10:30 +08:00
|
|
|
click()
|
2018-10-05 04:52:52 +08:00
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return h(Child, {
|
|
|
|
foo: 1,
|
|
|
|
id: 'test',
|
|
|
|
class: 'c' + this.count,
|
|
|
|
style: { color: this.count ? 'red' : 'green' },
|
2018-10-10 09:10:30 +08:00
|
|
|
onClick: this.inc
|
2018-10-05 04:52:52 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 07:49:41 +08:00
|
|
|
class Child extends Component<{ [key: string]: any; foo: number }> {
|
2018-10-05 04:52:52 +08:00
|
|
|
updated() {
|
|
|
|
childUpdated()
|
|
|
|
}
|
2018-10-10 09:10:30 +08:00
|
|
|
render() {
|
|
|
|
return h(GrandChild, this.$props)
|
2018-10-05 04:52:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 07:49:41 +08:00
|
|
|
class GrandChild extends Component<{ [key: string]: any; foo: number }> {
|
2018-10-10 08:22:29 +08:00
|
|
|
static props = {
|
|
|
|
foo: Number
|
2018-10-05 04:52:52 +08:00
|
|
|
}
|
|
|
|
updated() {
|
|
|
|
grandChildUpdated()
|
|
|
|
}
|
|
|
|
render(props: any) {
|
2018-10-10 09:10:30 +08:00
|
|
|
return cloneVNode(
|
|
|
|
h(
|
|
|
|
'div',
|
|
|
|
{
|
|
|
|
class: 'c2',
|
|
|
|
style: { fontWeight: 'bold' }
|
|
|
|
},
|
|
|
|
props.foo
|
|
|
|
),
|
|
|
|
this.$attrs
|
2018-10-05 04:52:52 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const root = document.createElement('div')
|
|
|
|
document.body.appendChild(root)
|
2018-11-02 05:08:33 +08:00
|
|
|
await render(h(Hello), root)
|
2018-10-05 04:52:52 +08:00
|
|
|
|
|
|
|
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'))
|
2018-10-10 09:10:30 +08:00
|
|
|
expect(click).toHaveBeenCalled()
|
|
|
|
|
|
|
|
// ...while declared ones remain props
|
|
|
|
expect(node.hasAttribute('foo')).toBe(false)
|
2018-10-05 04:52:52 +08:00
|
|
|
|
|
|
|
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')
|
2018-10-10 09:10:30 +08:00
|
|
|
|
|
|
|
expect(node.hasAttribute('foo')).toBe(false)
|
2018-10-05 04:52:52 +08:00
|
|
|
})
|
2018-09-25 09:13:06 +08:00
|
|
|
})
|