From: Eduardo San Martin Morote Date: Thu, 29 Apr 2021 12:40:07 +0000 (+0200) Subject: feat(plugins): allow chaining X-Git-Tag: v2.0.0-alpha.14~24 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3a49d34f5d30c1d346243df0d043a0709b2a4861;p=thirdparty%2Fvuejs%2Fpinia.git feat(plugins): allow chaining --- diff --git a/__tests__/storePlugins.spec.ts b/__tests__/storePlugins.spec.ts index 13e5d248..fde8ccf3 100644 --- a/__tests__/storePlugins.spec.ts +++ b/__tests__/storePlugins.spec.ts @@ -8,6 +8,8 @@ declare module '../src' { uid: App['_uid'] hasApp: boolean idFromPlugin: Id + globalA: string + globalB: string } } @@ -27,6 +29,7 @@ describe('store plugins', () => { }, }, }) + it('adds properties to stores', () => { const pinia = createPinia() @@ -92,4 +95,17 @@ describe('store plugins', () => { const store = useStore(pinia) expect(store.doubleN).toBe(40) }) + + it('allows chaining', () => { + const pinia = createPinia() + + // must call use after installing the plugin + pinia.use(() => ({ globalA: 'a' })).use(() => ({ globalB: 'b' })) + + mount({ template: 'none' }, { global: { plugins: [pinia] } }) + + const store = useStore(pinia) + expect(store.globalA).toBe('a') + expect(store.globalB).toBe('b') + }) }) diff --git a/src/rootStore.ts b/src/rootStore.ts index 8c115f70..dd4f1c22 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -119,7 +119,7 @@ export interface Pinia { * * @param plugin - store plugin to add */ - use(plugin: PiniaStorePlugin): void + use(plugin: PiniaStorePlugin): Pinia /** * Installed store plugins @@ -194,6 +194,7 @@ export function createPinia(): Pinia { } else { _p.push(plugin) } + return this }, _p,