]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: add failing patch
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 4 Aug 2021 06:19:59 +0000 (08:19 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 4 Aug 2021 06:19:59 +0000 (08:19 +0200)
__tests__/store.patch.spec.ts
src/store.ts

index 3216954ec14ee850e68ec19409313a410f3d03a0..e4492e24a68858c63d4b860c3020c91c931d4089 100644 (file)
@@ -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
index 63d9d59a0e27f8482c157f18a6663000a24ebf17..c4cdcf042981d2c3ebe556bfbbd49aefa56d2c5f 100644 (file)
@@ -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)
         }
       })