]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(testing): allow overriding plugin computed properties
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 25 Apr 2022 13:11:55 +0000 (15:11 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 25 Apr 2022 13:11:55 +0000 (15:11 +0200)
packages/testing/src/testing.spec.ts
packages/testing/src/testing.ts

index 4b0b4364624fb01271bb22f1124c28f9304a0272..ef1b206918104872bff8bd95621f8a4ca665f864 100644 (file)
@@ -281,4 +281,35 @@ describe('Testing', () => {
     expect(spy).toHaveBeenCalledTimes(2)
     expect(spy).toHaveBeenLastCalledWith(5)
   })
+
+  it('can override computed added in plugins', () => {
+    const pinia = createTestingPinia({
+      plugins: [
+        ({ store }) => {
+          store.triple = computed(() => store.n * 3)
+        },
+      ],
+    })
+
+    const store = useCounter(pinia)
+    store.n++
+    // @ts-expect-error: non declared
+    expect(store.triple).toBe(3)
+    // once the getter is overridden, it stays
+    // @ts-expect-error: non declared
+    store.triple = 10
+    // @ts-expect-error: non declared
+    expect(store.triple).toBe(10)
+    store.n++
+    // @ts-expect-error: non declared
+    expect(store.triple).toBe(10)
+    // it can be set to undefined again to reset
+    // @ts-expect-error
+    store.triple = undefined
+    // @ts-expect-error: non declared
+    expect(store.triple).toBe(6)
+    store.n++
+    // @ts-expect-error: non declared
+    expect(store.triple).toBe(9)
+  })
 })
index 11f85f4199726ca58f14054c996db4b480c59657..7afaadfbaee74cb91dbfa99573894e513cd7beee 100644 (file)
@@ -106,12 +106,12 @@ export function createTestingPinia({
     }
   })
 
-  // allow computed to be manually overridden
-  pinia._p.push(WritableComputed)
-
   // bypass waiting for the app to be installed to ensure the action stubbing happens last
   plugins.forEach((plugin) => pinia._p.push(plugin))
 
+  // allow computed to be manually overridden
+  pinia._p.push(WritableComputed)
+
   const createSpy =
     _createSpy ||
     (typeof jest !== 'undefined' && jest.fn) ||