Skip to content

Commit

Permalink
fix(defineQuery): inject globals
Browse files Browse the repository at this point in the history
Close #145
Close #145
  • Loading branch information
posva committed Jan 3, 2025
1 parent 8ad0615 commit 2307daf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
36 changes: 35 additions & 1 deletion src/define-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
import { createPinia } from 'pinia'
import type { App } from 'vue'
import { createApp, defineComponent, effectScope, ref } from 'vue'
import { createApp, defineComponent, effectScope, inject, provide, ref } from 'vue'
import { defineQuery } from './define-query'
import { useQuery } from './use-query'
import type { UseQueryOptions } from './query-options'
Expand Down Expand Up @@ -82,6 +82,40 @@ describe('defineQuery', () => {
expect(todoFilter).toBe(returnedValues.todoFilter)
})

it('injects from app not from parent component', () => {
const useData = defineQuery(() => {
const value = inject('injected', 'ko')
expect(value).toBe('ok')
return { value }
})
const Injector = defineComponent({
template: '<p>child</p>',
setup() {
return { ...useData() }
},
})
mount(
{
template: '<Injector />',
components: { Injector },
setup() {
provide('injected', 'ko-component')
return {}
},
},
{
global: {
plugins: [createPinia(), PiniaColada],
provide: {
injected: 'ok',
},
},
},
)

expect(useData().value).toBe('ok')
})

describe('refetchOnMount', () => {
it('refreshes the query if mounted in a new component', async () => {
const spy = vi.fn(async () => {
Expand Down
11 changes: 8 additions & 3 deletions src/query-store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineStore, skipHydrate } from 'pinia'
import { defineStore, getActivePinia, skipHydrate } from 'pinia'
import {
type App,
type ComponentInternalInstance,
type EffectScope,
type ShallowRef,
Expand Down Expand Up @@ -249,6 +250,9 @@ export const useQueryCache = /* @__PURE__ */ defineStore(QUERY_STORE_ID, ({ acti
// and plugins won't be able to hook into entry creation and fetching
// this allows use to attach reactive effects to the scope later on
const scope = getCurrentScope()!
const app: App<unknown>
// @ts-expect-error: internal
= getActivePinia()!._a

if (process.env.NODE_ENV !== 'production') {
if (!hasInjectionContext()) {
Expand Down Expand Up @@ -326,8 +330,9 @@ export const useQueryCache = /* @__PURE__ */ defineStore(QUERY_STORE_ID, ({ acti
if (!defineQueryEntry) {
// create the entry first
currentDefineQueryEntry = defineQueryEntry = [[], null]
// then run it s oit can add the queries to the entry
defineQueryEntry[1] = scope.run(fn)
// then run it so it can add the queries to the entry
// we use the app context for injections and the scope for effects
defineQueryEntry[1] = app.runWithContext(() => scope.run(fn)!)
currentDefineQueryEntry = null
defineQueryMap.set(fn, defineQueryEntry)
} else {
Expand Down

0 comments on commit 2307daf

Please sign in to comment.