2018-11-09 01:54:11 +08:00
|
|
|
import { useState, h, nextTick, useEffect, Component } from '../src'
|
2018-11-02 05:08:33 +08:00
|
|
|
import { renderInstance, serialize, triggerEvent } from '@vue/runtime-test'
|
2018-10-29 05:45:53 +08:00
|
|
|
|
|
|
|
describe('hooks', () => {
|
|
|
|
it('useState', async () => {
|
2018-10-29 07:15:18 +08:00
|
|
|
class Counter extends Component {
|
|
|
|
render() {
|
|
|
|
const [count, setCount] = useState(0)
|
|
|
|
return h(
|
|
|
|
'div',
|
|
|
|
{
|
|
|
|
onClick: () => {
|
|
|
|
setCount(count + 1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
count
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 05:08:33 +08:00
|
|
|
const counter = await renderInstance(Counter)
|
2018-10-29 07:15:18 +08:00
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 05:08:33 +08:00
|
|
|
const counter = await renderInstance(Counter)
|
2018-10-29 07:15:18 +08:00
|
|
|
expect(serialize(counter.$el)).toBe(`<div>0</div>`)
|
|
|
|
|
|
|
|
triggerEvent(counter.$el, 'click')
|
|
|
|
await nextTick()
|
|
|
|
expect(serialize(counter.$el)).toBe(`<div>1</div>`)
|
|
|
|
})
|
|
|
|
|
2018-10-30 06:50:07 +08:00
|
|
|
it('useEffect', async () => {
|
|
|
|
let effect = -1
|
|
|
|
|
2018-11-09 01:54:11 +08:00
|
|
|
class Counter extends Component {
|
|
|
|
render() {
|
|
|
|
const [count, setCount] = useState(0)
|
|
|
|
useEffect(() => {
|
|
|
|
effect = count
|
|
|
|
})
|
|
|
|
return h(
|
|
|
|
'div',
|
|
|
|
{
|
|
|
|
onClick: () => {
|
|
|
|
setCount(count + 1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
count
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 06:50:07 +08:00
|
|
|
|
2018-11-02 05:08:33 +08:00
|
|
|
const counter = await renderInstance(Counter)
|
2018-10-30 06:50:07 +08:00
|
|
|
expect(effect).toBe(0)
|
|
|
|
triggerEvent(counter.$el, 'click')
|
|
|
|
await nextTick()
|
|
|
|
expect(effect).toBe(1)
|
|
|
|
})
|
|
|
|
|
2018-10-29 05:45:53 +08:00
|
|
|
it('useEffect with empty keys', async () => {
|
|
|
|
// TODO
|
|
|
|
})
|
|
|
|
|
|
|
|
it('useEffect with keys', async () => {
|
|
|
|
// TODO
|
|
|
|
})
|
2018-10-30 06:50:07 +08:00
|
|
|
|
|
|
|
it('useData', () => {
|
|
|
|
// TODO
|
|
|
|
})
|
|
|
|
|
|
|
|
it('useMounted/useUnmounted/useUpdated', () => {
|
|
|
|
// TODO
|
|
|
|
})
|
|
|
|
|
|
|
|
it('useWatch', () => {
|
|
|
|
// TODO
|
|
|
|
})
|
|
|
|
|
|
|
|
it('useComputed', () => {
|
|
|
|
// TODO
|
|
|
|
})
|
2018-10-29 05:45:53 +08:00
|
|
|
})
|