]> 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:13:14 +0000 (16:13 +0200)
__tests__/storePlugins.spec.ts
test-dts/customizations.test-d.ts

index a6d7b8b00a3b0ceeca6e4ec9933212bffb5e13df..747518755cc4365bdf97895880806a97378a2cd2 100644 (file)
@@ -10,11 +10,14 @@ declare module '../src' {
     idFromPlugin: Id
     globalA: string
     globalB: string
+    notShared: number
+    shared: number
   }
 
   export interface PiniaCustomStateProperties<S> {
     // pluginN: 'test' extends Id ? number : never | undefined
     pluginN: number
+    shared: number
   }
 }
 
@@ -40,9 +43,8 @@ describe('store plugins', () => {
 
     // must call use after installing the plugin
     pinia.use(({ app, store }) => {
-      if (!('pluginN' in store.$state)) {
-        // @ts-expect-error: the check above makes this impossible
-        // but in practice we need this until effectScope is released
+      if (!store.$state.hasOwnProperty('pluginN')) {
+        // @ts-expect-error: cannot be a ref yet
         store.$state.pluginN = ref(20)
       }
       // @ts-expect-error: TODO: allow setting refs
@@ -52,6 +54,7 @@ describe('store plugins', () => {
 
     const store = useStore(pinia)
 
+    expect(store.$state.pluginN).toBe(20)
     expect(store.pluginN).toBe(20)
     expect(store.uid).toBeDefined()
     // @ts-expect-error: pluginN is a number
@@ -118,4 +121,48 @@ describe('store plugins', () => {
     expect(store.globalA).toBe('a')
     expect(store.globalB).toBe('b')
   })
+
+  it('shares the same ref among stores', () => {
+    const pinia = createPinia()
+
+    mount({ template: 'none' }, { global: { plugins: [pinia] } })
+
+    // must call use after installing the plugin
+    pinia.use(({ app, store }) => {
+      if (!store.$state.hasOwnProperty('shared')) {
+        // @ts-expect-error: cannot be a ref yet
+        store.$state.shared = ref(20)
+      }
+      // @ts-expect-error: TODO: allow setting refs
+      store.notShared = ref(10)
+      // @ts-expect-error: TODO: allow setting refs
+      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 7e6e6155343d5b39835ce040fac5757d4b11771b..54f91b2799b2a872dc6b245db11c8e9829d8b7e5 100644 (file)
@@ -13,6 +13,7 @@ declare module '../dist/pinia' {
 
   export interface PiniaCustomStateProperties<S> {
     myState: number
+    stateOnly: number
   }
 
   export interface DefineStoreOptions<Id, S, G, A> {
@@ -52,6 +53,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,