import { createPinia, defineStore, setActivePinia } from '../src'
import { mount } from '@vue/test-utils'
-import { watch, nextTick, ref } from 'vue'
+import { watch, nextTick, defineComponent } from 'vue'
describe('Store Lifespan', () => {
function defineMyStore() {
}
const pinia = createPinia()
- // let pinia: object
- // const useStore = () => {
- // // create a new store
- // pinia = {}
- // setActivePinia(pinia)
- // return defineMyStore()()
- // }
+ it('state reactivity outlives component life', async () => {
+ const useStore = defineMyStore()
+ setActivePinia(createPinia())
- it('bug report', async () => {
const inComponentWatch = jest.fn()
- const n = ref(0)
+ const Component = defineComponent({
+ render: () => null,
+ setup() {
+ const store = useStore()
+ watch(() => store.n, inComponentWatch)
+ store.n++
+ },
+ })
- const wrapper = mount(
- {
- render: () => null,
- setup() {
- watch(() => n.value, inComponentWatch)
- n.value++
- },
+ const options = {
+ global: {
+ plugins: [pinia],
},
- {
- global: {
- plugins: [pinia],
- },
- }
- )
+ }
+
+ let wrapper = mount(Component, options)
await wrapper.unmount()
expect(inComponentWatch).toHaveBeenCalledTimes(1)
- // store!.n++
- n.value++
+ let store = useStore()
+ store.n++
await nextTick()
expect(inComponentWatch).toHaveBeenCalledTimes(1)
- })
-
- it('state reactivity outlives component life', async () => {
- const useStore = defineMyStore()
- setActivePinia(createPinia())
-
- const inComponentWatch = jest.fn()
-
- let store: ReturnType<typeof useStore>
-
- const n = ref(0)
-
- const wrapper = mount(
- {
- render: () => null,
- setup() {
- store = useStore()
- // watch(() => store.n, inComponentWatch)
- watch(() => n.value, inComponentWatch)
- store.n++
- n.value++
- },
- },
- {
- global: {
- plugins: [pinia],
- },
- }
- )
+ wrapper = mount(Component, options)
await wrapper.unmount()
- expect(inComponentWatch).toHaveBeenCalledTimes(1)
+ expect(inComponentWatch).toHaveBeenCalledTimes(2)
- // store!.n++
- n.value++
+ store = useStore()
+ store.n++
await nextTick()
- expect(inComponentWatch).toHaveBeenCalledTimes(1)
+ expect(inComponentWatch).toHaveBeenCalledTimes(2)
})
})