test: fix deep watch for Map/Set

This commit is contained in:
Evan You 2019-08-27 15:01:01 -04:00
parent 62e07a1b7e
commit 2b6ca9a7b6
2 changed files with 23 additions and 7 deletions

View File

@ -261,34 +261,45 @@ describe('api: watch', () => {
count: ref(0)
},
array: [1, 2, 3],
map: new Map([['a', 1], ['b', 2]])
map: new Map([['a', 1], ['b', 2]]),
set: new Set([1, 2, 3])
})
let dummy
watch(
() => state,
state => {
dummy = [state.nested.count, state.array[0], state.map.get('a')]
dummy = [
state.nested.count,
state.array[0],
state.map.get('a'),
state.set.has(1)
]
},
{ deep: true }
)
await nextTick()
expect(dummy).toEqual([0, 1, 1])
expect(dummy).toEqual([0, 1, 1, true])
state.nested.count++
await nextTick()
expect(dummy).toEqual([1, 1, 1])
expect(dummy).toEqual([1, 1, 1, true])
// nested array mutation
state.array[0] = 2
await nextTick()
expect(dummy).toEqual([1, 2, 1])
expect(dummy).toEqual([1, 2, 1, true])
// nested map mutation
state.map.set('a', 2)
await nextTick()
expect(dummy).toEqual([1, 2, 2])
expect(dummy).toEqual([1, 2, 2, true])
// nested set mutation
state.set.delete(1)
await nextTick()
expect(dummy).toEqual([1, 2, 2, false])
})
it('lazy', async () => {

View File

@ -164,7 +164,12 @@ function traverse(value: any, seen: Set<any> = new Set()) {
for (let i = 0; i < value.length; i++) {
traverse(value[i], seen)
}
} else if (value instanceof Map || value instanceof Set) {
} else if (value instanceof Map) {
;(value as any).forEach((v: any, key: any) => {
// to register mutation dep for existing keys
traverse(value.get(key), seen)
})
} else if (value instanceof Set) {
;(value as any).forEach((v: any) => {
traverse(v, seen)
})