mapGetters,
mapState,
mapStores,
- PiniaPlugin,
setMapStoreSuffix,
} from '../src'
-import { createLocalVue, mount } from '@vue/test-utils'
-import VueCompositionAPI, {
- nextTick,
- defineComponent,
-} from '@vue/composition-api'
+import { mount } from '@vue/test-utils'
+import { nextTick, defineComponent } from 'vue'
describe('Map Helpers', () => {
const useCartStore = defineStore({ id: 'cart' })
},
})
- const localVue = createLocalVue()
- localVue.use(VueCompositionAPI)
- localVue.use(PiniaPlugin)
-
describe('mapStores', () => {
it('mapStores computes only once when mapping one store', async () => {
const pinia = createPinia()
},
})
- const wrapper = mount(Component, { localVue, pinia })
+ const wrapper = mount(Component, { global: { plugins: [pinia] } })
// const store = useStore()
// const other = useCartStore()
expect(wrapper.vm.mainStore).toBeDefined()
},
})
- const wrapper = mount(Component, { localVue, pinia })
+ const wrapper = mount(Component, { global: { plugins: [pinia] } })
expect(wrapper.text()).toBe('0 0 cart')
await nextTick()
- // NOTE: it seems to be the same as the number of stores, probably because
- // we use Vue.set
- expect(fromStore).toHaveBeenCalledTimes(2)
+ expect(fromStore).toHaveBeenCalledTimes(1)
await wrapper.trigger('click')
expect(wrapper.text()).toBe('1 1 cart')
- expect(fromStore).toHaveBeenCalledTimes(2)
+ expect(fromStore).toHaveBeenCalledTimes(1)
await wrapper.trigger('click')
expect(wrapper.text()).toBe('2 2 cart')
- expect(fromStore).toHaveBeenCalledTimes(2)
+ expect(fromStore).toHaveBeenCalledTimes(1)
})
it('can set custom suffix', async () => {
},
})
- const wrapper = mount(Component, { localVue, pinia })
+ const wrapper = mount(Component, { global: { plugins: [pinia] } })
// const store = useStore()
// const other = useCartStore()
+ // @ts-expect-error: by default this shouldn't exist
expect(wrapper.vm.main).toBeDefined()
expect(wrapper.vm.mainStore).not.toBeDefined()
expect(wrapper.text()).toBe('0')
},
})
- const wrapper = mount(Component, { localVue, pinia })
+ const wrapper = mount(Component, { global: { plugins: [pinia] } })
expect(wrapper.text()).toBe(expectedText)
}
},
})
- const wrapper = mount(Component, { localVue, pinia })
+ const wrapper = mount(Component, { global: { plugins: [pinia] } })
expect(vm).toBe(wrapper.vm)
})
},
})
- const wrapper = mount(Component, { localVue, pinia })
+ const wrapper = mount(Component, { global: { plugins: [pinia] } })
expect(wrapper.vm.increment()).toBe(undefined)
expect(wrapper.vm.setN(4)).toBe(4)
},
})
- const wrapper = mount(Component, { localVue, pinia })
+ const wrapper = mount(Component, { global: { plugins: [pinia] } })
expect(wrapper.vm.inc()).toBe(undefined)
expect(wrapper.vm.set(4)).toBe(4)
-import type Vue from 'vue'
+import { ComponentInstance } from '@vue/devtools-api'
import {
GenericStore,
GenericStoreDefinition,
S extends StateTree = StateTree,
G = Record<string, Method>,
A = Record<string, Method>
->(vm: Vue, useStore: StoreDefinition<Id, S, G, A>): Store<Id, S, G, A> {
- const cache = vm._pStores || (vm._pStores = {})
+>(
+ vm: ComponentInstance,
+ useStore: StoreDefinition<Id, S, G, A>
+): Store<Id, S, G, A> {
+ const cache = '_pStores' in vm ? vm._pStores : (vm._pStores = {})
const id = useStore.$id
return (cache[id] || (cache[id] = useStore(vm.$pinia))) as Store<Id, S, G, A>
}
): MapStateReturn<S, G> | MapStateObjectReturn<Id, S, G, A, KeyMapper> {
return Array.isArray(keysOrMapper)
? keysOrMapper.reduce((reduced, key) => {
- reduced[key] = function (this: Vue) {
+ reduced[key] = function (this: ComponentInstance) {
return getCachedStore(this, useStore)[key]
} as () => any
return reduced
}, {} as MapStateReturn<S, G>)
: Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => {
- reduced[key] = function (this: Vue) {
+ reduced[key] = function (this: ComponentInstance) {
const store = getCachedStore(this, useStore)
const storeKey = keysOrMapper[key]
// for some reason TS is unable to infer the type of storeKey to be a
): MapActionsReturn<A> | MapActionsObjectReturn<A, KeyMapper> {
return Array.isArray(keysOrMapper)
? keysOrMapper.reduce((reduced, key) => {
- reduced[key] = function (this: Vue, ...args: any[]) {
+ reduced[key] = function (this: ComponentInstance, ...args: any[]) {
return (getCachedStore(this, useStore)[key] as Method)(...args)
} as Store<string, StateTree, {}, A>[keyof A]
return reduced
}, {} as MapActionsReturn<A>)
: Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => {
- reduced[key] = function (this: Vue, ...args: any[]) {
+ reduced[key] = function (this: ComponentInstance, ...args: any[]) {
return getCachedStore(this, useStore)[keysOrMapper[key]](...args)
} as Store<string, StateTree, {}, A>[keyof KeyMapper[]]
return reduced