From: Eduardo San Martin Morote Date: Thu, 24 Jun 2021 14:13:14 +0000 (+0200) Subject: test: correct customization types X-Git-Tag: v2.0.0-beta.5~31 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3703a824b44096aab31baef06aa15988d8e2f571;p=thirdparty%2Fvuejs%2Fpinia.git test: correct customization types --- diff --git a/__tests__/storePlugins.spec.ts b/__tests__/storePlugins.spec.ts index a6d7b8b0..74751875 100644 --- a/__tests__/storePlugins.spec.ts +++ b/__tests__/storePlugins.spec.ts @@ -10,11 +10,14 @@ declare module '../src' { idFromPlugin: Id globalA: string globalB: string + notShared: number + shared: number } export interface PiniaCustomStateProperties { // 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) + }) }) diff --git a/test-dts/customizations.test-d.ts b/test-dts/customizations.test-d.ts index 7e6e6155..54f91b27 100644 --- a/test-dts/customizations.test-d.ts +++ b/test-dts/customizations.test-d.ts @@ -13,6 +13,7 @@ declare module '../dist/pinia' { export interface PiniaCustomStateProperties { myState: number + stateOnly: number } export interface DefineStoreOptions { @@ -52,6 +53,19 @@ const useStore = defineStore({ }, }, + getters: { + two(state): boolean { + expectType(this.myState) + expectType(state.myState) + expectType(state.stateOnly) + + // @ts-expect-error + this.stateOnly + + return true + }, + }, + debounce: { one: 200, two: 300,