Skip to content

Commit

Permalink
test: improve test coverage (#9203)
Browse files Browse the repository at this point in the history
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Haoqun Jiang <[email protected]>
  • Loading branch information
3 people authored May 28, 2024
1 parent 87c5443 commit 94b9b37
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/reactivity/__tests__/ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
})
})
16 changes: 16 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
10 changes: 10 additions & 0 deletions packages/runtime-core/__tests__/vnode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
27 changes: 26 additions & 1 deletion packages/runtime-dom/__tests__/directives/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand Down

0 comments on commit 94b9b37

Please sign in to comment.