test: test for options API

This commit is contained in:
Evan You
2019-09-04 18:16:11 -04:00
parent 7cd33587ba
commit 13298bc4fa
3 changed files with 349 additions and 14 deletions

View File

@@ -28,7 +28,7 @@ import {
onBeforeUnmount,
onUnmounted
} from './apiLifecycle'
import { DebuggerEvent } from '@vue/reactivity'
import { DebuggerEvent, reactive } from '@vue/reactivity'
import { warn } from './warning'
// TODO legacy component definition also supports constructors with .options
@@ -83,7 +83,7 @@ export function applyOptions(
asMixin: boolean = false
) {
const data =
instance.data === EMPTY_OBJ ? (instance.data = {}) : instance.data
instance.data === EMPTY_OBJ ? (instance.data = reactive({})) : instance.data
const ctx = instance.renderProxy as any
const {
// composition
@@ -135,7 +135,13 @@ export function applyOptions(
}
if (computedOptions) {
for (const key in computedOptions) {
data[key] = computed(computedOptions[key] as any)
const opt = computedOptions[key]
data[key] = isFunction(opt)
? computed(opt.bind(ctx))
: computed({
get: opt.get.bind(ctx),
set: opt.set.bind(ctx)
})
}
}
if (methods) {
@@ -148,9 +154,9 @@ export function applyOptions(
const raw = watchOptions[key]
const getter = () => ctx[key]
if (isString(raw)) {
const handler = data[key]
const handler = data[raw]
if (isFunction(handler)) {
watch(getter, handler.bind(ctx))
watch(getter, handler as any)
} else if (__DEV__) {
// TODO warn invalid watch handler path
}

View File

@@ -337,7 +337,7 @@ export function setupStatefulComponent(instance: ComponentInstance) {
// setup returned bindings.
// assuming a render function compiled from template is present.
if (isObject(setupResult)) {
instance.data = setupResult
instance.data = reactive(setupResult)
} else if (__DEV__ && setupResult !== undefined) {
warn(
`setup() should return an object. Received: ${
@@ -360,7 +360,9 @@ export function setupStatefulComponent(instance: ComponentInstance) {
if (__FEATURE_OPTIONS__) {
applyOptions(instance, Component)
}
instance.data = reactive(instance.data === EMPTY_OBJ ? {} : instance.data)
if (instance.data === EMPTY_OBJ) {
instance.data = reactive({})
}
currentInstance = null
}