fix(reactivity): track reactive keys in raw collection types

Also warn against presence of both raw and reactive versions of the
same object in a collection as keys.

fix #919
This commit is contained in:
Evan You
2020-04-04 12:57:15 -04:00
parent 7402951d94
commit 5dcc645fc0
3 changed files with 126 additions and 6 deletions

View File

@@ -1,7 +1,10 @@
import { reactive, effect, isReactive, toRaw } from '../../src'
import { mockWarn } from '@vue/shared'
describe('reactivity/collections', () => {
describe('Set', () => {
mockWarn()
it('instanceof', () => {
const original = new Set()
const observed = reactive(original)
@@ -380,5 +383,34 @@ describe('reactivity/collections', () => {
expect(set.delete(entry)).toBe(true)
expect(set.has(entry)).toBe(false)
})
it('should track deletion of reactive entries in raw set', () => {
const raw = new Set()
const entry = reactive({})
raw.add(entry)
const set = reactive(raw)
let dummy
effect(() => {
dummy = set.has(entry)
})
expect(dummy).toBe(true)
set.delete(entry)
expect(dummy).toBe(false)
})
it('should warn when set contains both raw and reactive versions of the same object', () => {
const raw = new Set()
const rawKey = {}
const key = reactive(rawKey)
raw.add(rawKey)
raw.add(key)
const set = reactive(raw)
set.delete(key)
expect(
`Reactive Set contains both the raw and reactive`
).toHaveBeenWarned()
})
})
})