test: basic tests for hooks

This commit is contained in:
Evan You
2018-10-28 17:45:53 -04:00
parent 665cd8e3d9
commit a6a571f973
2 changed files with 69 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
import { ComponentInstance, FunctionalComponent } from '../component'
import { ComponentInstance, FunctionalComponent, Component } from '../component'
import { mergeLifecycleHooks, Data } from '../componentOptions'
import { VNode, Slots } from '../vdom'
import { observable } from '@vue/observer'
@@ -36,7 +36,7 @@ export function unsetCurrentInstance() {
currentInstance = null
}
export function useState(initial: any) {
export function useState<T>(initial: T): [T, (newValue: T) => void] {
if (!currentInstance) {
throw new Error(
`useState must be called in a function passed to withHooks.`
@@ -107,20 +107,20 @@ function injectEffect(
: effect
}
export function withHooks<T extends FunctionalComponent>(render: T): T {
return {
displayName: render.name,
export function withHooks(render: FunctionalComponent): new () => Component {
return class ComponentWithHooks extends Component {
static displayName = render.name
created() {
hooksState.set(this._self, {
hooksState.set((this as any)._self, {
state: observable({}),
effects: []
})
},
}
render(props: Data, slots: Slots, attrs: Data, parentVNode: VNode) {
setCurrentInstance(this._self)
setCurrentInstance((this as any)._self)
const ret = render(props, slots, attrs, parentVNode)
unsetCurrentInstance()
return ret
}
} as any
}
}