b: { n: 0 },
})
})
+
+ it('allows overriding computed properties', () => {
+ const useStore = defineStore('lol', {
+ state: () => ({ n: 0 }),
+ getters: {
+ double: (state) => state.n * 2,
+ },
+ })
+ const pinia = createTestingPinia()
+ const store = useStore(pinia)
+
+ store.n++
+ expect(store.double).toBe(2)
+ // once the getter is overridden, it stays
+ store.double = 3
+ expect(store.double).toBe(3)
+ store.n++
+ expect(store.double).toBe(3)
+ // it can be set to undefined again to reset
+ // @ts-expect-error
+ store.double = undefined
+ expect(store.double).toBe(4)
+ store.n++
+ expect(store.double).toBe(6)
+ })
})
-import { App, createApp, isReactive, isRef, isVue2, set } from 'vue-demi'
+import {
+ App,
+ createApp,
+ customRef,
+ isReactive,
+ isRef,
+ isVue2,
+ set,
+ toRaw,
+} from 'vue-demi'
+import type { ComputedRef, WritableComputedRef } from 'vue-demi'
import {
Pinia,
PiniaPlugin,
createPinia,
StateTree,
_DeepPartial,
+ PiniaPluginContext,
} from 'pinia'
export interface TestingOptions {
}
})
+ // allow computed to be manually overridden
+ pinia._p.push(WritableComputed)
+
plugins.forEach((plugin) => pinia.use(plugin))
const createSpy =
typeof o.toJSON !== 'function'
)
}
+
+function isComputed<T>(
+ v: ComputedRef<T> | WritableComputedRef<T> | unknown
+): v is ComputedRef<T> | WritableComputedRef<T> {
+ return !!v && isRef(v) && 'effect' in v
+}
+
+function WritableComputed({ store }: PiniaPluginContext) {
+ const rawStore = toRaw(store)
+ for (const key in rawStore) {
+ const value = rawStore[key]
+ if (isComputed(value)) {
+ rawStore[key] = customRef((track, trigger) => {
+ let internalValue: any
+ return {
+ get: () => {
+ track()
+ return internalValue !== undefined ? internalValue : value.value
+ },
+ set: (newValue) => {
+ internalValue = newValue
+ trigger()
+ },
+ }
+ })
+ }
+ }
+}