mapState,
mapStores,
PiniaPlugin,
+ setMapStoreSuffix,
} from '../src'
import { createLocalVue, mount } from '@vue/test-utils'
import VueCompositionAPI, {
expect(wrapper.text()).toBe('2 2 cart')
expect(fromStore).toHaveBeenCalledTimes(2)
})
+
+ it('can set custom suffix', async () => {
+ const pinia = createPinia()
+ setMapStoreSuffix('')
+ const Component = defineComponent({
+ template: `<p @click="main.n++">{{ main.n }}</p>`,
+ computed: {
+ ...mapStores(useStore),
+ },
+ })
+
+ const wrapper = mount(Component, { localVue, pinia })
+ // const store = useStore()
+ // const other = useCartStore()
+ expect(wrapper.vm.main).toBeDefined()
+ expect(wrapper.vm.mainStore).not.toBeDefined()
+ expect(wrapper.text()).toBe('0')
+ await nextTick()
+
+ await wrapper.trigger('click')
+ expect(wrapper.text()).toBe('1')
+ await wrapper.trigger('click')
+ expect(wrapper.text()).toBe('2')
+ })
})
it('mapGetters', () => {
StoreDefinition,
} from './types'
+/**
+ * Interface to allow customizing map helpers. Extend this interface with the
+ * following properties:
+ *
+ * - `suffix`: string. Affects the suffix of `mapStores()`, defaults to `Store`.
+ */
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface MapStoresCustomization {
+ // cannot be added or it wouldn't be able to be extended
// suffix?: string
}
return (cache[id] || (cache[id] = useStore(vm.$pinia))) as Store<Id, S, G, A>
}
+export let mapStoreSuffix = 'Store'
+
+/**
+ * Changes the suffix added by `mapStores()`. Can be set to an empty string.
+ * Defaults to `"Store"`. Make sure to extend the MapStoresCustomization
+ * interface if you need are using TypeScript.
+ *
+ * @param suffix - new suffix
+ */
+export function setMapStoreSuffix(
+ suffix: 'suffix' extends keyof MapStoresCustomization
+ ? MapStoresCustomization['suffix']
+ : string // could be 'Store' but that would be annoying for JS
+): void {
+ mapStoreSuffix = suffix
+}
+
/**
* Allows using stores without the composition API (`setup()`) by generating an
* object to be spread in the `computed` field of a component. It accepts a list
): Spread<Stores> {
return stores.reduce((reduced, useStore) => {
// @ts-ignore: $id is added by defineStore
- reduced[useStore.$id + 'Store'] = function (this: Vue) {
+ reduced[useStore.$id + mapStoreSuffix] = function (this: Vue) {
return getCachedStore(this, useStore)
}
return reduced