}
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)
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)
+ })
})
StoreWithActions,
Method,
StateDescriptor,
+ PiniaCustomProperties,
} from './types'
import { useStoreDevtools } from './devtools'
import {
Pinia,
setActivePinia,
getActivePinia,
- PiniaCustomProperties,
piniaSymbol,
} from './rootStore'
import { assign } from './utils'
>(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