From: Eduardo San Martin Morote Date: Wed, 28 Apr 2021 17:21:22 +0000 (+0200) Subject: feat(mapHelpers): warn on array mapStores X-Git-Tag: v2.0.0-alpha.14~28 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d385bd98b136be15b6aa3ac6c2c8ca9261af4635;p=thirdparty%2Fvuejs%2Fpinia.git feat(mapHelpers): warn on array mapStores --- diff --git a/__tests__/mapHelpers.spec.ts b/__tests__/mapHelpers.spec.ts index da442499..cdd1f074 100644 --- a/__tests__/mapHelpers.spec.ts +++ b/__tests__/mapHelpers.spec.ts @@ -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', () => { diff --git a/src/mapHelpers.ts b/src/mapHelpers.ts index 1962d6e0..eaa4d3c0 100644 --- a/src/mapHelpers.ts +++ b/src/mapHelpers.ts @@ -101,6 +101,18 @@ export function setMapStoreSuffix( export function mapStores( ...stores: [...Stores] ): Spread { + 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) { diff --git a/test-dts/customizations.test-d.ts b/test-dts/customizations.test-d.ts index 369cca3a..56e18056 100644 --- a/test-dts/customizations.test-d.ts +++ b/test-dts/customizations.test-d.ts @@ -1,4 +1,4 @@ -import { App } from '@vue/runtime-core' +import { App } from 'vue' import { expectType, createPinia, defineStore } from '.' declare module '../dist/pinia' { diff --git a/test-dts/mapHelpers.test-d.ts b/test-dts/mapHelpers.test-d.ts index 46db81a6..8b779992 100644 --- a/test-dts/mapHelpers.test-d.ts +++ b/test-dts/mapHelpers.test-d.ts @@ -42,9 +42,6 @@ type CounterStore = ReturnType const computedStores = mapStores(useStore, useStoreDos, useCounter) -// @ts-expect-error: no array -mapStores([useStore, useStoreDos, useCounter]) - expectType<{ nameStore: () => MainStore dosStore: () => DosStore diff --git a/test-dts/plugins.test-d.ts b/test-dts/plugins.test-d.ts index aab882e8..efb6d226 100644 --- a/test-dts/plugins.test-d.ts +++ b/test-dts/plugins.test-d.ts @@ -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(store) + expectType(pinia) + expectType(app) + expectType< + DefineStoreOptions< + string, + StateTree, + Record, + Record + > + >(options) })