-import { defineStore, setActiveReq, setStateProvider } from '../src'
+import {
+ createPinia,
+ defineStore,
+ setActiveReq,
+ setStateProvider,
+} from '../src'
import { mount } from '@vue/test-utils'
describe('Store', () => {
)
})
- it.skip('should outlive components', () => {
+ it('should outlive components', () => {
let store: ReturnType<typeof useStore> | undefined
- const wrapper = mount({
- setup() {
- store = useStore()
+ const wrapper = mount(
+ {
+ setup() {
+ store = useStore()
- return { store }
- },
+ return { store }
+ },
- template: `a: {{ store.a }}`,
- })
+ template: `a: {{ store.a }}`,
+ },
+ {
+ global: {
+ plugins: [createPinia()],
+ },
+ }
+ )
expect(wrapper.html()).toBe('a: true')
--- /dev/null
+export const IS_CLIENT = typeof window !== 'undefined'
piniaSymbol,
} from './rootStore'
import { addDevtools } from './devtools'
-
-const IS_CLIENT = typeof window !== 'undefined'
+import { IS_CLIENT } from './env'
+import { withScope } from './withScope'
function innerPatch<T extends StateTree>(
target: T,
if (!store) {
stores.set(
id,
- // @ts-ignore
- (store = buildStore(id, state, getters, actions, getInitialState(id)))
+ (store = withScope(
+ () =>
+ buildStore(
+ id,
+ state,
+ getters as Record<string, Method> | undefined,
+ actions as Record<string, Method> | undefined,
+ getInitialState(id)
+ ) as Store<Id, S, G, A>
+ ))
)
if (IS_CLIENT && __DEV__ /*|| __FEATURE_PROD_DEVTOOLS__*/) {
--- /dev/null
+import { createApp } from 'vue'
+import { IS_CLIENT } from './env'
+
+export function withScope<T>(factory: () => T): T {
+ if (__BROWSER__ && IS_CLIENT) {
+ let store: T
+ createApp({
+ setup() {
+ store = factory()
+ return () => null
+ },
+ }).mount(document.createElement('div'))
+ // TODO: collect apps to be unmounted when the main app is unmounted
+ return store!
+ } else {
+ // no need to wrap with an app on SSR
+ return factory()
+ }
+}