})()
}
+ const useArrayStore = () => {
+ // create a new store
+ setActivePinia(createPinia())
+ return defineStore({
+ id: 'main',
+ state: () => ({
+ items: [{ id: 0 }],
+ currentItem: { id: 1 },
+ }),
+ })()
+ }
+
it('patches a property without touching the rest', () => {
const store = useStore()
store.$patch({ a: false })
expect(store.list).toEqual([1, 2])
})
+ it('can patch an item that has been copied to an array', () => {
+ const store = useArrayStore()
+ store.$patch({ currentItem: { id: 2 } })
+ store.items.push(store.currentItem)
+ store.$patch({ currentItem: { id: 3 } })
+
+ expect(store.$state.items).toEqual([{ id: 0 }, { id: 2 }])
+ expect(store.items).toEqual([{ id: 0 }, { id: 2 }])
+ })
+
it('replaces whole nested arrays', () => {
const store = useStore()
// @ts-expect-error: new state
Ref,
ref,
set,
+ del,
isVue2,
} from 'vue-demi'
import {
}
// patch direct access properties to allow store.stateProperty to work as
// store.$state.stateProperty
- // @ts-expect-error
- store[stateKey] = toRef(newStore.$state, stateKey)
+ set(store, stateKey, toRef(newStore.$state, stateKey))
})
// remove deleted state properties
Object.keys(store.$state).forEach((stateKey) => {
if (!(stateKey in newStore.$state)) {
- delete store[stateKey]
+ del(store, stateKey)
}
})
for (const actionName in newStore._hmrPayload.actions) {
const action: _Method = newStore[actionName]
- // @ts-expect-error: new key
- store[actionName] =
- // new line forced for TS
- wrapAction(actionName, action)
+ set(store, actionName, wrapAction(actionName, action))
}
// TODO: does this work in both setup and option store?
for (const getterName in newStore._hmrPayload.getters) {
const getter: _Method = newStore._hmrPayload.getters[getterName]
- // @ts-expect-error
- store[getterName] =
- // ---
- buildState
- ? // special handling of options api
- computed(() => {
- setActivePinia(pinia)
- return getter.call(store, store)
- })
- : getter
+ const getterValue = buildState
+ ? // special handling of options api
+ computed(() => {
+ setActivePinia(pinia)
+ return getter.call(store, store)
+ })
+ : getter
+
+ set(store, getterName, getterValue)
}
// remove deleted getters
Object.keys(store._hmrPayload.getters).forEach((key) => {
if (!(key in newStore._hmrPayload.getters)) {
- delete store[key]
+ del(store, key)
}
})
// remove old actions
Object.keys(store._hmrPayload.actions).forEach((key) => {
if (!(key in newStore._hmrPayload.actions)) {
- delete store[key]
+ del(store, key)
}
})