* @vitest-environment node
*/
import { describe, it, expect } from 'vitest'
-import { createPinia, defineStore } from '../src'
+import { createPinia, defineStore, shouldHydrate } from '../src'
import { Component, createSSRApp, inject, ref, computed, customRef } from 'vue'
import { renderToString, ssrInterpolate } from '@vue/server-renderer'
import { useUserStore } from './pinia/stores/user'
expect(store.$state).toEqual({ start: 'start' })
})
+ // NOTE: added to make things easier but people should avoid creating objects
+ // with `Object.create(null)` and store them in pinia stores
+ // https://github.com/vuejs/pinia/issues/2837
+ it('can hydrate objects without a a null prototype chain', () => {
+ const obj = Object.create(null)
+ expect(() => {
+ shouldHydrate(obj)
+ }).not.toThrow()
+ })
+
describe('Setup Store', () => {
const useStore = defineStore('main', () => {
const count = ref(0)
* @returns true if `obj` should be hydrated
*/
export function shouldHydrate(obj: any) {
- return !isPlainObject(obj) || !obj.hasOwnProperty(skipHydrateSymbol)
+ return (
+ !isPlainObject(obj) ||
+ !Object.prototype.hasOwnProperty.call(obj, skipHydrateSymbol)
+ )
}
const { assign } = Object