Skip to content

Commit

Permalink
fix(toRefs): keep class prototype, close #1530 (#1599)
Browse files Browse the repository at this point in the history
Co-authored-by: nikitamihailov <mikhaylovnv.dev@gmail.com>
  • Loading branch information
chaii3 and chaii3 committed May 16, 2022
1 parent b833957 commit 356da7d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
22 changes: 22 additions & 0 deletions packages/core/useVModel/index.test.ts
Expand Up @@ -155,4 +155,26 @@ describe('useVModel', () => {
expect(dataD.value).toBe(null)
expect(dataE.value).toBe('default-data')
})

it('Should work with classes', async () => {
const emitMock = vitest.fn()

class SomeClass {
num1 = 1

someMethod() {}
}

const props = { cl: new SomeClass() }

const ref = useVModel(props, 'cl', emitMock, { passive: true, deep: true })

ref.value.num1 = 10

await nextTick()

const emitValue = (emitMock as any).calls[0][1]

expect(emitValue instanceof SomeClass).toBeTruthy()
})
})
18 changes: 18 additions & 0 deletions packages/shared/toRefs/index.test.ts
Expand Up @@ -96,4 +96,22 @@ describe('toRefs', () => {
refs[1].value = 1
expect(spy).toHaveBeenLastCalledWith(['a', 1])
})

it('should save instance of class', () => {
class SomeClass {
v = 1

fn() {

}
}

const obj = ref(new SomeClass())

const { v } = toRefs(obj)

v.value = 10

expect(obj.value instanceof SomeClass).toBeTruthy()
})
})
6 changes: 5 additions & 1 deletion packages/shared/toRefs/index.ts
Expand Up @@ -31,7 +31,11 @@ export function toRefs<T extends object>(
objectRef.value = copy
}
else {
objectRef.value = { ...objectRef.value, [key]: v }
const newObject = { ...objectRef.value, [key]: v }

Object.setPrototypeOf(newObject, objectRef.value)

objectRef.value = newObject
}
},
}))
Expand Down

0 comments on commit 356da7d

Please sign in to comment.