]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor: adapt v1 changes
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 8 Apr 2021 13:31:41 +0000 (15:31 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Fri, 9 Apr 2021 11:08:56 +0000 (13:08 +0200)
__tests__/mapHelpers.spec.ts
src/mapHelpers.ts

index e39c8508a008051bf096f08cde490d5b03a8e114..1215b591359fbbdde98f39db3da40b25c2679b78 100644 (file)
@@ -5,14 +5,10 @@ import {
   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' })
@@ -41,10 +37,6 @@ describe('Map Helpers', () => {
     },
   })
 
-  const localVue = createLocalVue()
-  localVue.use(VueCompositionAPI)
-  localVue.use(PiniaPlugin)
-
   describe('mapStores', () => {
     it('mapStores computes only once when mapping one store', async () => {
       const pinia = createPinia()
@@ -60,7 +52,7 @@ describe('Map Helpers', () => {
         },
       })
 
-      const wrapper = mount(Component, { localVue, pinia })
+      const wrapper = mount(Component, { global: { plugins: [pinia] } })
       // const store = useStore()
       // const other = useCartStore()
       expect(wrapper.vm.mainStore).toBeDefined()
@@ -90,19 +82,17 @@ describe('Map Helpers', () => {
         },
       })
 
-      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 () => {
@@ -115,9 +105,10 @@ describe('Map Helpers', () => {
         },
       })
 
-      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')
@@ -148,7 +139,7 @@ describe('Map Helpers', () => {
         },
       })
 
-      const wrapper = mount(Component, { localVue, pinia })
+      const wrapper = mount(Component, { global: { plugins: [pinia] } })
 
       expect(wrapper.text()).toBe(expectedText)
     }
@@ -192,7 +183,7 @@ describe('Map Helpers', () => {
         },
       })
 
-      const wrapper = mount(Component, { localVue, pinia })
+      const wrapper = mount(Component, { global: { plugins: [pinia] } })
       expect(vm).toBe(wrapper.vm)
     })
 
@@ -228,7 +219,7 @@ describe('Map Helpers', () => {
         },
       })
 
-      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)
@@ -243,7 +234,7 @@ describe('Map Helpers', () => {
         },
       })
 
-      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)
index 42158fad662f33b7fe1aefb3991aea6e99363c28..e9b6de65772dc089e6fe649336f3055975094622 100644 (file)
@@ -1,4 +1,4 @@
-import type Vue from 'vue'
+import { ComponentInstance } from '@vue/devtools-api'
 import {
   GenericStore,
   GenericStoreDefinition,
@@ -51,8 +51,11 @@ function getCachedStore<
   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>
 }
@@ -228,13 +231,13 @@ export function mapState<
 ): 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
@@ -342,13 +345,13 @@ export function mapActions<
 ): 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