From f5b7ce0a03c7f9b51e3669e80be07a62a0fc3416 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 15 Apr 2021 17:19:56 +0200 Subject: [PATCH] test(types): test options --- src/store.ts | 2 +- test-dts/customizations.test-d.ts | 67 +++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/store.ts b/src/store.ts index 1f8a1845..fc3dada9 100644 --- a/src/store.ts +++ b/src/store.ts @@ -248,7 +248,7 @@ function buildStoreToUse< // apply all plugins pinia._p.forEach((extender) => { - Object.assign(store, extender({ store, app: pinia._a, pinia, options })) + assign(store, extender({ store, app: pinia._a, pinia, options })) }) return store diff --git a/test-dts/customizations.test-d.ts b/test-dts/customizations.test-d.ts index ed5968a3..369cca3a 100644 --- a/test-dts/customizations.test-d.ts +++ b/test-dts/customizations.test-d.ts @@ -1,21 +1,64 @@ -import { defineStore, expectType, mapStores } from '.' +import { App } from '@vue/runtime-core' +import { expectType, createPinia, defineStore } from '.' declare module '../dist/pinia' { - export interface MapStoresCustomization { - // this is the only one that can be applied to work with other tests - suffix: 'Store' + export interface PiniaCustomProperties { + $actions: Array + } + + export interface DefineStoreOptions { + debounce?: { + // Record + [k in keyof A]?: number + } } } -const useCounter = defineStore({ - id: 'counter', - state: () => ({ n: 0 }), +const pinia = createPinia() + +pinia.use((context) => { + expectType(context.options.id) + expectType(context.store.$id) + expectType(context.app) + + return { + $actions: Object.keys(context.options.actions || {}), + } }) -type CounterStore = ReturnType +defineStore({ + id: 'a', + actions: { + one() {}, + two() { + this.one() + }, + three() { + this.two() + }, + }, -const computedStores = mapStores(useCounter) + debounce: { + one: 200, + two: 300, + // three: 100 + }, +}) -expectType<{ - counterStore: () => CounterStore -}>(computedStores) +type Procedure = (...args: any[]) => any + +function debounce(fn: F, time: number = 200) { + return fn +} + +pinia.use(({ options, store }) => { + if (options.debounce) { + return Object.keys(options.debounce).reduce((debouncedActions, action) => { + debouncedActions[action] = debounce( + store[action], + options.debounce![action as keyof typeof options['actions']] + ) + return debouncedActions + }, {} as Record any>) + } +}) -- 2.39.5