import { createPinia, defineStore } from '../src'
import { mount } from '@vue/test-utils'
-import { App, computed, ref, toRef } from 'vue'
+import { App, computed, ref, toRef, watch } from 'vue'
declare module '../src' {
export interface PiniaCustomProperties<Id> {
globalA: string
globalB: string
shared: number
+ double: number
}
export interface PiniaCustomStateProperties<S> {
useStore()
})
+
+ it('run inside store effect', async () => {
+ const pinia = createPinia()
+
+ // must call use after installing the plugin
+ pinia.use(({ store }) => ({
+ // @ts-expect-error: invalid computed
+ double: computed(() => store.$state.n * 2),
+ }))
+
+ const useStore = defineStore('main', {
+ state: () => ({ n: 1 }),
+ })
+
+ mount(
+ {
+ template: 'none',
+ setup() {
+ // create it inside of the component
+ useStore()
+ },
+ },
+ { global: { plugins: [pinia] } }
+ ).unmount()
+
+ const store = useStore(pinia)
+
+ const spy = jest.fn()
+ watch(() => store.double, spy, { flush: 'sync' })
+
+ expect(spy).toHaveBeenCalledTimes(0)
+
+ store.n++
+ expect(spy).toHaveBeenCalledTimes(1)
+ })
})
// apply all plugins
pinia._p.forEach((extender) => {
if (__DEV__ && IS_CLIENT) {
- const extensions = extender({
- store,
- app: pinia._a,
- pinia,
- // @ts-expect-error
- options: optionsForPlugin,
- })
- Object.keys(extensions || {}).forEach((key) =>
- store._customProperties.add(key)
- )
- assign(store, extensions)
- } else {
- assign(
- store,
+ const extensions = scope.run(() =>
extender({
store,
app: pinia._a,
// @ts-expect-error
options: optionsForPlugin,
})
+ )!
+ Object.keys(extensions || {}).forEach((key) =>
+ store._customProperties.add(key)
+ )
+ assign(store, extensions)
+ } else {
+ assign(
+ store,
+ scope.run(() =>
+ extender({
+ store,
+ app: pinia._a,
+ pinia,
+ // @ts-expect-error
+ options: optionsForPlugin,
+ })
+ )!
)
}
})