From: Evan You Date: Fri, 31 Jan 2025 03:24:00 +0000 (+0800) Subject: test(vapor): add test cases for v-for destructure with rest and default value X-Git-Tag: v3.6.0-alpha.1~16^2~116 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2b0731a43d98a9c1c6e68535a980092fa7a29421;p=thirdparty%2Fvuejs%2Fcore.git test(vapor): add test cases for v-for destructure with rest and default value --- diff --git a/packages/runtime-vapor/__tests__/for.spec.ts b/packages/runtime-vapor/__tests__/for.spec.ts index 13d1c1407a..28233cbc9f 100644 --- a/packages/runtime-vapor/__tests__/for.spec.ts +++ b/packages/runtime-vapor/__tests__/for.spec.ts @@ -1,4 +1,9 @@ -import { createFor, renderEffect } from '../src' +import { + createFor, + getDefaultValue, + getRestElement, + renderEffect, +} from '../src' import { nextTick, ref, shallowRef, triggerRef } from '@vue/runtime-dom' import { makeRender } from './_utils' @@ -257,6 +262,112 @@ describe('createFor', () => { expect(host.innerHTML).toBe('') }) + test('de-structured value (rest element)', async () => { + const list = ref([ + { name: '1', a: 1 }, + { name: '2', a: 2 }, + { name: '3', a: 3 }, + ]) + function reverse() { + list.value = list.value.reverse() + } + + const { host } = define(() => { + const n1 = createFor( + () => list.value, + state => { + const span = document.createElement('li') + renderEffect(() => { + span.innerHTML = JSON.stringify( + getRestElement(state[0].value, ['name']), + ) + // index should be undefined if source is not an object + expect(state[2].value).toBe(undefined) + }) + return span + }, + item => item.name, + ) + return n1 + }).render() + + expect(host.innerHTML).toBe( + '
  • {"a":1}
  • {"a":2}
  • {"a":3}
  • ', + ) + + // add + list.value.push({ name: '4', a: 4 }) + await nextTick() + expect(host.innerHTML).toBe( + '
  • {"a":1}
  • {"a":2}
  • {"a":3}
  • {"a":4}
  • ', + ) + + // move + reverse() + await nextTick() + expect(host.innerHTML).toBe( + '
  • {"a":4}
  • {"a":3}
  • {"a":2}
  • {"a":1}
  • ', + ) + + reverse() + await nextTick() + expect(host.innerHTML).toBe( + '
  • {"a":1}
  • {"a":2}
  • {"a":3}
  • {"a":4}
  • ', + ) + + // change + list.value[0].a = 5 + await nextTick() + expect(host.innerHTML).toBe( + '
  • {"a":5}
  • {"a":2}
  • {"a":3}
  • {"a":4}
  • ', + ) + + // remove + list.value.splice(1, 1) + await nextTick() + expect(host.innerHTML).toBe( + '
  • {"a":5}
  • {"a":3}
  • {"a":4}
  • ', + ) + + // clear + list.value = [] + await nextTick() + expect(host.innerHTML).toBe('') + }) + + test('de-structured value (default value)', async () => { + const list = ref([{ name: '1' }, { name: '2' }, { name: '3' }]) + + const { host } = define(() => { + const n1 = createFor( + () => list.value, + state => { + const span = document.createElement('li') + renderEffect(() => { + span.innerHTML = getDefaultValue(state[0].value.x, '0') + // index should be undefined if source is not an object + expect(state[2].value).toBe(undefined) + }) + return span + }, + item => item.name, + ) + return n1 + }).render() + + expect(host.innerHTML).toBe('
  • 0
  • 0
  • 0
  • ') + + // change + list.value[0].x = 5 + await nextTick() + expect(host.innerHTML).toBe('
  • 5
  • 0
  • 0
  • ') + + // clear + list.value = [] + await nextTick() + expect(host.innerHTML).toBe('') + }) + test('shallowRef source', async () => { const list = shallowRef([{ name: '1' }, { name: '2' }, { name: '3' }]) const setList = (update = list.value.slice()) => (list.value = update)