-import { createPinia, defineStore, setActivePinia, Pinia } from '../src'
+import { createPinia, defineStore, setActivePinia } from '../src'
import { mount } from '@vue/test-utils'
import { defineComponent, getCurrentInstance, nextTick, watch } from 'vue'
describe('Store', () => {
- let pinia: Pinia
- const useStore = () => {
- // create a new store
- pinia = createPinia()
- setActivePinia(pinia)
- return defineStore({
- id: 'main',
- state: () => ({
- a: true,
- nested: {
- foo: 'foo',
- a: { b: 'string' },
- },
- }),
- })()
- }
+ beforeEach(() => {
+ setActivePinia(createPinia())
+ })
+
+ const useStore = defineStore({
+ id: 'main',
+ state: () => ({
+ a: true,
+ nested: {
+ foo: 'foo',
+ a: { b: 'string' },
+ },
+ }),
+ })
it('reuses a store', () => {
- setActivePinia(createPinia())
const useStore = defineStore({ id: 'main' })
expect(useStore()).toBe(useStore())
})
it('works with id as first argument', () => {
- setActivePinia(createPinia())
const useStore = defineStore('main', {
state: () => ({
a: true,
})
it('works without setting the active pinia', async () => {
+ setActivePinia(undefined)
const pinia = createPinia()
const useStore = defineStore({
id: 'main',
a: false,
nested: {
foo: 'bar',
- a: { b: 'string' },
+ a: { b: 'string 2' },
},
}
a: false,
nested: {
foo: 'bar',
- a: { b: 'string' },
+ a: { b: 'string 2' },
},
})
})
it('do not share the state between same id store', () => {
const store = useStore()
- const store2 = useStore()
+ const store2 = useStore(createPinia())
expect(store.$state).not.toBe(store2.$state)
store.$state.nested.a.b = 'hey'
expect(store2.$state.nested.a.b).toBe('string')