diff --git a/packages/pinia/test-dts/mapHelpers.test-d.ts b/packages/pinia/test-dts/mapHelpers.test-d.ts index 3d2770a05e..3e7f179b47 100644 --- a/packages/pinia/test-dts/mapHelpers.test-d.ts +++ b/packages/pinia/test-dts/mapHelpers.test-d.ts @@ -1,169 +1,167 @@ import { computed, ref } from 'vue' import { defineStore, - expectType, mapStores, mapActions, mapState, mapWritableState, TypeEqual, } from './' - -const useOptionsStore = defineStore({ - id: 'name', - state: () => ({ a: 'on' as 'on' | 'off', nested: { counter: 0 } }), - getters: { - upper: (state) => state.a.toUpperCase(), - }, - actions: { - toggleA() { - this.a = this.a === 'off' ? 'on' : 'off' +import { describe, it, expectTypeOf } from 'vitest' + +describe('mapHelpers', () => { + const useOptionsStore = defineStore({ + id: 'name', + state: () => ({ a: 'on' as 'on' | 'off', nested: { counter: 0 } }), + getters: { + upper: (state) => state.a.toUpperCase(), }, - - setToggle(a: 'on' | 'off') { - return (this.a = a) + actions: { + toggleA() { + this.a = this.a === 'off' ? 'on' : 'off' + }, + + setToggle(a: 'on' | 'off') { + return (this.a = a) + }, }, - }, -}) + }) -const useSetupStore = defineStore('setupStore', () => { - const a = ref('on' as 'on' | 'off') - const upper = computed(() => a.value.toUpperCase()) - function toggleA() { - a.value = a.value === 'off' ? 'on' : 'off' - } - function setToggle(aVal: 'on' | 'off') { - return (a.value = aVal) - } - return { a, upper, toggleA, setToggle } -}) + const useSetupStore = defineStore('setupStore', () => { + const a = ref('on' as 'on' | 'off') + const upper = computed(() => a.value.toUpperCase()) + function toggleA() { + a.value = a.value === 'off' ? 'on' : 'off' + } + function setToggle(aVal: 'on' | 'off') { + return (a.value = aVal) + } + return { a, upper, toggleA, setToggle } + }) -const useCounter = defineStore({ - id: 'counter', - state: () => ({ n: 0 }), -}) + const useCounter = defineStore({ + id: 'counter', + state: () => ({ n: 0 }), + }) -const useStoreDos = defineStore({ - id: 'dos', - state: () => ({}), -}) + const useStoreDos = defineStore({ + id: 'dos', + state: () => ({}), + }) -type MainStore = ReturnType -type DosStore = ReturnType -type CounterStore = ReturnType - -const computedStores = mapStores(useOptionsStore, useStoreDos, useCounter) - -expectType<{ - nameStore: () => MainStore - dosStore: () => DosStore - counterStore: () => CounterStore -}>(computedStores) - -// store with no getters -expectType<{ - n: () => number -}>(mapState(useCounter, ['n'])) - -expectType<{ - a: () => 'on' | 'off' - upper: () => string -}>(mapState(useOptionsStore, ['a', 'upper'])) - -// @ts-expect-error -mapState(useOptionsStore, ['a']).nested - -// @ts-expect-error -mapState(useOptionsStore, ['a', 'upper']).nested - -expectType<{ - newA: () => 'on' | 'off' - newUpper: () => string -}>(mapState(useOptionsStore, { newA: 'a', newUpper: 'upper' })) - -expectType<{ - newA: () => 'on' | 'off' - newUpper: () => string -}>( - mapState(useOptionsStore, { - newA: (store) => { - expectType(store.upper) - return store.a - }, - newUpper: 'upper', + type MainStore = ReturnType + type DosStore = ReturnType + type CounterStore = ReturnType + + describe('mapStores', () => { + it('should map stores correctly', () => { + const computedStores = mapStores(useOptionsStore, useStoreDos, useCounter) + expectTypeOf<{ + nameStore: () => MainStore + dosStore: () => DosStore + counterStore: () => CounterStore + }>(computedStores) + }) }) -) - -expectType<{ - setToggle: (a: 'on' | 'off') => 'on' | 'off' - toggleA: () => void -}>(mapActions(useOptionsStore, ['setToggle', 'toggleA'])) - -expectType<{ - newSetToggle: (a: 'on' | 'off') => 'on' | 'off' - newToggleA: () => void -}>( - mapActions(useOptionsStore, { - newSetToggle: 'setToggle', - newToggleA: 'toggleA', + + describe('mapState', () => { + it('should map state correctly for store with no getters', () => { + expectTypeOf<{ + n: () => number + }>(mapState(useCounter, ['n'])) + }) + + it('should map state correctly for store with getters', () => { + expectTypeOf<{ + a: () => 'on' | 'off' + upper: () => string + }>(mapState(useOptionsStore, ['a', 'upper'])) + }) + + it('should map state with new keys', () => { + expectTypeOf<{ + newA: () => 'on' | 'off' + newUpper: () => string + }>(mapState(useOptionsStore, { newA: 'a', newUpper: 'upper' })) + }) + + it('should map state with function keys', () => { + expectTypeOf<{ + newA: () => 'on' | 'off' + newUpper: () => string + }>( + mapState(useOptionsStore, { + newA: (store) => { + expectTypeOf(store.upper) + return store.a + }, + newUpper: 'upper', + }) + ) + }) + + it('should map state for setup store', () => { + const setupStoreWithState = mapState(useSetupStore, ['a']) + expectTypeOf(setupStoreWithState).toEqualTypeOf<{ + a: () => 'on' | 'off' + }>() + + const setupStoreWithGetters = mapState(useSetupStore, ['a', 'upper']) + expectTypeOf(setupStoreWithGetters).toEqualTypeOf<{ + a: () => 'on' | 'off' + upper: () => string + }>() + }) }) -) - -expectType<{ - a: { - get: () => 'on' | 'off' - set: (v: 'on' | 'off') => any - } -}>(mapWritableState(useOptionsStore, ['a'])) -// @ts-expect-error: only defined in array -mapWritableState(useOptionsStore, ['a']).b - -expectType<{ - newA: { - get: () => 'on' | 'off' - set: (v: 'on' | 'off') => any - } -}>(mapWritableState(useOptionsStore, { newA: 'a' })) - -// @ts-expect-error: cannot use a getter -mapWritableState(useOptionsStore, ['upper']) -// @ts-expect-error: cannot use a getter -mapWritableState(useOptionsStore, { up: 'upper' }) - -expectType<{ - foo: { - get: () => 'on' | 'off' - set: (v: 'on' | 'off') => any - } -}>(mapWritableState(useSetupStore, { foo: 'a' })) - -expectType<{ - a: { - get: () => 'on' | 'off' - set: (v: 'on' | 'off') => any - } -}>(mapWritableState(useSetupStore, ['a'])) - -const setupStoreWithState = mapState(useSetupStore, ['a']) - -// store with no getters -expectType< - TypeEqual< - { - a: () => 'on' | 'off' - }, - typeof setupStoreWithState - > ->(true) -const setupStoreWithGetters = mapState(useSetupStore, ['a', 'upper']) + describe('mapActions', () => { + it('should map actions correctly', () => { + expectTypeOf<{ + setToggle: (a: 'on' | 'off') => 'on' | 'off' + toggleA: () => void + }>(mapActions(useOptionsStore, ['setToggle', 'toggleA'])) + + expectTypeOf<{ + newSetToggle: (a: 'on' | 'off') => 'on' | 'off' + newToggleA: () => void + }>( + mapActions(useOptionsStore, { + newSetToggle: 'setToggle', + newToggleA: 'toggleA', + }) + ) + }) + }) -expectType< - TypeEqual< - { - a: () => 'on' | 'off' - upper: () => string - }, - typeof setupStoreWithGetters - > ->(true) + describe('mapWritableState', () => { + it('should map writable state correctly', () => { + expectTypeOf<{ + a: { + get: () => 'on' | 'off' + set: (v: 'on' | 'off') => any + } + }>(mapWritableState(useOptionsStore, ['a'])) + + expectTypeOf<{ + newA: { + get: () => 'on' | 'off' + set: (v: 'on' | 'off') => any + } + }>(mapWritableState(useOptionsStore, { newA: 'a' })) + + expectTypeOf<{ + foo: { + get: () => 'on' | 'off' + set: (v: 'on' | 'off') => any + } + }>(mapWritableState(useSetupStore, { foo: 'a' })) + + expectTypeOf<{ + a: { + get: () => 'on' | 'off' + set: (v: 'on' | 'off') => any + } + }>(mapWritableState(useSetupStore, ['a'])) + }) + }) +})