Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Fix Provide/ProvideReactive compatibility (#249) #264

Merged
merged 2 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/vue-property-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function Provide(key?: string | symbol) {
return createDecorator((componentOptions, k) => {
let provide: any = componentOptions.provide
if (typeof provide !== 'function' || !provide.managed) {
const original = componentOptions.provide
const original: any = componentOptions.provide
provide = componentOptions.provide = function(this: any) {
let rv = Object.create(
(typeof original === 'function' ? original.call(this) : original) ||
Expand All @@ -87,26 +87,26 @@ export function Provide(key?: string | symbol) {
export function ProvideReactive(key?: string | symbol) {
return createDecorator((componentOptions, k) => {
let provide: any = componentOptions.provide
if (typeof provide !== 'function' || !provide.managed) {
const original = componentOptions.provide
if (typeof provide !== 'function' || !provide.managedReactive) {
const original: any = componentOptions.provide
provide = componentOptions.provide = function(this: any) {
let rv = Object.create(
(typeof original === 'function' ? original.call(this) : original) ||
null,
)
rv[reactiveInjectKey] = {}
for (let i in provide.managed) {
rv[provide.managed[i]] = this[i] // Duplicates the behavior of `@Provide`
Object.defineProperty(rv[reactiveInjectKey], provide.managed[i], {
for (let i in provide.managedReactive) {
rv[provide.managedReactive[i]] = this[i] // Duplicates the behavior of `@Provide`
Object.defineProperty(rv[reactiveInjectKey], provide.managedReactive[i], {
enumerable: true,
get: () => this[i],
})
}
return rv
}
provide.managed = {}
provide.managedReactive = {}
}
provide.managed[k] = key || k
provide.managedReactive[k] = key || k
})
}

Expand Down
44 changes: 43 additions & 1 deletion tests/ProvideReactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Vue from 'vue'
import {
Component,
ProvideReactive,
InjectReactive
InjectReactive,
Provide,
Inject
} from '../src/vue-property-decorator'

describe(ProvideReactive, () => {
Expand Down Expand Up @@ -39,6 +41,46 @@ describe(ProvideReactive, () => {
})
})


describe('is compatible with @Provide()', () => {
@Component
class ParentComponent extends Vue {
@Provide() first = 'whatever'
@ProvideReactive() one = 'one'
}
@Component
class ChildComponent extends Vue {
@InjectReactive() one!: string
}

const parent = new ParentComponent()
const component = new ChildComponent({ parent })

test('provides value', () => {
expect(component.one).toBe('one')
})
})


describe('can @Inject() dependency provided using @ProvideReactive()', () => {
@Component
class ParentComponent extends Vue {
@ProvideReactive() one = 'one'
}
@Component
class ChildComponent extends Vue {
@Inject() one!: string
}

const parent = new ParentComponent()
const component = new ChildComponent({ parent })

test('provides value', () => {
expect(component.one).toBe('one')
})
})


describe('when key is given', () => {
const key = 'KEY'
const value = 'VALUE'
Expand Down