From: daiwei Date: Thu, 23 Jan 2025 09:26:11 +0000 (+0800) Subject: wip: ssr test cases X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=25b023d17a40f4dc135cdb90499cbaac862c06bc;p=thirdparty%2Fvuejs%2Fcore.git wip: ssr test cases --- diff --git a/packages/compiler-ssr/__tests__/ssrVSkip.spec.ts b/packages/compiler-ssr/__tests__/ssrVSkip.spec.ts new file mode 100644 index 0000000000..9bee16cc27 --- /dev/null +++ b/packages/compiler-ssr/__tests__/ssrVSkip.spec.ts @@ -0,0 +1,335 @@ +import { compile } from '@vue/compiler-ssr' + +describe('ssr: v-skip', () => { + test('basic', () => { + expect(compile(`
`).code).toMatchInlineSnapshot(` + "const { createCommentVNode: _createCommentVNode } = require("vue") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.foo) { + _createCommentVNode("v-skip", true) + } else { + _push(\`
\`) + } + }" + `) + }) + + test('with text children', () => { + expect(compile(`
hello
`).code) + .toMatchInlineSnapshot(` + " + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.foo) { + _push(\`hello\`) + } else { + _push(\`
hello
\`) + } + }" + `) + }) + + test('with element children', () => { + expect(compile(`
`).code) + .toMatchInlineSnapshot(` + " + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.foo) { + _push(\`\`) + } else { + _push(\`
\`) + } + }" + `) + }) + + test('with component children', () => { + expect(compile(`
`).code) + .toMatchInlineSnapshot(` + "const { resolveComponent: _resolveComponent } = require("vue") + const { ssrRenderComponent: _ssrRenderComponent } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + const _component_MyComponent = _resolveComponent("MyComponent") + + if (_ctx.foo) { + _push(_ssrRenderComponent(_component_MyComponent, null, null, _parent)) + } else { + _push(\`
\`) + _push(_ssrRenderComponent(_component_MyComponent, null, null, _parent)) + _push(\`
\`) + } + }" + `) + }) + + test('with multiple children', () => { + expect(compile(`
`).code) + .toMatchInlineSnapshot(` + " + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.foo) { + _push(\`\`) + } else { + _push(\`
\`) + } + }" + `) + }) + + test('nested v-skip', () => { + expect(compile(`
`).code) + .toMatchInlineSnapshot(` + "const { createCommentVNode: _createCommentVNode } = require("vue") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.foo) { + _push(\`\`) + if (_ctx.bar) { + _createCommentVNode("v-skip", true) + } else { + _push(\`
\`) + } + _push(\`\`) + } else { + _push(\`
\`) + if (_ctx.bar) { + _createCommentVNode("v-skip", true) + } else { + _push(\`
\`) + } + _push(\`
\`) + } + }" + `) + }) + + test('v-if + v-skip', () => { + expect(compile(`
`).code) + .toMatchInlineSnapshot(` + "const { createCommentVNode: _createCommentVNode } = require("vue") + const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.ok) { + _push(\`\`) + if (_ctx.foo) { + _createCommentVNode("v-skip", true) + } else { + _push(\`
\`) + } + _push(\`\`) + } else { + _push(\`\`) + } + }" + `) + }) + + test.todo('with key', () => { + expect(compile(`
`).code).toMatchInlineSnapshot(` + "const { createCommentVNode: _createCommentVNode } = require("vue") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.ok) { + _createCommentVNode("v-skip", true) + } else { + _push(\`
\`) + } + }" + `) + }) + + test('v-else + v-skip', () => { + expect(compile(`
`).code) + .toMatchInlineSnapshot(` + "const { createCommentVNode: _createCommentVNode } = require("vue") + const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.ok) { + _push(\`
\`) + } else { + _push(\`\`) + if (_ctx.nested) { + _createCommentVNode("v-skip", true) + } else { + _push(\`
\`) + } + _push(\`\`) + } + }" + `) + }) + + test('v-else-if + v-skip', () => { + expect( + compile(`
`).code, + ).toMatchInlineSnapshot(` + "const { createCommentVNode: _createCommentVNode } = require("vue") + const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.ok) { + _push(\`
\`) + } else if (_ctx.yes) { + _push(\`\`) + if (_ctx.nested) { + _createCommentVNode("v-skip", true) + } else { + _push(\`
\`) + } + _push(\`\`) + } else { + _push(\`\`) + } + }" + `) + }) + + test('on component', () => { + expect(compile(``).code).toMatchInlineSnapshot(` + "const { withCtx: _withCtx, createCommentVNode: _createCommentVNode, resolveComponent: _resolveComponent } = require("vue") + const { ssrRenderComponent: _ssrRenderComponent } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + const _component_Comp = _resolveComponent("Comp") + + if (_ctx.foo) { + _createCommentVNode("v-skip", true) + } else { + _push(_ssrRenderComponent(_component_Comp, null, null, _parent)) + } + }" + `) + }) + + test('on component with default slot', () => { + expect(compile(`foo`).code).toMatchInlineSnapshot(` + "const { withCtx: _withCtx, resolveComponent: _resolveComponent } = require("vue") + const { ssrRenderComponent: _ssrRenderComponent } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + const _component_Comp = _resolveComponent("Comp") + + if (_ctx.ok) { + _push(\`foo\`) + } else { + _push(_ssrRenderComponent(_component_Comp, null, { + default: _withCtx((_, _push, _parent, _scopeId) => { + if (_push) { + _push(\`foo\`) + } + }), + _: 1 /* STABLE */ + }, _parent)) + } + }" + `) + }) + + test('on component with multiple named slot', () => { + expect( + compile( + ` + + + `, + ).code, + ).toMatchInlineSnapshot(` + "const { withCtx: _withCtx, resolveComponent: _resolveComponent } = require("vue") + const { ssrRenderComponent: _ssrRenderComponent } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + const _component_Comp = _resolveComponent("Comp") + + if (_ctx.ok) { + _push(\`default\`) + } else { + _push(_ssrRenderComponent(_component_Comp, null, { + default: _withCtx((_, _push, _parent, _scopeId) => { + if (_push) { + _push(\`default\`) + } + }), + foo: _withCtx((_, _push, _parent, _scopeId) => { + if (_push) { + _push(\`foo\`) + } + }), + _: 1 /* STABLE */ + }, _parent)) + } + }" + `) + }) + + test('on component with multiple implicit slot', () => { + expect( + compile( + ` + + +
+ `, + ).code, + ).toMatchInlineSnapshot(` + "const { withCtx: _withCtx, resolveComponent: _resolveComponent } = require("vue") + const { ssrRenderComponent: _ssrRenderComponent } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + const _component_Comp = _resolveComponent("Comp") + + if (_ctx.ok) { + _push(\`
\`) + } else { + _push(_ssrRenderComponent(_component_Comp, null, { + foo: _withCtx((_, _push, _parent, _scopeId) => { + if (_push) { + _push(\`foo\`) + } + }), + default: _withCtx((_, _push, _parent, _scopeId) => { + if (_push) { + _push(\`
\`) + } + }), + _: 1 /* STABLE */ + }, _parent)) + } + }" + `) + }) + + test('on dynamic component', () => { + expect( + compile( + ` + + `, + ).code, + ).toMatchInlineSnapshot(` + "const { withCtx: _withCtx, resolveDynamicComponent: _resolveDynamicComponent, createVNode: _createVNode } = require("vue") + const { ssrRenderSlot: _ssrRenderSlot, ssrRenderVNode: _ssrRenderVNode } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.ok) { + _ssrRenderSlot(_ctx.$slots, "default", {}, null, _push, _parent, _scopeId) + } else { + _ssrRenderVNode(_push, _createVNode(_resolveDynamicComponent(_ctx.Comp), null, { + default: _withCtx((_, _push, _parent, _scopeId) => { + if (_push) { + _ssrRenderSlot(_ctx.$slots, "default", {}, null, _push, _parent, _scopeId) + } + }), + _: 3 /* FORWARDED */ + }), _parent) + } + }" + `) + }) +})