/
shallowReactive.spec.ts
189 lines (157 loc) 路 4.67 KB
/
shallowReactive.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import {
isReactive,
isShallow,
reactive,
shallowReactive,
shallowReadonly
} from '../src/reactive'
import { effect } from '../src/effect'
import { Ref, isRef, ref } from '../src/ref'
describe('shallowReactive', () => {
test('should not make non-reactive properties reactive', () => {
const props = shallowReactive({ n: { foo: 1 } })
expect(isReactive(props.n)).toBe(false)
})
test('should keep reactive properties reactive', () => {
const props: any = shallowReactive({ n: reactive({ foo: 1 }) })
props.n = reactive({ foo: 2 })
expect(isReactive(props.n)).toBe(true)
})
// #2843
test('should allow shallow and normal reactive for same target', () => {
const original = { foo: {} }
const shallowProxy = shallowReactive(original)
const reactiveProxy = reactive(original)
expect(shallowProxy).not.toBe(reactiveProxy)
expect(isReactive(shallowProxy.foo)).toBe(false)
expect(isReactive(reactiveProxy.foo)).toBe(true)
})
test('isShallow', () => {
expect(isShallow(shallowReactive({}))).toBe(true)
expect(isShallow(shallowReadonly({}))).toBe(true)
})
// #5271
test('should respect shallow reactive nested inside reactive on reset', () => {
const r = reactive({ foo: shallowReactive({ bar: {} }) })
expect(isShallow(r.foo)).toBe(true)
expect(isReactive(r.foo.bar)).toBe(false)
r.foo = shallowReactive({ bar: {} })
expect(isShallow(r.foo)).toBe(true)
expect(isReactive(r.foo.bar)).toBe(false)
})
// vuejs/vue#12597
test('should not unwrap refs', () => {
const foo = shallowReactive({
bar: ref(123)
})
expect(isRef(foo.bar)).toBe(true)
expect(foo.bar.value).toBe(123)
})
// vuejs/vue#12688
test('should not mutate refs', () => {
const original = ref(123)
const foo = shallowReactive<{ bar: Ref<number> | number }>({
bar: original
})
expect(foo.bar).toBe(original)
foo.bar = 234
expect(foo.bar).toBe(234)
expect(original.value).toBe(123)
})
test('should respect shallow/deep versions of same target on access', () => {
const original = {}
const shallow = shallowReactive(original)
const deep = reactive(original)
const r = reactive({ shallow, deep })
expect(r.shallow).toBe(shallow)
expect(r.deep).toBe(deep)
})
describe('collections', () => {
test('should be reactive', () => {
const shallowSet = shallowReactive(new Set())
const a = {}
let size
effect(() => {
size = shallowSet.size
})
expect(size).toBe(0)
shallowSet.add(a)
expect(size).toBe(1)
shallowSet.delete(a)
expect(size).toBe(0)
})
test('should not observe when iterating', () => {
const shallowSet = shallowReactive(new Set())
const a = {}
shallowSet.add(a)
const spreadA = [...shallowSet][0]
expect(isReactive(spreadA)).toBe(false)
})
test('should not get reactive entry', () => {
const shallowMap = shallowReactive(new Map())
const a = {}
const key = 'a'
shallowMap.set(key, a)
expect(isReactive(shallowMap.get(key))).toBe(false)
})
test('should not get reactive on foreach', () => {
const shallowSet = shallowReactive(new Set())
const a = {}
shallowSet.add(a)
shallowSet.forEach(x => expect(isReactive(x)).toBe(false))
})
// #1210
test('onTrack on called on objectSpread', () => {
const onTrackFn = jest.fn()
const shallowSet = shallowReactive(new Set())
let a
effect(
() => {
a = Array.from(shallowSet)
},
{
onTrack: onTrackFn
}
)
expect(a).toMatchObject([])
expect(onTrackFn).toHaveBeenCalled()
})
})
describe('array', () => {
test('should be reactive', () => {
const shallowArray = shallowReactive<unknown[]>([])
const a = {}
let size
effect(() => {
size = shallowArray.length
})
expect(size).toBe(0)
shallowArray.push(a)
expect(size).toBe(1)
shallowArray.pop()
expect(size).toBe(0)
})
test('should not observe when iterating', () => {
const shallowArray = shallowReactive<object[]>([])
const a = {}
shallowArray.push(a)
const spreadA = [...shallowArray][0]
expect(isReactive(spreadA)).toBe(false)
})
test('onTrack on called on objectSpread', () => {
const onTrackFn = jest.fn()
const shallowArray = shallowReactive([])
let a
effect(
() => {
a = Array.from(shallowArray)
},
{
onTrack: onTrackFn
}
)
expect(a).toMatchObject([])
expect(onTrackFn).toHaveBeenCalled()
})
})
})