]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test(types): test options
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 15 Apr 2021 15:19:56 +0000 (17:19 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Wed, 28 Apr 2021 17:45:38 +0000 (19:45 +0200)
src/store.ts
test-dts/customizations.test-d.ts

index 1f8a18457d623df90efe6d3f7fcddf58725a0ede..fc3dada9897badec238f59734929a910f4afb955 100644 (file)
@@ -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
index ed5968a38d574d0d2e2f6862fa520f6ce1c8adcb..369cca3a0c86d327b82e3ef311677782c6b8665e 100644 (file)
@@ -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<Id, S, G, A> {
+    $actions: Array<keyof A>
+  }
+
+  export interface DefineStoreOptions<Id, S, G, A> {
+    debounce?: {
+      // Record<keyof A, number>
+      [k in keyof A]?: number
+    }
   }
 }
 
-const useCounter = defineStore({
-  id: 'counter',
-  state: () => ({ n: 0 }),
+const pinia = createPinia()
+
+pinia.use((context) => {
+  expectType<string>(context.options.id)
+  expectType<string>(context.store.$id)
+  expectType<App>(context.app)
+
+  return {
+    $actions: Object.keys(context.options.actions || {}),
+  }
 })
 
-type CounterStore = ReturnType<typeof useCounter>
+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<F extends Procedure>(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<string, (...args: any[]) => any>)
+  }
+})