| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- import { createApp, defineAsyncComponent, h } from 'vue'
- import { renderToString } from '../src/renderToString'
- const components = {
- one: {
- template: `<div><slot/></div>`,
- },
- }
- describe('ssr: slot', () => {
- test('text slot', async () => {
- expect(
- await renderToString(
- createApp({
- components,
- template: `<one>hello</one>`,
- }),
- ),
- ).toBe(`<div><!--[-->hello<!--]--></div>`)
- })
- test('element slot', async () => {
- expect(
- await renderToString(
- createApp({
- components,
- template: `<one><div>hi</div></one>`,
- }),
- ),
- ).toBe(`<div><!--[--><div>hi</div><!--]--></div>`)
- })
- test('empty slot', async () => {
- expect(
- await renderToString(
- createApp({
- components: {
- one: {
- template: `<div><slot/></div>`,
- },
- },
- template: `<one><template v-if="false"/></one>`,
- }),
- ),
- ).toBe(`<div><!--[--><!--]--></div>`)
- })
- test('empty slot (manual comments)', async () => {
- expect(
- await renderToString(
- createApp({
- components: {
- one: {
- template: `<div><slot/></div>`,
- },
- },
- template: `<one><!--hello--></one>`,
- }),
- ),
- ).toBe(`<div><!--[--><!--]--></div>`)
- })
- test('empty slot (multi-line comments)', async () => {
- expect(
- await renderToString(
- createApp({
- components: {
- one: {
- template: `<div><slot/></div>`,
- },
- },
- template: `<one><!--he\nllo--></one>`,
- }),
- ),
- ).toBe(`<div><!--[--><!--]--></div>`)
- })
- test('multiple elements', async () => {
- expect(
- await renderToString(
- createApp({
- components,
- template: `<one><div>one</div><div>two</div></one>`,
- }),
- ),
- ).toBe(`<div><!--[--><div>one</div><div>two</div><!--]--></div>`)
- })
- test('fragment slot (template v-if)', async () => {
- expect(
- await renderToString(
- createApp({
- components,
- template: `<one><template v-if="true">hello</template></one>`,
- }),
- ),
- ).toBe(`<div><!--[--><!--[-->hello<!--]--><!--]--></div>`)
- })
- test('fragment slot (template v-if + multiple elements)', async () => {
- expect(
- await renderToString(
- createApp({
- components,
- template: `<one><template v-if="true"><div>one</div><div>two</div></template></one>`,
- }),
- ),
- ).toBe(
- `<div><!--[--><!--[--><div>one</div><div>two</div><!--]--><!--]--></div>`,
- )
- })
- test('transition slot', async () => {
- const ReusableTransition = {
- template: `<transition><slot/></transition>`,
- }
- const ReusableTransitionWithAppear = {
- template: `<transition appear><slot/></transition>`,
- }
- expect(
- await renderToString(
- createApp({
- components: {
- one: ReusableTransition,
- },
- template: `<one><div v-if="false">foo</div></one>`,
- }),
- ),
- ).toBe(`<!---->`)
- expect(await renderToString(createApp(ReusableTransition))).toBe(`<!---->`)
- expect(await renderToString(createApp(ReusableTransitionWithAppear))).toBe(
- `<template><!----></template>`,
- )
- expect(
- await renderToString(
- createApp({
- components: {
- one: ReusableTransition,
- },
- template: `<one><slot/></one>`,
- }),
- ),
- ).toBe(`<!---->`)
- expect(
- await renderToString(
- createApp({
- components: {
- one: ReusableTransitionWithAppear,
- },
- template: `<one><slot/></one>`,
- }),
- ),
- ).toBe(`<template><!----></template>`)
- expect(
- await renderToString(
- createApp({
- render() {
- return h(ReusableTransition, null, {
- default: () => null,
- })
- },
- }),
- ),
- ).toBe(`<!---->`)
- expect(
- await renderToString(
- createApp({
- render() {
- return h(ReusableTransitionWithAppear, null, {
- default: () => null,
- })
- },
- }),
- ),
- ).toBe(`<template><!----></template>`)
- expect(
- await renderToString(
- createApp({
- render() {
- return h(ReusableTransitionWithAppear, null, {
- default: () => [],
- })
- },
- }),
- ),
- ).toBe(`<template><!----></template>`)
- expect(
- await renderToString(
- createApp({
- render() {
- return h(ReusableTransition, null, {
- default: () => [],
- })
- },
- }),
- ),
- ).toBe(`<!---->`)
- expect(
- await renderToString(
- createApp({
- components: {
- one: ReusableTransition,
- },
- template: `<one><div v-if="true">foo</div></one>`,
- }),
- ),
- ).toBe(`<div>foo</div>`)
- })
- // #9933
- test('transition-group slot', async () => {
- expect(
- await renderToString(
- createApp({
- components: {
- one: {
- template: `<TransitionGroup tag="div"><slot/></TransitionGroup>`,
- },
- },
- template: `<one><p v-for="i in 2">{{i}}</p></one>`,
- }),
- ),
- ).toBe(`<div><p>1</p><p>2</p></div>`)
- })
- // #12438
- test('async component slot with v-if true', async () => {
- const Layout = defineAsyncComponent(() =>
- Promise.resolve({
- template: `<div><slot name="header">default header</slot></div>`,
- }),
- )
- const LayoutLoader = {
- setup(_: any, context: any) {
- return () => h(Layout, {}, context.slots)
- },
- }
- expect(
- await renderToString(
- createApp({
- components: {
- LayoutLoader,
- },
- template: `
- <Suspense>
- <LayoutLoader>
- <template v-if="true" #header>
- new header
- </template>
- </LayoutLoader>
- </Suspense>
- `,
- }),
- ),
- ).toBe(`<div><!--[--> new header <!--]--></div>`)
- })
- // #11326
- test('dynamic component slot', async () => {
- expect(
- await renderToString(
- createApp({
- components: {
- ButtonComp: {
- template: `<component is="button"><slot/></component>`,
- },
- Wrap: {
- template: `<div><slot/></div>`,
- },
- },
- template: `<ButtonComp><Wrap><div v-if="false">hello</div></Wrap></ButtonComp>`,
- }),
- ),
- ).toBe(`<button><!--[--><div><!--[--><!--]--></div><!--]--></button>`)
- expect(
- await renderToString(
- createApp({
- components: {
- ButtonComp: {
- template: `<component is="button"><slot/></component>`,
- },
- Wrap: {
- template: `<div><slot/></div>`,
- },
- },
- template: `<ButtonComp><Wrap><div v-if="true">hello</div></Wrap></ButtonComp>`,
- }),
- ),
- ).toBe(
- `<button><!--[--><div><!--[--><div>hello</div><!--]--></div><!--]--></button>`,
- )
- expect(
- await renderToString(
- createApp({
- components: {
- ButtonComp: {
- template: `<component is="button"><slot/></component>`,
- },
- },
- template: `<ButtonComp><template v-if="false">hello</template></ButtonComp>`,
- }),
- ),
- ).toBe(`<button><!--[--><!--]--></button>`)
- })
- })
|