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
}
}
// 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'))
const store = useStore()
+ expect(store.$state.pluginN).toBe(20)
expect(store.pluginN).toBe(20)
// @ts-expect-error: n is a number
store.pluginN.notExisting
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)
+ })
})