} from '../src'
import { mount } from '@vue/test-utils'
import { nextTick, defineComponent } from 'vue'
+import { mockWarn } from 'jest-mock-warn'
describe('Map Helpers', () => {
const useCartStore = defineStore({ id: 'cart' })
})
describe('mapStores', () => {
+ mockWarn()
it('mapStores computes only once when mapping one store', async () => {
const pinia = createPinia()
const fromStore = jest.fn(function () {
await wrapper.trigger('click')
expect(wrapper.text()).toBe('2')
})
+
+ it('should warn when an array is passed', () => {
+ mapStores([])
+ expect('pass all stores to "mapStores()"').toHaveBeenWarned()
+ })
})
it('mapGetters', () => {
export function mapStores<Stores extends any[]>(
...stores: [...Stores]
): Spread<Stores> {
+ if (__DEV__ && Array.isArray(stores[0])) {
+ console.warn(
+ `[🍍]: Directly pass all stores to "mapStores()" without putting them in an array:\n` +
+ `Replace\n` +
+ `\tmapStores([useAuthStore, useCartStore])\n` +
+ `with\n` +
+ `\tmapStores(useAuthStore, useCartStore)\n` +
+ `This will fail in production if not fixed.`
+ )
+ stores = stores[0]
+ }
+
return stores.reduce((reduced, useStore) => {
// @ts-ignore: $id is added by defineStore
reduced[useStore.$id + mapStoreSuffix] = function (this: Vue) {
-import { App } from '@vue/runtime-core'
+import { App } from 'vue'
import { expectType, createPinia, defineStore } from '.'
declare module '../dist/pinia' {
const computedStores = mapStores(useStore, useStoreDos, useCounter)
-// @ts-expect-error: no array
-mapStores([useStore, useStoreDos, useCounter])
-
expectType<{
nameStore: () => MainStore
dosStore: () => DosStore
-import { expectType, createPinia, GenericStore } from '.'
+import { App } from 'vue'
+import {
+ expectType,
+ createPinia,
+ GenericStore,
+ Pinia,
+ StateTree,
+ DefineStoreOptions,
+} from '.'
const pinia = createPinia()
-pinia.use(({ store }) => {
+pinia.use(({ store, app, options, pinia }) => {
expectType<GenericStore>(store)
+ expectType<Pinia>(pinia)
+ expectType<App>(app)
+ expectType<
+ DefineStoreOptions<
+ string,
+ StateTree,
+ Record<string, any>,
+ Record<string, any>
+ >
+ >(options)
})