Skip to content

Commit 94b9b37

Browse files
Alfred-Skyblueautofix-ci[bot]haoqunjiang
authored
test: improve test coverage (#9203)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Haoqun Jiang <[email protected]>
1 parent 87c5443 commit 94b9b37

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

packages/reactivity/__tests__/ref.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ref,
88
toRef,
99
toRefs,
10+
toValue,
1011
} from '../src/index'
1112
import { computed } from '@vue/runtime-dom'
1213
import { customRef, shallowRef, triggerRef, unref } from '../src/ref'
@@ -251,6 +252,18 @@ describe('reactivity/ref', () => {
251252
x: 1,
252253
})
253254
const x = toRef(a, 'x')
255+
256+
const b = ref({ y: 1 })
257+
258+
const c = toRef(b)
259+
260+
const d = toRef({ z: 1 })
261+
262+
expect(isRef(d)).toBe(true)
263+
expect(d.value.z).toBe(1)
264+
265+
expect(c).toBe(b)
266+
254267
expect(isRef(x)).toBe(true)
255268
expect(x.value).toBe(1)
256269

@@ -442,4 +455,16 @@ describe('reactivity/ref', () => {
442455
expect(a.value).toBe(rr)
443456
expect(a.value).not.toBe(r)
444457
})
458+
459+
test('toValue', () => {
460+
const a = ref(1)
461+
const b = computed(() => a.value + 1)
462+
const c = () => a.value + 2
463+
const d = 4
464+
465+
expect(toValue(a)).toBe(1)
466+
expect(toValue(b)).toBe(2)
467+
expect(toValue(c)).toBe(3)
468+
expect(toValue(d)).toBe(4)
469+
})
445470
})

packages/runtime-core/__tests__/apiWatch.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,4 +1516,20 @@ describe('api: watch', () => {
15161516
unwatch!()
15171517
expect(scope.effects.length).toBe(0)
15181518
})
1519+
1520+
test('circular reference', async () => {
1521+
const obj = { a: 1 }
1522+
// @ts-expect-error
1523+
obj.b = obj
1524+
const foo = ref(obj)
1525+
const spy = vi.fn()
1526+
1527+
watch(foo, spy, { deep: true })
1528+
1529+
// @ts-expect-error
1530+
foo.value.b.a = 2
1531+
await nextTick()
1532+
expect(spy).toHaveBeenCalledTimes(1)
1533+
expect(foo.value.a).toBe(2)
1534+
})
15191535
})

packages/runtime-core/__tests__/vnode.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ describe('vnode', () => {
291291
const cloned8 = cloneVNode(original4)
292292
expect(cloned8.ref).toMatchObject({ i: mockInstance2, r, k: 'foo' })
293293

294+
// @ts-expect-error #8230
295+
const original5 = createVNode('div', { ref: 111, ref_key: 'foo' })
296+
expect(original5.ref).toMatchObject({
297+
i: mockInstance2,
298+
r: '111',
299+
k: 'foo',
300+
})
301+
const cloned9 = cloneVNode(original5)
302+
expect(cloned9.ref).toMatchObject({ i: mockInstance2, r: '111', k: 'foo' })
303+
294304
setCurrentRenderingInstance(null)
295305
})
296306

packages/runtime-dom/__tests__/directives/vModel.spec.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,13 @@ describe('vModel', () => {
256256
it('should support modifiers', async () => {
257257
const component = defineComponent({
258258
data() {
259-
return { number: null, trim: null, lazy: null, trimNumber: null }
259+
return {
260+
number: null,
261+
trim: null,
262+
lazy: null,
263+
trimNumber: null,
264+
trimLazy: null,
265+
}
260266
},
261267
render() {
262268
return [
@@ -284,6 +290,19 @@ describe('vModel', () => {
284290
trim: true,
285291
},
286292
),
293+
withVModel(
294+
h('input', {
295+
class: 'trim-lazy',
296+
'onUpdate:modelValue': (val: any) => {
297+
this.trimLazy = val
298+
},
299+
}),
300+
this.trim,
301+
{
302+
trim: true,
303+
lazy: true,
304+
},
305+
),
287306
withVModel(
288307
h('input', {
289308
class: 'trim-number',
@@ -317,6 +336,7 @@ describe('vModel', () => {
317336
const number = root.querySelector('.number')
318337
const trim = root.querySelector('.trim')
319338
const trimNumber = root.querySelector('.trim-number')
339+
const trimLazy = root.querySelector('.trim-lazy')
320340
const lazy = root.querySelector('.lazy')
321341
const data = root._vnode.component.data
322342

@@ -340,6 +360,11 @@ describe('vModel', () => {
340360
await nextTick()
341361
expect(data.trimNumber).toEqual(1.2)
342362

363+
trimLazy.value = ' ddd '
364+
triggerEvent('change', trimLazy)
365+
await nextTick()
366+
expect(data.trimLazy).toEqual('ddd')
367+
343368
lazy.value = 'foo'
344369
triggerEvent('change', lazy)
345370
await nextTick()

0 commit comments

Comments
 (0)