feat: make hooks usable inside classes

This commit is contained in:
Evan You
2018-10-28 19:15:18 -04:00
parent 98782b326a
commit 894bead914
6 changed files with 112 additions and 31 deletions

View File

@@ -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
})