test: test for app-level APIs

This commit is contained in:
Evan You
2019-09-03 18:11:04 -04:00
parent 1e4535dc78
commit 98d1406214
12 changed files with 353 additions and 120 deletions

View File

@@ -6,7 +6,7 @@ export interface InjectionKey<T> extends Symbol {}
export function provide<T>(key: InjectionKey<T> | string, value: T) {
if (!currentInstance) {
if (__DEV__) {
warn(`provide() is used without an active component instance.`)
warn(`provide() can only be used inside setup().`)
}
} else {
let provides = currentInstance.provides
@@ -27,17 +27,16 @@ export function provide<T>(key: InjectionKey<T> | string, value: T) {
export function inject<T>(key: InjectionKey<T> | string): T | undefined
export function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T
export function inject(key: InjectionKey<any> | string, defaultValue?: any) {
if (!currentInstance) {
// TODO warn
} else {
// TODO should also check for app-level provides
const provides = currentInstance.parent && currentInstance.provides
if (provides && key in provides) {
if (currentInstance) {
const provides = currentInstance.provides
if (key in provides) {
return provides[key as any] as any
} else if (defaultValue !== undefined) {
return defaultValue
} else if (__DEV__) {
warn(`injection "${key}" not found.`)
}
} else if (__DEV__) {
warn(`inject() can only be used inside setup().`)
}
}