From 94b9b37362310c34b846a634a5365b9b2d5db937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=9C=E6=96=B9os?= Date: Tue, 28 May 2024 17:36:29 +0800 Subject: [PATCH] test: improve test coverage (#9203) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Haoqun Jiang --- packages/reactivity/__tests__/ref.spec.ts | 25 +++++++++++++++++ .../runtime-core/__tests__/apiWatch.spec.ts | 16 +++++++++++ packages/runtime-core/__tests__/vnode.spec.ts | 10 +++++++ .../__tests__/directives/vModel.spec.ts | 27 ++++++++++++++++++- 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/packages/reactivity/__tests__/ref.spec.ts b/packages/reactivity/__tests__/ref.spec.ts index 2b2024d9723..f42281edd31 100644 --- a/packages/reactivity/__tests__/ref.spec.ts +++ b/packages/reactivity/__tests__/ref.spec.ts @@ -7,6 +7,7 @@ import { ref, toRef, toRefs, + toValue, } from '../src/index' import { computed } from '@vue/runtime-dom' import { customRef, shallowRef, triggerRef, unref } from '../src/ref' @@ -251,6 +252,18 @@ describe('reactivity/ref', () => { x: 1, }) const x = toRef(a, 'x') + + const b = ref({ y: 1 }) + + const c = toRef(b) + + const d = toRef({ z: 1 }) + + expect(isRef(d)).toBe(true) + expect(d.value.z).toBe(1) + + expect(c).toBe(b) + expect(isRef(x)).toBe(true) expect(x.value).toBe(1) @@ -442,4 +455,16 @@ describe('reactivity/ref', () => { expect(a.value).toBe(rr) expect(a.value).not.toBe(r) }) + + test('toValue', () => { + const a = ref(1) + const b = computed(() => a.value + 1) + const c = () => a.value + 2 + const d = 4 + + expect(toValue(a)).toBe(1) + expect(toValue(b)).toBe(2) + expect(toValue(c)).toBe(3) + expect(toValue(d)).toBe(4) + }) }) diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 359e06394e4..8229dce36db 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -1516,4 +1516,20 @@ describe('api: watch', () => { unwatch!() expect(scope.effects.length).toBe(0) }) + + test('circular reference', async () => { + const obj = { a: 1 } + // @ts-expect-error + obj.b = obj + const foo = ref(obj) + const spy = vi.fn() + + watch(foo, spy, { deep: true }) + + // @ts-expect-error + foo.value.b.a = 2 + await nextTick() + expect(spy).toHaveBeenCalledTimes(1) + expect(foo.value.a).toBe(2) + }) }) diff --git a/packages/runtime-core/__tests__/vnode.spec.ts b/packages/runtime-core/__tests__/vnode.spec.ts index 653613ddb2e..29f5a042cc9 100644 --- a/packages/runtime-core/__tests__/vnode.spec.ts +++ b/packages/runtime-core/__tests__/vnode.spec.ts @@ -291,6 +291,16 @@ describe('vnode', () => { const cloned8 = cloneVNode(original4) expect(cloned8.ref).toMatchObject({ i: mockInstance2, r, k: 'foo' }) + // @ts-expect-error #8230 + const original5 = createVNode('div', { ref: 111, ref_key: 'foo' }) + expect(original5.ref).toMatchObject({ + i: mockInstance2, + r: '111', + k: 'foo', + }) + const cloned9 = cloneVNode(original5) + expect(cloned9.ref).toMatchObject({ i: mockInstance2, r: '111', k: 'foo' }) + setCurrentRenderingInstance(null) }) diff --git a/packages/runtime-dom/__tests__/directives/vModel.spec.ts b/packages/runtime-dom/__tests__/directives/vModel.spec.ts index 66b57b68964..5ff0fd3521f 100644 --- a/packages/runtime-dom/__tests__/directives/vModel.spec.ts +++ b/packages/runtime-dom/__tests__/directives/vModel.spec.ts @@ -256,7 +256,13 @@ describe('vModel', () => { it('should support modifiers', async () => { const component = defineComponent({ data() { - return { number: null, trim: null, lazy: null, trimNumber: null } + return { + number: null, + trim: null, + lazy: null, + trimNumber: null, + trimLazy: null, + } }, render() { return [ @@ -284,6 +290,19 @@ describe('vModel', () => { trim: true, }, ), + withVModel( + h('input', { + class: 'trim-lazy', + 'onUpdate:modelValue': (val: any) => { + this.trimLazy = val + }, + }), + this.trim, + { + trim: true, + lazy: true, + }, + ), withVModel( h('input', { class: 'trim-number', @@ -317,6 +336,7 @@ describe('vModel', () => { const number = root.querySelector('.number') const trim = root.querySelector('.trim') const trimNumber = root.querySelector('.trim-number') + const trimLazy = root.querySelector('.trim-lazy') const lazy = root.querySelector('.lazy') const data = root._vnode.component.data @@ -340,6 +360,11 @@ describe('vModel', () => { await nextTick() expect(data.trimNumber).toEqual(1.2) + trimLazy.value = ' ddd ' + triggerEvent('change', trimLazy) + await nextTick() + expect(data.trimLazy).toEqual('ddd') + lazy.value = 'foo' triggerEvent('change', lazy) await nextTick()