From: Eduardo San Martin Morote Date: Thu, 24 Jun 2021 14:13:14 +0000 (+0200) Subject: test: correct customization types X-Git-Tag: v0.5.3~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=47c6309ba13ed68922ad9895c6bb2847a6681198;p=thirdparty%2Fvuejs%2Fpinia.git test: correct customization types --- diff --git a/__tests__/storePlugins.spec.ts b/__tests__/storePlugins.spec.ts index b74050e2..93c9e75c 100644 --- a/__tests__/storePlugins.spec.ts +++ b/__tests__/storePlugins.spec.ts @@ -9,12 +9,17 @@ declare module '../src' { hasPinia: boolean idFromPlugin: Id someRef: number + globalA: string + globalB: string + notShared: number + shared: number } export interface PiniaCustomStateProperties { // 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: '

' }, { 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) + }) }) diff --git a/test-dts/customizations.test-d.ts b/test-dts/customizations.test-d.ts index 1f8de1e1..2fcbf822 100644 --- a/test-dts/customizations.test-d.ts +++ b/test-dts/customizations.test-d.ts @@ -12,6 +12,7 @@ declare module '../dist/src' { export interface PiniaCustomStateProperties { myState: number + stateOnly: number } export interface DefineStoreOptions { @@ -50,6 +51,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,