feat: make hooks usable inside classes
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { withHooks, useState, h, nextTick, useEffect } from '../src'
|
||||
import { withHooks, useState, h, nextTick, useEffect, Component } from '../src'
|
||||
import { renderIntsance, serialize, triggerEvent } from '@vue/runtime-test'
|
||||
|
||||
describe('hooks', () => {
|
||||
@@ -50,6 +50,61 @@ describe('hooks', () => {
|
||||
expect(effect).toBe(1)
|
||||
})
|
||||
|
||||
it('should be usable inside class', async () => {
|
||||
class Counter extends Component {
|
||||
render() {
|
||||
const [count, setCount] = useState(0)
|
||||
return h(
|
||||
'div',
|
||||
{
|
||||
onClick: () => {
|
||||
setCount(count + 1)
|
||||
}
|
||||
},
|
||||
count
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const counter = renderIntsance(Counter)
|
||||
expect(serialize(counter.$el)).toBe(`<div>0</div>`)
|
||||
|
||||
triggerEvent(counter.$el, 'click')
|
||||
await nextTick()
|
||||
expect(serialize(counter.$el)).toBe(`<div>1</div>`)
|
||||
})
|
||||
|
||||
it('should be usable via hooks() method', async () => {
|
||||
class Counter extends Component {
|
||||
hooks() {
|
||||
const [count, setCount] = useState(0)
|
||||
return {
|
||||
count,
|
||||
setCount
|
||||
}
|
||||
}
|
||||
render() {
|
||||
const { count, setCount } = this as any
|
||||
return h(
|
||||
'div',
|
||||
{
|
||||
onClick: () => {
|
||||
setCount(count + 1)
|
||||
}
|
||||
},
|
||||
count
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const counter = renderIntsance(Counter)
|
||||
expect(serialize(counter.$el)).toBe(`<div>0</div>`)
|
||||
|
||||
triggerEvent(counter.$el, 'click')
|
||||
await nextTick()
|
||||
expect(serialize(counter.$el)).toBe(`<div>1</div>`)
|
||||
})
|
||||
|
||||
it('useEffect with empty keys', async () => {
|
||||
// TODO
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user