]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: correct customization types
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 24 Jun 2021 14:13:14 +0000 (16:13 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 24 Jun 2021 14:17:17 +0000 (16:17 +0200)
__tests__/storePlugins.spec.ts
test-dts/customizations.test-d.ts

index b74050e24b5246fefcb9bc9f8189cdae42b6cc1f..93c9e75cd86fbdec54ec5b83991e999b5ea43222 100644 (file)
@@ -9,12 +9,17 @@ declare module '../src' {
     hasPinia: boolean
     idFromPlugin: Id
     someRef: number
+    globalA: string
+    globalB: string
+    notShared: number
+    shared: number
   }
 
   export interface PiniaCustomStateProperties<S> {
     // pluginN: 'test' extends Id ? number : never | undefined
     pluginN: number
     someRef: number
+    shared: number
   }
 }
 
@@ -41,9 +46,7 @@ describe('store plugins', () => {
 
     // must call use after installing the plugin
     pinia.use(({ store }) => {
-      if (!('pluginN' in store.$state)) {
-        // the check above makes this impossible
-        // but in practice we need this until effectScope is released
+      if (!store.$state.hasOwnProperty('pluginN')) {
         set(store.$state, 'pluginN', ref(20))
       }
       set(store, 'pluginN', toRef(store.$state, 'pluginN'))
@@ -51,6 +54,7 @@ describe('store plugins', () => {
 
     const store = useStore()
 
+    expect(store.$state.pluginN).toBe(20)
     expect(store.pluginN).toBe(20)
     // @ts-expect-error: n is a number
     store.pluginN.notExisting
@@ -123,4 +127,45 @@ describe('store plugins', () => {
     expect(store.$state.someRef).toBe(2)
     expect(someRef.value).toBe(2)
   })
+
+  it('shares the same ref among stores', () => {
+    const pinia = createPinia()
+
+    mount({ template: '<p/>' }, { localVue, pinia })
+
+    // must call use after installing the plugin
+    pinia.use(({ store }) => {
+      if (!store.$state.hasOwnProperty('shared')) {
+        set(store.$state, 'shared', ref(20))
+      }
+      set(store, 'notShared', ref(10))
+      set(store, 'shared', toRef(store.$state, 'shared'))
+    })
+
+    const store = useStore(pinia)
+    const store2 = useStore(pinia)
+
+    expect(store.$state.shared).toBe(20)
+    expect(store.shared).toBe(20)
+    expect(store2.$state.shared).toBe(20)
+    expect(store2.shared).toBe(20)
+
+    store.$state.shared = 10
+    expect(store.$state.shared).toBe(10)
+    expect(store.shared).toBe(10)
+    expect(store2.$state.shared).toBe(10)
+    expect(store2.shared).toBe(10)
+
+    store.shared = 1
+    expect(store.$state.shared).toBe(1)
+    expect(store.shared).toBe(1)
+    expect(store2.$state.shared).toBe(1)
+    expect(store2.shared).toBe(1)
+
+    store.notShared = 5
+    expect(store.$state).not.toHaveProperty('notShared')
+    expect(store.notShared).toBe(5)
+    expect(store2.$state).not.toHaveProperty('notShared')
+    expect(store2.notShared).toBe(10)
+  })
 })
index 1f8de1e1ed47fbe012be36451efdfe71426a0bce..2fcbf822e4c1d89a8ad143431ff199fa332d044a 100644 (file)
@@ -12,6 +12,7 @@ declare module '../dist/src' {
 
   export interface PiniaCustomStateProperties<S> {
     myState: number
+    stateOnly: number
   }
 
   export interface DefineStoreOptions<Id, S, G, A> {
@@ -50,6 +51,19 @@ const useStore = defineStore({
     },
   },
 
+  getters: {
+    two(state): boolean {
+      expectType<number>(this.myState)
+      expectType<number>(state.myState)
+      expectType<number>(state.stateOnly)
+
+      // @ts-expect-error
+      this.stateOnly
+
+      return true
+    },
+  },
+
   debounce: {
     one: 200,
     two: 300,