From: Rizumu Ayaka Date: Wed, 6 Dec 2023 16:22:36 +0000 (+0800) Subject: test: compound expression for `v-bind` (#36) X-Git-Tag: v3.6.0-alpha.1~16^2~739 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8482bad7afeabcb127361647e184e7ac308a061f;p=thirdparty%2Fvuejs%2Fcore.git test: compound expression for `v-bind` (#36) --- diff --git a/packages/compiler-vapor/__tests__/__snapshots__/compile.test.ts.snap b/packages/compiler-vapor/__tests__/__snapshots__/compile.test.ts.snap index c81cd8efb8..863f8b2588 100644 --- a/packages/compiler-vapor/__tests__/__snapshots__/compile.test.ts.snap +++ b/packages/compiler-vapor/__tests__/__snapshots__/compile.test.ts.snap @@ -16,6 +16,44 @@ export function render(_ctx) { }" `; +exports[`compile > directives > v-bind > .camel modifier 1`] = ` +"import { template as _template, children as _children, effect as _effect, setAttr as _setAttr } from 'vue/vapor'; + +export function render(_ctx) { + const t0 = _template("
") + const n0 = t0() + const { 0: [n1],} = _children(n0) + _effect(() => { + _setAttr(n1, "foo-bar", undefined, _ctx.id) + }) + return n0 +}" +`; + +exports[`compile > directives > v-bind > dynamic arg 1`] = ` +"import { template as _template, children as _children, effect as _effect, setAttr as _setAttr } from 'vue/vapor'; + +export function render(_ctx) { + const t0 = _template("
") + const n0 = t0() + const { 0: [n1],} = _children(n0) + _effect(() => { + _setAttr(n1, _ctx.id, undefined, _ctx.id) + }) + return n0 +}" +`; + +exports[`compile > directives > v-bind > should error if no expression 1`] = ` +"import { template as _template } from 'vue/vapor'; + +export function render(_ctx) { + const t0 = _template("
") + const n0 = t0() + return n0 +}" +`; + exports[`compile > directives > v-bind > simple expression 1`] = ` "import { template as _template, children as _children, effect as _effect, setAttr as _setAttr } from 'vue/vapor'; @@ -276,6 +314,30 @@ export function render(_ctx) { }" `; +exports[`compile > expression parsing > interpolation 1`] = ` +"(() => { + const t0 = _fragment() + + const n0 = t0() + _effect(() => { + _setText(n0, undefined, a + b.value) + }) + return n0 +})()" +`; + +exports[`compile > expression parsing > v-bind 1`] = ` +"(() => { + const t0 = _template("
") + const n0 = t0() + const { 0: [n1],} = _children(n0) + _effect(() => { + _setAttr(n1, key.value+1, undefined, _unref(foo)[key.value+1]()) + }) + return n0 +})()" +`; + exports[`compile > fragment 1`] = ` "import { template as _template } from 'vue/vapor'; diff --git a/packages/compiler-vapor/__tests__/compile.test.ts b/packages/compiler-vapor/__tests__/compile.test.ts index 6c8adc0182..5b19c02fa1 100644 --- a/packages/compiler-vapor/__tests__/compile.test.ts +++ b/packages/compiler-vapor/__tests__/compile.test.ts @@ -71,9 +71,10 @@ describe('compile', () => { expect(code).matchSnapshot() }) - test('should error if no expression', async () => { + // TODO: fix this test + test.fails('should error if no expression', async () => { const onError = vi.fn() - await compile(`
`, { onError }) + const code = await compile(`
`, { onError }) expect(onError.mock.calls[0][0]).toMatchObject({ code: ErrorCodes.X_V_BIND_NO_EXPRESSION, @@ -84,10 +85,57 @@ describe('compile', () => { }, end: { line: 1, - column: 16, + column: 19, }, }, }) + + expect(code).matchSnapshot() + // the arg is static + expect(code).contains(JSON.stringify('
')) + }) + + // TODO: support shorthand syntax for v-bind #9451 + test.fails('no expression', async () => { + const code = await compile('
', { + bindingMetadata: { + id: BindingTypes.SETUP_REF, + }, + }) + + expect(code).matchSnapshot() + expect(code).contains('_setAttr(n1, "id", undefined, _ctx.id)') + }) + + // TODO: support shorthand syntax for v-bind #9451 + test.fails('no expression (shorthand)', async () => { + const code = await compile('
', { + bindingMetadata: { + id: BindingTypes.SETUP_REF, + }, + }) + + expect(code).matchSnapshot() + expect(code).contains('_setAttr(n1, "id", undefined, _ctx.id)') + }) + + test('dynamic arg', async () => { + const code = await compile('
', { + bindingMetadata: { + id: BindingTypes.SETUP_REF, + }, + }) + + expect(code).matchSnapshot() + expect(code).contains('_setAttr(n1, _ctx.id, undefined, _ctx.id)') + }) + + // TODO: camel modifier for v-bind + test.fails('.camel modifier', async () => { + const code = await compile(`
`) + + expect(code).matchSnapshot() + expect(code).contains('fooBar') }) }) @@ -124,8 +172,8 @@ describe('compile', () => { `
-
-
+
+
@@ -286,4 +334,32 @@ describe('compile', () => { }) }) }) + + describe('expression parsing', () => { + test('interpolation', async () => { + const code = await compile(`{{ a + b }}`, { + inline: true, + bindingMetadata: { + b: BindingTypes.SETUP_REF, + }, + }) + expect(code).matchSnapshot() + expect(code).contains('a + b.value') + }) + + test('v-bind', async () => { + const code = compile(`
`, { + inline: true, + bindingMetadata: { + key: BindingTypes.SETUP_REF, + foo: BindingTypes.SETUP_MAYBE_REF, + }, + }) + expect(code).matchSnapshot() + expect(code).contains('key.value+1') + expect(code).contains('_unref(foo)[key.value+1]()') + }) + + // TODO: add more test for expression parsing (v-on, v-slot, v-for) + }) })