Skip to content

Commit dcb5985

Browse files
committed
Merge remote-tracking branch 'github/master' into changing_unwrap_ref
2 parents 6a66b7b + 1068212 commit dcb5985

23 files changed

+361
-318
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* **compiler:** compiler options have been adjusted.
3131
- new option `decodeEntities` is added.
3232
- `namedCharacterReferences` option has been removed.
33-
- `maxCRNameLength` option has been rmeoved.
33+
- `maxCRNameLength` option has been removed.
3434
* **asyncComponent:** `retryWhen` and `maxRetries` options for
3535
`defineAsyncComponent` has been replaced by the more flexible `onError`
3636
option, per https://github.com/vuejs/rfcs/pull/148

packages/reactivity/__tests__/readonly.spec.ts

Lines changed: 15 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import {
55
isReactive,
66
isReadonly,
77
markNonReactive,
8-
markReadonly,
9-
lock,
10-
unlock,
118
effect,
129
ref,
1310
shallowReadonly
@@ -91,22 +88,7 @@ describe('reactivity/readonly', () => {
9188
).toHaveBeenWarnedLast()
9289
})
9390

94-
it('should allow mutation when unlocked', () => {
95-
const observed: any = readonly({ foo: 1, bar: { baz: 2 } })
96-
unlock()
97-
observed.prop = 2
98-
observed.bar.qux = 3
99-
delete observed.bar.baz
100-
delete observed.foo
101-
lock()
102-
expect(observed.prop).toBe(2)
103-
expect(observed.foo).toBeUndefined()
104-
expect(observed.bar.qux).toBe(3)
105-
expect('baz' in observed.bar).toBe(false)
106-
expect(`target is readonly`).not.toHaveBeenWarned()
107-
})
108-
109-
it('should not trigger effects when locked', () => {
91+
it('should not trigger effects', () => {
11092
const observed: any = readonly({ a: 1 })
11193
let dummy
11294
effect(() => {
@@ -118,20 +100,6 @@ describe('reactivity/readonly', () => {
118100
expect(dummy).toBe(1)
119101
expect(`target is readonly`).toHaveBeenWarned()
120102
})
121-
122-
it('should trigger effects when unlocked', () => {
123-
const observed: any = readonly({ a: 1 })
124-
let dummy
125-
effect(() => {
126-
dummy = observed.a
127-
})
128-
expect(dummy).toBe(1)
129-
unlock()
130-
observed.a = 2
131-
lock()
132-
expect(observed.a).toBe(2)
133-
expect(dummy).toBe(2)
134-
})
135103
})
136104

137105
describe('Array', () => {
@@ -183,23 +151,7 @@ describe('reactivity/readonly', () => {
183151
expect(`target is readonly.`).toHaveBeenWarnedTimes(5)
184152
})
185153

186-
it('should allow mutation when unlocked', () => {
187-
const observed: any = readonly([{ foo: 1, bar: { baz: 2 } }])
188-
unlock()
189-
observed[1] = 2
190-
observed.push(3)
191-
observed[0].foo = 2
192-
observed[0].bar.baz = 3
193-
lock()
194-
expect(observed.length).toBe(3)
195-
expect(observed[1]).toBe(2)
196-
expect(observed[2]).toBe(3)
197-
expect(observed[0].foo).toBe(2)
198-
expect(observed[0].bar.baz).toBe(3)
199-
expect(`target is readonly`).not.toHaveBeenWarned()
200-
})
201-
202-
it('should not trigger effects when locked', () => {
154+
it('should not trigger effects', () => {
203155
const observed: any = readonly([{ a: 1 }])
204156
let dummy
205157
effect(() => {
@@ -215,30 +167,6 @@ describe('reactivity/readonly', () => {
215167
expect(dummy).toBe(1)
216168
expect(`target is readonly`).toHaveBeenWarnedTimes(2)
217169
})
218-
219-
it('should trigger effects when unlocked', () => {
220-
const observed: any = readonly([{ a: 1 }])
221-
let dummy
222-
effect(() => {
223-
dummy = observed[0].a
224-
})
225-
expect(dummy).toBe(1)
226-
227-
unlock()
228-
229-
observed[0].a = 2
230-
expect(observed[0].a).toBe(2)
231-
expect(dummy).toBe(2)
232-
233-
observed[0] = { a: 3 }
234-
expect(observed[0].a).toBe(3)
235-
expect(dummy).toBe(3)
236-
237-
observed.unshift({ a: 4 })
238-
expect(observed[0].a).toBe(4)
239-
expect(dummy).toBe(4)
240-
lock()
241-
})
242170
})
243171

244172
const maps = [Map, WeakMap]
@@ -276,23 +204,6 @@ describe('reactivity/readonly', () => {
276204
).toHaveBeenWarned()
277205
})
278206

279-
test('should allow mutation & trigger effect when unlocked', () => {
280-
const map = readonly(new Collection())
281-
const isWeak = Collection === WeakMap
282-
const key = {}
283-
let dummy
284-
effect(() => {
285-
dummy = map.get(key) + (isWeak ? 0 : map.size)
286-
})
287-
expect(dummy).toBeNaN()
288-
unlock()
289-
map.set(key, 1)
290-
lock()
291-
expect(dummy).toBe(isWeak ? 1 : 2)
292-
expect(map.get(key)).toBe(1)
293-
expect(`target is readonly`).not.toHaveBeenWarned()
294-
})
295-
296207
if (Collection === Map) {
297208
test('should retrieve readonly values on iteration', () => {
298209
const key1 = {}
@@ -347,22 +258,6 @@ describe('reactivity/readonly', () => {
347258
).toHaveBeenWarned()
348259
})
349260

350-
test('should allow mutation & trigger effect when unlocked', () => {
351-
const set = readonly(new Collection())
352-
const key = {}
353-
let dummy
354-
effect(() => {
355-
dummy = set.has(key)
356-
})
357-
expect(dummy).toBe(false)
358-
unlock()
359-
set.add(key)
360-
lock()
361-
expect(dummy).toBe(true)
362-
expect(set.has(key)).toBe(true)
363-
expect(`target is readonly`).not.toHaveBeenWarned()
364-
})
365-
366261
if (Collection === Set) {
367262
test('should retrieve readonly values on iteration', () => {
368263
const original = new Collection([{}, {}])
@@ -401,6 +296,19 @@ describe('reactivity/readonly', () => {
401296
expect(toRaw(a)).toBe(toRaw(b))
402297
})
403298

299+
test('readonly should track and trigger if wrapping reactive original', () => {
300+
const a = reactive({ n: 1 })
301+
const b = readonly(a)
302+
let dummy
303+
effect(() => {
304+
dummy = b.n
305+
})
306+
expect(dummy).toBe(1)
307+
a.n++
308+
expect(b.n).toBe(2)
309+
expect(dummy).toBe(2)
310+
})
311+
404312
test('observing already observed value should return same Proxy', () => {
405313
const original = { foo: 1 }
406314
const observed = readonly(original)
@@ -424,17 +332,6 @@ describe('reactivity/readonly', () => {
424332
expect(isReactive(obj.bar)).toBe(false)
425333
})
426334

427-
test('markReadonly', () => {
428-
const obj = reactive({
429-
foo: { a: 1 },
430-
bar: markReadonly({ b: 2 })
431-
})
432-
expect(isReactive(obj.foo)).toBe(true)
433-
expect(isReactive(obj.bar)).toBe(true)
434-
expect(isReadonly(obj.foo)).toBe(false)
435-
expect(isReadonly(obj.bar)).toBe(true)
436-
})
437-
438335
test('should make ref readonly', () => {
439336
const n: any = readonly(ref(1))
440337
n.value = 2
@@ -470,13 +367,5 @@ describe('reactivity/readonly', () => {
470367
`Set operation on key "foo" failed: target is readonly.`
471368
).not.toHaveBeenWarned()
472369
})
473-
474-
test('should keep reactive properties reactive', () => {
475-
const props: any = shallowReadonly({ n: reactive({ foo: 1 }) })
476-
unlock()
477-
props.n = reactive({ foo: 2 })
478-
lock()
479-
expect(isReactive(props.n)).toBe(true)
480-
})
481370
})
482371
})

packages/reactivity/__tests__/ref.spec.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import {
33
effect,
44
reactive,
55
isRef,
6+
toRef,
67
toRefs,
78
Ref,
89
isReactive
910
} from '../src/index'
1011
import { computed } from '@vue/runtime-dom'
11-
import { shallowRef, unref } from '../src/ref'
12+
import { shallowRef, unref, customRef } from '../src/ref'
1213

1314
describe('reactivity/ref', () => {
1415
it('should hold a value', () => {
@@ -168,6 +169,34 @@ describe('reactivity/ref', () => {
168169
expect(isRef({ value: 0 })).toBe(false)
169170
})
170171

172+
test('toRef', () => {
173+
const a = reactive({
174+
x: 1
175+
})
176+
const x = toRef(a, 'x')
177+
expect(isRef(x)).toBe(true)
178+
expect(x.value).toBe(1)
179+
180+
// source -> proxy
181+
a.x = 2
182+
expect(x.value).toBe(2)
183+
184+
// proxy -> source
185+
x.value = 3
186+
expect(a.x).toBe(3)
187+
188+
// reactivity
189+
let dummyX
190+
effect(() => {
191+
dummyX = x.value
192+
})
193+
expect(dummyX).toBe(x.value)
194+
195+
// mutating source should trigger effect using the proxy refs
196+
a.x = 4
197+
expect(dummyX).toBe(4)
198+
})
199+
171200
test('toRefs', () => {
172201
const a = reactive({
173202
x: 1,
@@ -208,4 +237,35 @@ describe('reactivity/ref', () => {
208237
expect(dummyX).toBe(4)
209238
expect(dummyY).toBe(5)
210239
})
240+
241+
test('customRef', () => {
242+
let value = 1
243+
let _trigger: () => void
244+
245+
const custom = customRef((track, trigger) => ({
246+
get() {
247+
track()
248+
return value
249+
},
250+
set(newValue: number) {
251+
value = newValue
252+
_trigger = trigger
253+
}
254+
}))
255+
256+
expect(isRef(custom)).toBe(true)
257+
258+
let dummy
259+
effect(() => {
260+
dummy = custom.value
261+
})
262+
expect(dummy).toBe(1)
263+
264+
custom.value = 2
265+
// should not trigger yet
266+
expect(dummy).toBe(1)
267+
268+
_trigger!()
269+
expect(dummy).toBe(2)
270+
})
211271
})

0 commit comments

Comments
 (0)