]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(mapHelpers): warn on array mapStores
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 28 Apr 2021 17:21:22 +0000 (19:21 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Wed, 28 Apr 2021 17:45:38 +0000 (19:45 +0200)
__tests__/mapHelpers.spec.ts
src/mapHelpers.ts
test-dts/customizations.test-d.ts
test-dts/mapHelpers.test-d.ts
test-dts/plugins.test-d.ts

index da44249969a88a987e38f655d1ef025f735af4be..cdd1f0742feeda768ecee6fa7b19d620ba5adc81 100644 (file)
@@ -10,6 +10,7 @@ import {
 } 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' })
@@ -39,6 +40,7 @@ describe('Map Helpers', () => {
   })
 
   describe('mapStores', () => {
+    mockWarn()
     it('mapStores computes only once when mapping one store', async () => {
       const pinia = createPinia()
       const fromStore = jest.fn(function () {
@@ -120,6 +122,11 @@ describe('Map Helpers', () => {
       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', () => {
index 1962d6e0c12420d41826582e56c8198a52466518..eaa4d3c0d6324288974cfe15da0ac08bc630b230 100644 (file)
@@ -101,6 +101,18 @@ export function setMapStoreSuffix(
 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) {
index 369cca3a0c86d327b82e3ef311677782c6b8665e..56e180563f682220102397ebf757dafc6abdfefe 100644 (file)
@@ -1,4 +1,4 @@
-import { App } from '@vue/runtime-core'
+import { App } from 'vue'
 import { expectType, createPinia, defineStore } from '.'
 
 declare module '../dist/pinia' {
index 46db81a68db2243db2b3d86976579f49b813cbba..8b77999268e4e68c2f3efba8ef8d14bd2297b3bf 100644 (file)
@@ -42,9 +42,6 @@ type CounterStore = ReturnType<typeof useCounter>
 
 const computedStores = mapStores(useStore, useStoreDos, useCounter)
 
-// @ts-expect-error: no array
-mapStores([useStore, useStoreDos, useCounter])
-
 expectType<{
   nameStore: () => MainStore
   dosStore: () => DosStore
index aab882e88a12a73c31f0dfce3cf0187833c42167..efb6d226b6e3b639d9290660e58f3e0fb4d77ec4 100644 (file)
@@ -1,7 +1,25 @@
-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)
 })