|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { createApp } from 'vue' |
| 6 | +import { renderToString } from '../src/renderToString' |
| 7 | + |
| 8 | +describe('ssr: compiler options', () => { |
| 9 | + test('config.isCustomElement (deprecated)', async () => { |
| 10 | + const app = createApp({ |
| 11 | + template: `<div><x-button/></div>` |
| 12 | + }) |
| 13 | + app.config.isCustomElement = tag => tag.startsWith('x-') |
| 14 | + expect(await renderToString(app)).toBe(`<div><x-button></x-button></div>`) |
| 15 | + }) |
| 16 | + |
| 17 | + test('config.compilerOptions.isCustomElement', async () => { |
| 18 | + const app = createApp({ |
| 19 | + template: `<div><x-panel/></div>` |
| 20 | + }) |
| 21 | + app.config.compilerOptions.isCustomElement = tag => tag.startsWith('x-') |
| 22 | + expect(await renderToString(app)).toBe(`<div><x-panel></x-panel></div>`) |
| 23 | + }) |
| 24 | + |
| 25 | + test('component.compilerOptions.isCustomElement', async () => { |
| 26 | + const app = createApp({ |
| 27 | + template: `<div><x-card/><y-child/></div>`, |
| 28 | + compilerOptions: { |
| 29 | + isCustomElement: (tag: string) => tag.startsWith('x-') |
| 30 | + }, |
| 31 | + components: { |
| 32 | + YChild: { |
| 33 | + template: `<div><y-button/></div>` |
| 34 | + } |
| 35 | + } |
| 36 | + }) |
| 37 | + app.config.compilerOptions.isCustomElement = tag => tag.startsWith('y-') |
| 38 | + expect(await renderToString(app)).toBe( |
| 39 | + `<div><x-card></x-card><div><y-button></y-button></div></div>` |
| 40 | + ) |
| 41 | + }) |
| 42 | + |
| 43 | + test('component.delimiters (deprecated)', async () => { |
| 44 | + const app = createApp({ |
| 45 | + template: `<div>[[ 1 + 1 ]]</div>`, |
| 46 | + delimiters: ['[[', ']]'] |
| 47 | + }) |
| 48 | + expect(await renderToString(app)).toBe(`<div>2</div>`) |
| 49 | + }) |
| 50 | + |
| 51 | + test('config.compilerOptions.delimiters', async () => { |
| 52 | + const app = createApp({ |
| 53 | + template: `<div>[( 1 + 1 )]</div>` |
| 54 | + }) |
| 55 | + app.config.compilerOptions.delimiters = ['[(', ')]'] |
| 56 | + expect(await renderToString(app)).toBe(`<div>2</div>`) |
| 57 | + }) |
| 58 | + |
| 59 | + test('component.compilerOptions.delimiters', async () => { |
| 60 | + const app = createApp({ |
| 61 | + template: `<div>[[ 1 + 1 ]]<ChildComponent/></div>`, |
| 62 | + compilerOptions: { |
| 63 | + delimiters: ['[[', ']]'] |
| 64 | + }, |
| 65 | + components: { |
| 66 | + ChildComponent: { |
| 67 | + template: `<div>(( 2 + 2 ))</div>` |
| 68 | + } |
| 69 | + } |
| 70 | + }) |
| 71 | + app.config.compilerOptions.delimiters = ['((', '))'] |
| 72 | + expect(await renderToString(app)).toBe(`<div>2<div>4</div></div>`) |
| 73 | + }) |
| 74 | + |
| 75 | + test('compilerOptions.whitespace', async () => { |
| 76 | + const app = createApp({ |
| 77 | + template: `<div><span>Hello world</span><ChildComponent/></div>`, |
| 78 | + compilerOptions: { |
| 79 | + whitespace: 'condense' |
| 80 | + }, |
| 81 | + components: { |
| 82 | + ChildComponent: { |
| 83 | + template: `<span>Hello world</span>` |
| 84 | + } |
| 85 | + } |
| 86 | + }) |
| 87 | + app.config.compilerOptions.whitespace = 'preserve' |
| 88 | + expect(await renderToString(app)).toBe( |
| 89 | + `<div><span>Hello world</span><span>Hello world</span></div>` |
| 90 | + ) |
| 91 | + }) |
| 92 | + |
| 93 | + test('caching with compilerOptions', async () => { |
| 94 | + const template = `<div>{{1 + 1}} [[1 + 1]]</div>` |
| 95 | + |
| 96 | + const app = createApp({ |
| 97 | + template: `<div><ChildOne/><ChildTwo/><ChildThree/></div>`, |
| 98 | + components: { |
| 99 | + ChildOne: { |
| 100 | + template |
| 101 | + }, |
| 102 | + ChildTwo: { |
| 103 | + template, |
| 104 | + compilerOptions: { |
| 105 | + whitespace: 'preserve' |
| 106 | + } |
| 107 | + }, |
| 108 | + ChildThree: { |
| 109 | + template, |
| 110 | + compilerOptions: { |
| 111 | + delimiters: ['[[', ']]'] |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + }) |
| 116 | + expect(await renderToString(app)).toBe( |
| 117 | + `<div><div>2 [[1 + 1]]</div><div>2 [[1 + 1]]</div><div>{{1 + 1}} 2</div></div>` |
| 118 | + ) |
| 119 | + }) |
| 120 | + |
| 121 | + test('caching with isCustomElement', async () => { |
| 122 | + const template = `<div><MyChild/></div>` |
| 123 | + |
| 124 | + const app = createApp({ |
| 125 | + template, |
| 126 | + // No compilerOptions on the root |
| 127 | + components: { |
| 128 | + MyChild: { |
| 129 | + template, |
| 130 | + compilerOptions: { |
| 131 | + isCustomElement: tag => tag.startsWith('x-') |
| 132 | + }, |
| 133 | + components: { |
| 134 | + MyChild: { |
| 135 | + template, |
| 136 | + compilerOptions: { |
| 137 | + isCustomElement: tag => tag.startsWith('My') |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + }) |
| 144 | + expect(await renderToString(app)).toBe( |
| 145 | + `<div><div><div><MyChild></MyChild></div></div></div>` |
| 146 | + ) |
| 147 | + }) |
| 148 | +}) |
0 commit comments