]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: mapState with array
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 6 Apr 2021 18:16:41 +0000 (20:16 +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/index.ts
src/mapHelpers.ts

index 0b9ac8340747547d16bed6a66fd82731316a0056..4cd6410c98c8e95dc247e754d49a249692899989 100644 (file)
@@ -1,4 +1,11 @@
-import { createPinia, defineStore, mapStores, PiniaPlugin } from '../src'
+import {
+  createPinia,
+  defineStore,
+  mapGetters,
+  mapState,
+  mapStores,
+  PiniaPlugin,
+} from '../src'
 import { createLocalVue, mount } from '@vue/test-utils'
 import VueCompositionAPI, {
   nextTick,
@@ -97,17 +104,43 @@ describe('Map Helpers', () => {
     })
   })
 
-    // mapStores(useStore).main().$patch({ n: 20 })
+  it('mapGetters', () => {
+    expect(mapGetters).toBe(mapState)
+  })
+
+  describe('mapState', () => {
+    async function testComponent(
+      computedProperties: any,
+      template: string,
+      expectedText: string
+    ) {
+      const pinia = createPinia()
+      const Component = defineComponent({
+        template: `<p>${template}</p>`,
+        computed: {
+          ...computedProperties,
+        },
+      })
+
+      const wrapper = mount(Component, { localVue, pinia })
 
-    const wrapper = mount(Component, { localVue, pinia })
-    expect(wrapper.vm.main).toBeDefined()
-    const store = useStore()
-    expect(wrapper.text()).toBe('0 0')
-    expect(fromStore).toHaveBeenCalledTimes(1)
+      expect(wrapper.text()).toBe(expectedText)
+    }
 
-    store.n++
-    await nextTick()
-    expect(wrapper.text()).toBe('1 1')
-    expect(fromStore).toHaveBeenCalledTimes(1)
+    it('array', async () => {
+      await testComponent(
+        mapState(useStore, ['n', 'a']),
+        `{{ n }} {{ a }}`,
+        `0 true`
+      )
+    })
+
+    it('getters', async () => {
+      await testComponent(
+        mapState(useStore, ['double', 'notA', 'a']),
+        `{{ a }} {{ notA }} {{ double }}`,
+        `true false 0`
+      )
+    })
   })
 })
index 11b72d9603726f164dabeeb9db1f35f9b1cb58d4..fded0df0408b856d88f999274e458f0c23d161ca 100644 (file)
@@ -15,6 +15,6 @@ export {
   DefineStoreOptions,
 } from './types'
 
-export { mapStores } from './mapHelpers'
+export { mapStores, mapState, mapGetters } from './mapHelpers'
 // TODO: remove in beta
 export { createStore } from './deprecated'
index cdca90ea49fba01f4427027c1c60bdaa2900b64a..52ee8bace3b35b7685a902d53941d33c4fddc318 100644 (file)
@@ -1,4 +1,10 @@
-import { GenericStoreDefinition, Store, StoreDefinition } from './types'
+import {
+  GenericStoreDefinition,
+  Method,
+  StateTree,
+  Store,
+  StoreDefinition,
+} from './types'
 
 type StoreObject<S> = S extends StoreDefinition<
   infer Ids,
@@ -46,3 +52,24 @@ export function mapStores<Stores extends unknown[]>(
     return reduced
   }, {} as Spread<Stores>)
 }
+
+type MapStateReturn<S extends StateTree, G> = {
+  [s in keyof S | keyof G]: () => Store<string, S, G, {}>
+}
+
+export function mapState<Id extends string, S extends StateTree, G, A>(
+  useStore: StoreDefinition<Id, S, G, A>,
+  keys: Array<keyof S | keyof G>
+): MapStateReturn<S, G> {
+  return keys.reduce((reduced, key) => {
+    reduced[key] = function () {
+      return useStore((this as any).$pinia)[key]
+    }
+    return reduced
+  }, {} as MapStateReturn<S, G>)
+}
+
+/**
+ * Alias for `mapState()`. You should use `mapState()` instead.
+ */
+export const mapGetters = mapState