-import { createPinia, defineStore, setActivePinia } from '../src'
+import {
+ createPinia,
+ defineStore,
+ getActivePinia,
+ setActivePinia,
+} from '../src'
import { mount } from '@vue/test-utils'
-import { watch, nextTick, defineComponent, ref, Ref, onMounted } from 'vue'
+import {
+ watch,
+ nextTick,
+ defineComponent,
+ ref,
+ Ref,
+ onMounted,
+ getCurrentInstance,
+} from 'vue'
describe('Store Lifespan', () => {
function defineMyStore() {
const pinia = createPinia()
+ it('gets the active pinia outside of setup', () => {
+ setActivePinia(pinia)
+ expect(getCurrentInstance()).toBeFalsy()
+ expect(getActivePinia()).toBe(pinia)
+ })
+
+ it('gets the active pinia inside of setup', () => {
+ expect.assertions(3)
+ const pinia = createPinia()
+ setActivePinia(undefined)
+ expect(getActivePinia()).toBe(undefined)
+
+ mount(
+ {
+ template: 'no',
+ setup() {
+ expect(getActivePinia()).toBe(pinia)
+ },
+ },
+ { global: { plugins: [pinia] } }
+ )
+ // and outside too
+ expect(getActivePinia()).toBe(pinia)
+ })
+
it('state reactivity outlives component life', async () => {
const useStore = defineMyStore()
const hotState = ref({} as S)
+ /* istanbul ignore if */
if (__DEV__ && !pinia._e.active) {
throw new Error('Pinia destroyed')
}
)
}
+ /* istanbul ignore next */
const $reset = __DEV__
? () => {
throw new Error(
Object.defineProperty(store, '$state', {
get: () => (__DEV__ && hot ? hotState.value : pinia.state.value[$id]),
set: (state) => {
+ /* istanbul ignore if */
if (__DEV__ && hot) {
throw new Error('cannot set hotState')
}
})
// add the hotUpdate before plugins to allow them to override it
+ /* istanbul ignore else */
if (__DEV__) {
store._hotUpdate = markRaw((newStore) => {
store._hotUpdating = true
// apply all plugins
pinia._p.forEach((extender) => {
+ /* istanbul ignore else */
if (__DEV__ && IS_CLIENT) {
const extensions = scope.run(() =>
extender({
* {@link Pinia} instance with test specific properties.
*/
export interface TestingPinia extends Pinia {
- /**
- * Clears the cache of spies used for actions.
- */
- resetSpyCache(): void
-
/** App used by Pinia */
app: App
}
stubActions = true,
stubPatch = false,
fakeApp = false,
- createSpy,
+ createSpy: _createSpy,
}: TestingOptions = {}): TestingPinia {
const pinia = createPinia()
plugins.forEach((plugin) => pinia.use(plugin))
- // @ts-ignore: this can fail in TS depending of the existence of jest
- createSpy = createSpy || (typeof jest !== undefined && jest.fn)
+ const createSpy = _createSpy || (typeof jest !== undefined && jest.fn)
+ /* istanbul ignore if */
if (!createSpy) {
throw new Error('You must configure the `createSpy` option.')
}
- // Cache of all actions to share them across all stores
- const spiedActions = new Map<string, Record<string, any>>()
-
pinia.use(({ store, options }) => {
- if (!spiedActions.has(store.$id)) {
- spiedActions.set(store.$id, {})
- }
- const actionsCache = spiedActions.get(store.$id)!
-
Object.keys(options.actions || {}).forEach((action) => {
- actionsCache[action] =
- actionsCache[action] ||
- (stubActions ? createSpy!() : createSpy!(store[action]))
- store[action] = actionsCache[action]
+ store[action] = stubActions ? createSpy() : createSpy(store[action])
})
- store.$patch = stubPatch ? createSpy!() : createSpy!(store.$patch)
+ store.$patch = stubPatch ? createSpy() : createSpy(store.$patch)
})
if (fakeApp) {
return Object.assign(
{
- resetSpyCache() {
- spiedActions.clear()
- },
- get app() {
+ get app(): App {
return (this as TestingPinia)._a
},
},