}
describe('store plugins', () => {
- const useStore = defineStore({ id: 'test' })
+ const useStore = defineStore({
+ id: 'test',
+
+ actions: {
+ incrementN() {
+ return this.n++
+ },
+ },
+
+ getters: {
+ doubleN() {
+ return this.n * 2
+ },
+ },
+ })
it('adds properties to stores', () => {
const pinia = createPinia()
expect(store.n).toBe(20)
expect(store.uid).toBeDefined()
+ // @ts-expect-error: n is a number
+ store.n.notExisting
})
it('can install plugins before installing pinia', () => {
expect(store.uid).toBeDefined()
expect(store.hasApp).toBe(true)
})
+
+ it('can be used in actions', () => {
+ const pinia = createPinia()
+
+ // must call use after installing the plugin
+ pinia.use(() => {
+ return { n: 20 }
+ })
+
+ mount({ template: 'none' }, { global: { plugins: [pinia] } })
+
+ const store = useStore(pinia)
+
+ expect(store.incrementN()).toBe(20)
+ })
+
+ it('can be used in getters', () => {
+ const pinia = createPinia()
+
+ // must call use after installing the plugin
+ pinia.use(() => {
+ return { n: 20 }
+ })
+
+ mount({ template: 'none' }, { global: { plugins: [pinia] } })
+
+ const store = useStore(pinia)
+ expect(store.doubleN).toBe(40)
+ })
})
},
})
```
+
+## Comparison with other
createPinia,
Pinia,
PiniaStorePlugin,
- PiniaCustomProperties,
} from './rootStore'
export { defineStore } from './store'
export {
StoreWithGetters,
StoreWithActions,
StoreWithState,
+ PiniaCustomProperties,
} from './types'
// TODO: remove in beta
import { App, InjectionKey, Plugin, Ref, ref, warn } from 'vue'
import { IS_CLIENT } from './env'
-import { StateTree, StoreWithState, StateDescriptor } from './types'
+import {
+ StateTree,
+ StoreWithState,
+ StateDescriptor,
+ PiniaCustomProperties,
+} from './types'
/**
* setActivePinia must be called to handle SSR at the top of functions like
return pinia
}
-
-/**
- * Properties that are added to every store by `pinia.use()`
- */
-export interface PiniaCustomProperties {}
StoreWithActions,
StateDescriptor,
Method,
+ PiniaCustomProperties,
} from './types'
import {
getActivePinia,
getClientApp,
piniaSymbol,
Pinia,
- PiniaCustomProperties,
} from './rootStore'
import { addDevtools } from './devtools'
import { IS_CLIENT } from './env'
>(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
S extends StateTree,
G,
A
-> = StoreWithState<Id, S> & S & StoreWithGetters<G> & StoreWithActions<A>
+> = StoreWithState<Id, S> &
+ S &
+ StoreWithGetters<G> &
+ StoreWithActions<A> &
+ PiniaCustomProperties
/**
* Generic store type
Record<string, Method>,
Record<string, Method>
>
+
+/**
+ * Properties that are added to every store by `pinia.use()`
+ */
+export interface PiniaCustomProperties {}