-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
/
shallowReadonly.spec.ts
213 lines (177 loc) 路 6.65 KB
/
shallowReadonly.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { isReactive, isReadonly, readonly, shallowReadonly } from '../src'
describe('reactivity/shallowReadonly', () => {
test('should not make non-reactive properties reactive', () => {
const props = shallowReadonly({ n: { foo: 1 } })
expect(isReactive(props.n)).toBe(false)
})
test('should make root level properties readonly', () => {
const props = shallowReadonly({ n: 1 })
// @ts-expect-error
props.n = 2
expect(props.n).toBe(1)
expect(
`Set operation on key "n" failed: target is readonly.`
).toHaveBeenWarned()
})
// to retain 2.x behavior.
test('should NOT make nested properties readonly', () => {
const props = shallowReadonly({ n: { foo: 1 } })
props.n.foo = 2
expect(props.n.foo).toBe(2)
expect(
`Set operation on key "foo" failed: target is readonly.`
).not.toHaveBeenWarned()
})
// #2843
test('should differentiate from normal readonly calls', () => {
const original = { foo: {} }
const shallowProxy = shallowReadonly(original)
const reactiveProxy = readonly(original)
expect(shallowProxy).not.toBe(reactiveProxy)
expect(isReadonly(shallowProxy.foo)).toBe(false)
expect(isReadonly(reactiveProxy.foo)).toBe(true)
})
describe('collection/Map', () => {
;[Map, WeakMap].forEach(Collection => {
test('should make the map/weak-map readonly', () => {
const key = {}
const val = { foo: 1 }
const original = new Collection([[key, val]])
const sroMap = shallowReadonly(original)
expect(isReadonly(sroMap)).toBe(true)
expect(isReactive(sroMap)).toBe(false)
expect(sroMap.get(key)).toBe(val)
sroMap.set(key, {} as any)
expect(
`Set operation on key "[object Object]" failed: target is readonly.`
).toHaveBeenWarned()
})
test('should not make nested values readonly', () => {
const key = {}
const val = { foo: 1 }
const original = new Collection([[key, val]])
const sroMap = shallowReadonly(original)
expect(isReadonly(sroMap.get(key))).toBe(false)
expect(isReactive(sroMap.get(key))).toBe(false)
sroMap.get(key)!.foo = 2
expect(
`Set operation on key "foo" failed: target is readonly.`
).not.toHaveBeenWarned()
})
})
test('should not make the value generated by the iterable method readonly', () => {
const key = {}
const val = { foo: 1 }
const original = new Map([[key, val]])
const sroMap = shallowReadonly(original)
const values1 = [...sroMap.values()]
const values2 = [...sroMap.entries()]
expect(isReadonly(values1[0])).toBe(false)
expect(isReactive(values1[0])).toBe(false)
expect(values1[0]).toBe(val)
values1[0].foo = 2
expect(
`Set operation on key "foo" failed: target is readonly.`
).not.toHaveBeenWarned()
expect(isReadonly(values2[0][1])).toBe(false)
expect(isReactive(values2[0][1])).toBe(false)
expect(values2[0][1]).toBe(val)
values2[0][1].foo = 2
expect(
`Set operation on key "foo" failed: target is readonly.`
).not.toHaveBeenWarned()
})
test('should not make the value generated by the forEach method readonly', () => {
const val = { foo: 1 }
const original = new Map([['key', val]])
const sroMap = shallowReadonly(original)
sroMap.forEach(val => {
expect(isReadonly(val)).toBe(false)
expect(isReactive(val)).toBe(false)
expect(val).toBe(val)
val.foo = 2
expect(
`Set operation on key "foo" failed: target is readonly.`
).not.toHaveBeenWarned()
})
})
test('should return undefined from Map.clear() call', () => {
const sroMap = shallowReadonly(new Map())
expect(sroMap.clear()).toBeUndefined()
expect(`Clear operation failed: target is readonly.`).toHaveBeenWarned()
})
})
describe('collection/Set', () => {
test('should make the set/weak-set readonly', () => {
;[Set, WeakSet].forEach(Collection => {
const obj = { foo: 1 }
const original = new Collection([obj])
const sroSet = shallowReadonly(original)
expect(isReadonly(sroSet)).toBe(true)
expect(isReactive(sroSet)).toBe(false)
expect(sroSet.has(obj)).toBe(true)
sroSet.add({} as any)
expect(
`Add operation on key "[object Object]" failed: target is readonly.`
).toHaveBeenWarned()
})
})
test('should not make nested values readonly', () => {
const obj = { foo: 1 }
const original = new Set([obj])
const sroSet = shallowReadonly(original)
const values = [...sroSet.values()]
expect(values[0]).toBe(obj)
expect(isReadonly(values[0])).toBe(false)
expect(isReactive(values[0])).toBe(false)
sroSet.add({} as any)
expect(
`Add operation on key "[object Object]" failed: target is readonly.`
).toHaveBeenWarned()
values[0].foo = 2
expect(
`Set operation on key "foo" failed: target is readonly.`
).not.toHaveBeenWarned()
})
test('should not make the value generated by the iterable method readonly', () => {
const val = { foo: 1 }
const original = new Set([val])
const sroSet = shallowReadonly(original)
const values1 = [...sroSet.values()]
const values2 = [...sroSet.entries()]
expect(isReadonly(values1[0])).toBe(false)
expect(isReactive(values1[0])).toBe(false)
expect(values1[0]).toBe(val)
values1[0].foo = 2
expect(
`Set operation on key "foo" failed: target is readonly.`
).not.toHaveBeenWarned()
expect(isReadonly(values2[0][1])).toBe(false)
expect(isReactive(values2[0][1])).toBe(false)
expect(values2[0][1]).toBe(val)
values2[0][1].foo = 2
expect(
`Set operation on key "foo" failed: target is readonly.`
).not.toHaveBeenWarned()
})
test('should not make the value generated by the forEach method readonly', () => {
const val = { foo: 1 }
const original = new Set([val])
const sroSet = shallowReadonly(original)
sroSet.forEach(val => {
expect(isReadonly(val)).toBe(false)
expect(isReactive(val)).toBe(false)
expect(val).toBe(val)
val.foo = 2
expect(
`Set operation on key "foo" failed: target is readonly.`
).not.toHaveBeenWarned()
})
})
test('should return undefined from Set.clear() call', () => {
const sroSet = shallowReadonly(new Set())
expect(sroSet.clear()).toBeUndefined()
expect(`Clear operation failed: target is readonly.`).toHaveBeenWarned()
})
})
})