]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(types): pass custom properties to actions and getters
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 31 Mar 2021 10:05:12 +0000 (12:05 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 31 Mar 2021 10:05:12 +0000 (12:05 +0200)
__tests__/storePlugins.spec.ts
src/store.ts

index 62bb11ee4fbd75d4b883e7942daccbe20ed5bc5d..6d6f31f5214e80e203d679fe57f0f96d186f79c9 100644 (file)
@@ -11,7 +11,21 @@ declare module '../src' {
 }
 
 describe('store plugins', () => {
-  const useStore = defineStore({ id: 'test' })
+  const useStore = defineStore({
+    id: 'test',
+
+    actions: {
+      incrementN() {
+        return this.n++
+      },
+    },
+
+    getters: {
+      doubleN() {
+        return this.n * 2
+      },
+    },
+  })
   const localVue = createLocalVue()
   localVue.use(PiniaPlugin)
 
@@ -48,4 +62,33 @@ describe('store plugins', () => {
     expect(store.n).toBe(1)
     expect(store.hasPinia).toBe(true)
   })
+
+  it('can be used in actions', () => {
+    const pinia = createPinia()
+    pinia.Vue = Vue
+    mount({ template: '<p/>' }, { localVue })
+
+    // must call use after installing the plugin
+    pinia.use(() => {
+      return { n: 20 }
+    })
+
+    const store = useStore(pinia)
+
+    expect(store.incrementN()).toBe(20)
+  })
+
+  it('can be used in getters', () => {
+    const pinia = createPinia()
+    pinia.Vue = Vue
+    mount({ template: '<p/>' }, { localVue })
+
+    // must call use after installing the plugin
+    pinia.use(() => {
+      return { n: 20 }
+    })
+
+    const store = useStore(pinia)
+    expect(store.doubleN).toBe(40)
+  })
 })
index 61ceedf9cf18f1e0c9e211d01a1426f9dabd7a5a..61c4aeefcc6b82a62a8a527313c4e1861a0e2cc1 100644 (file)
@@ -18,6 +18,7 @@ import {
   StoreWithActions,
   Method,
   StateDescriptor,
+  PiniaCustomProperties,
 } from './types'
 import { useStoreDevtools } from './devtools'
 import {
@@ -25,7 +26,6 @@ import {
   Pinia,
   setActivePinia,
   getActivePinia,
-  PiniaCustomProperties,
   piniaSymbol,
 } from './rootStore'
 import { assign } from './utils'
@@ -253,9 +253,16 @@ export function defineStore<
 >(options: {
   id: Id
   state?: () => S
-  getters?: G & ThisType<S & StoreWithGetters<G>>
+  getters?: G & ThisType<S & StoreWithGetters<G> & PiniaCustomProperties>
   // allow actions use other actions
-  actions?: A & ThisType<A & S & StoreWithState<Id, S> & StoreWithGetters<G>>
+  actions?: A &
+    ThisType<
+      A &
+        S &
+        StoreWithState<Id, S> &
+        StoreWithGetters<G> &
+        PiniaCustomProperties
+    >
 }) {
   const { id, state, getters, actions } = options