From: Eduardo San Martin Morote Date: Wed, 4 Aug 2021 06:19:59 +0000 (+0200) Subject: test: add failing patch X-Git-Tag: v2.0.0-rc.2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4abf2562225cbbead0c71e682d8c270ce980f303;p=thirdparty%2Fvuejs%2Fpinia.git test: add failing patch --- diff --git a/__tests__/store.patch.spec.ts b/__tests__/store.patch.spec.ts index 3216954e..e4492e24 100644 --- a/__tests__/store.patch.spec.ts +++ b/__tests__/store.patch.spec.ts @@ -18,6 +18,18 @@ describe('store.$patch', () => { })() } + 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 }) @@ -40,6 +52,16 @@ describe('store.$patch', () => { 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 diff --git a/src/store.ts b/src/store.ts index 63d9d59a..c4cdcf04 100644 --- a/src/store.ts +++ b/src/store.ts @@ -18,6 +18,7 @@ import { Ref, ref, set, + del, isVue2, } from 'vue-demi' import { @@ -471,14 +472,13 @@ function createSetupStore< } // 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) } }) @@ -490,38 +490,34 @@ function createSetupStore< 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) } })