Skip to content

Commit d32cc67

Browse files
committed
feat: setActiveStore for tests
1 parent 418121c commit d32cc67

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

packages/vue/src/plugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { VueStore } from './store'
21
import { type App, inject, type InjectionKey } from 'vue'
2+
import { getActiveStore, type VueStore } from './store'
33

44
export interface RstoreVueGlobal {
55
store: VueStore
@@ -18,9 +18,9 @@ export function install(vueApp: App, options: PluginOptions) {
1818
}
1919

2020
export function useStore(): RstoreVueGlobal['store'] {
21-
const injected = inject(injectionKey, null)
21+
const injected = inject(injectionKey, null)?.store ?? getActiveStore()
2222
if (!injected) {
23-
throw new Error('Rstore is not installed. Make sure to install the plugin with `app.use(RstorePlugin, { store })`.')
23+
throw new Error('Rstore is not installed. Make sure to install the plugin with `app.use(RstorePlugin, { store })`. For tests, use `setActiveStore(store)`.')
2424
}
25-
return injected.store
25+
return injected
2626
}

packages/vue/src/store.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,19 @@ declare module '@rstore/shared' {
176176
$time: Ref<number>
177177
}
178178
}
179+
180+
let activeStore: VueStore | null = null
181+
182+
/**
183+
* Set the active store for testing modules or code outside of Vue components that need to access the store.
184+
*/
185+
export function setActiveStore(store: VueStore | null) {
186+
activeStore = store
187+
}
188+
189+
/**
190+
* Get the active store. This is useful for testing modules or code outside of Vue components that need to access the store.
191+
*/
192+
export function getActiveStore(): VueStore | null {
193+
return activeStore
194+
}

0 commit comments

Comments
 (0)