Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose providePinia to use multiple root stores #878

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions packages/pinia/__tests__/multipleRoots.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { createPinia, defineStore, providePinia } from '../src'
import { mount } from '@vue/test-utils'
import { defineComponent } from 'vue'

describe('Multiple Roots', () => {
function defineMyStore() {
return defineStore('main', {
state: () => ({
n: 0,
}),
})
}

it('provides a pinia to children components', async () => {
const pinia = createPinia()
const useA = defineMyStore()
let nestedA!: ReturnType<typeof useA>

const ChildComponent = defineComponent({
template: 'no',
setup() {
// should use the provided pinia by the parent
const store = useA()
nestedA = store
expect(store.n).toBe(0)
},
})
mount(
{
template: '<ChildComponent />',
components: { ChildComponent },
setup() {
providePinia(createPinia())
const store = useA()
expect(store.n).toBe(0)
store.n++
},
},
{ global: { plugins: [pinia] } }
)

const store = useA()
// should be the parent one
expect(store).not.toBe(nestedA)
expect(store.n).toBe(1)
})

it('should be able to use plugins', async () => {
const pinia = createPinia()
const useA = defineMyStore()

// @ts-expect-error: type not defined
pinia.use(() => ({ parent: true }))

const ChildComponent = defineComponent({
template: 'no',
setup() {
// should use the provided pinia by the parent
const store = useA()
// @ts-expect-error: not defined
expect(store.parent).toBeUndefined()
// @ts-expect-error: not defined
expect(store.child).toBe(true)
},
})
mount(
{
template: '<ChildComponent />',
components: { ChildComponent },
setup() {
const pinia = createPinia()
// @ts-expect-error: type not defined
pinia.use(() => ({ child: true }))
providePinia(pinia)
const store = useA()
// @ts-expect-error: not defined
expect(store.child).toBeUndefined()
// @ts-expect-error: not defined
expect(store.parent).toBe(true)
},
},
{ global: { plugins: [pinia] } }
)
})
})
2 changes: 1 addition & 1 deletion packages/pinia/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @module pinia
*/
export { setActivePinia, getActivePinia } from './rootStore'
export { setActivePinia, getActivePinia, providePinia } from './rootStore'
export { createPinia } from './createPinia'
export type {
Pinia,
Expand Down
8 changes: 8 additions & 0 deletions packages/pinia/src/rootStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getCurrentInstance,
inject,
InjectionKey,
provide,
Ref,
} from 'vue-demi'
import {
Expand Down Expand Up @@ -97,6 +98,13 @@ export const piniaSymbol = (
__DEV__ ? Symbol('pinia') : /* istanbul ignore next */ Symbol()
) as InjectionKey<Pinia>

/**
* Define which pinia to use in all child components.
*/
export function providePinia(pinia: Pinia) {
provide(piniaSymbol, pinia)
}

/**
* Context argument passed to Pinia plugins.
*/
Expand Down