|
1 | 1 | import { getCompiledString } from './utils'
|
2 | 2 | import { compile } from '../src'
|
| 3 | +import { renderToString } from '@vue/server-renderer' |
| 4 | +import { createApp } from '@vue/runtime-dom' |
3 | 5 |
|
4 | 6 | describe('ssr: element', () => {
|
5 | 7 | test('basic elements', () => {
|
@@ -71,6 +73,160 @@ describe('ssr: element', () => {
|
71 | 73 | `)
|
72 | 74 | })
|
73 | 75 |
|
| 76 | + test('<select> with dynamic value assigns `selected` option attribute', async () => { |
| 77 | + expect( |
| 78 | + getCompiledString( |
| 79 | + `<select :value="selectValue"><option value="1"></option></select>`, |
| 80 | + ), |
| 81 | + ).toMatchInlineSnapshot(` |
| 82 | + "\`<select><option value="1"\${ |
| 83 | + (_ssrIncludeBooleanAttr((Array.isArray(_ctx.selectValue)) |
| 84 | + ? _ssrLooseContain(_ctx.selectValue, "1") |
| 85 | + : _ssrLooseEqual(_ctx.selectValue, "1"))) ? " selected" : "" |
| 86 | + }></option></select>\`" |
| 87 | + `) |
| 88 | + |
| 89 | + expect( |
| 90 | + await renderToString( |
| 91 | + createApp({ |
| 92 | + data: () => ({ selected: 2 }), |
| 93 | + template: `<div><select :value="selected"><option value="1">1</option><option value="2">2</option></select></div>`, |
| 94 | + }), |
| 95 | + ), |
| 96 | + ).toMatchInlineSnapshot( |
| 97 | + `"<div><select><option value="1">1</option><option value="2" selected>2</option></select></div>"`, |
| 98 | + ) |
| 99 | + }) |
| 100 | + |
| 101 | + test('<select> with static value assigns `selected` option attribute', async () => { |
| 102 | + expect( |
| 103 | + getCompiledString( |
| 104 | + `<select value="selectValue"><option value="1"></option></select>`, |
| 105 | + ), |
| 106 | + ).toMatchInlineSnapshot(` |
| 107 | + "\`<select><option value="1"\${ |
| 108 | + (_ssrIncludeBooleanAttr(_ssrLooseEqual("selectValue", "1"))) ? " selected" : "" |
| 109 | + }></option></select>\`" |
| 110 | + `) |
| 111 | + |
| 112 | + expect( |
| 113 | + await renderToString( |
| 114 | + createApp({ |
| 115 | + template: `<div><select value="2"><option value="1">1</option><option value="2">2</option></select></div>`, |
| 116 | + }), |
| 117 | + ), |
| 118 | + ).toMatchInlineSnapshot( |
| 119 | + `"<div><select><option value="1">1</option><option value="2" selected>2</option></select></div>"`, |
| 120 | + ) |
| 121 | + }) |
| 122 | + |
| 123 | + test('<select> with dynamic v-bind assigns `selected` option attribute', async () => { |
| 124 | + expect( |
| 125 | + compile(`<select v-bind="obj"><option value="1"></option></select>`) |
| 126 | + .code, |
| 127 | + ).toMatchInlineSnapshot(` |
| 128 | + "const { mergeProps: _mergeProps } = require("vue") |
| 129 | + const { ssrRenderAttrs: _ssrRenderAttrs, ssrIncludeBooleanAttr: _ssrIncludeBooleanAttr, ssrLooseContain: _ssrLooseContain, ssrLooseEqual: _ssrLooseEqual } = require("vue/server-renderer") |
| 130 | +
|
| 131 | + return function ssrRender(_ctx, _push, _parent, _attrs) { |
| 132 | + let _temp0 |
| 133 | +
|
| 134 | + _push(\`<select\${ |
| 135 | + _ssrRenderAttrs(_temp0 = _mergeProps(_ctx.obj, _attrs)) |
| 136 | + }><option value="1"\${ |
| 137 | + (_ssrIncludeBooleanAttr(("value" in _temp0) |
| 138 | + ? (Array.isArray(_temp0.value)) |
| 139 | + ? _ssrLooseContain(_temp0.value, "1") |
| 140 | + : _ssrLooseEqual(_temp0.value, "1") |
| 141 | + : false)) ? " selected" : "" |
| 142 | + }></option></select>\`) |
| 143 | + }" |
| 144 | + `) |
| 145 | + |
| 146 | + expect( |
| 147 | + await renderToString( |
| 148 | + createApp({ |
| 149 | + data: () => ({ obj: { value: 2 } }), |
| 150 | + template: `<div><select v-bind="obj"><option value="1">1</option><option value="2">2</option></select></div>`, |
| 151 | + }), |
| 152 | + ), |
| 153 | + ).toMatchInlineSnapshot( |
| 154 | + `"<div><select value="2"><option value="1">1</option><option value="2" selected>2</option></select></div>"`, |
| 155 | + ) |
| 156 | + }) |
| 157 | + |
| 158 | + test('<select> with dynamic v-bind and dynamic value bind assigns `selected` option attribute', async () => { |
| 159 | + expect( |
| 160 | + compile( |
| 161 | + `<select v-bind="obj" :value="selectValue"><option value="1"></option></select>`, |
| 162 | + ).code, |
| 163 | + ).toMatchInlineSnapshot(` |
| 164 | + "const { mergeProps: _mergeProps } = require("vue") |
| 165 | + const { ssrRenderAttrs: _ssrRenderAttrs, ssrIncludeBooleanAttr: _ssrIncludeBooleanAttr, ssrLooseContain: _ssrLooseContain, ssrLooseEqual: _ssrLooseEqual } = require("vue/server-renderer") |
| 166 | +
|
| 167 | + return function ssrRender(_ctx, _push, _parent, _attrs) { |
| 168 | + let _temp0 |
| 169 | +
|
| 170 | + _push(\`<select\${ |
| 171 | + _ssrRenderAttrs(_temp0 = _mergeProps(_ctx.obj, { value: _ctx.selectValue }, _attrs)) |
| 172 | + }><option value="1"\${ |
| 173 | + (_ssrIncludeBooleanAttr(("value" in _temp0) |
| 174 | + ? (Array.isArray(_temp0.value)) |
| 175 | + ? _ssrLooseContain(_temp0.value, "1") |
| 176 | + : _ssrLooseEqual(_temp0.value, "1") |
| 177 | + : false)) ? " selected" : "" |
| 178 | + }></option></select>\`) |
| 179 | + }" |
| 180 | + `) |
| 181 | + |
| 182 | + expect( |
| 183 | + await renderToString( |
| 184 | + createApp({ |
| 185 | + data: () => ({ obj: { value: 1 } }), |
| 186 | + template: `<div><select v-bind="obj" :value="2"><option value="1">1</option><option value="2">2</option></select></div>`, |
| 187 | + }), |
| 188 | + ), |
| 189 | + ).toMatchInlineSnapshot( |
| 190 | + `"<div><select value="2"><option value="1">1</option><option value="2" selected>2</option></select></div>"`, |
| 191 | + ) |
| 192 | + }) |
| 193 | + |
| 194 | + test('<select> with dynamic v-bind and static value bind assigns `selected` option attribute', async () => { |
| 195 | + expect( |
| 196 | + compile( |
| 197 | + `<select v-bind="obj" value="selectValue"><option value="1"></option></select>`, |
| 198 | + ).code, |
| 199 | + ).toMatchInlineSnapshot(` |
| 200 | + "const { mergeProps: _mergeProps } = require("vue") |
| 201 | + const { ssrRenderAttrs: _ssrRenderAttrs, ssrIncludeBooleanAttr: _ssrIncludeBooleanAttr, ssrLooseContain: _ssrLooseContain, ssrLooseEqual: _ssrLooseEqual } = require("vue/server-renderer") |
| 202 | +
|
| 203 | + return function ssrRender(_ctx, _push, _parent, _attrs) { |
| 204 | + let _temp0 |
| 205 | +
|
| 206 | + _push(\`<select\${ |
| 207 | + _ssrRenderAttrs(_temp0 = _mergeProps(_ctx.obj, { value: "selectValue" }, _attrs)) |
| 208 | + }><option value="1"\${ |
| 209 | + (_ssrIncludeBooleanAttr(("value" in _temp0) |
| 210 | + ? (Array.isArray(_temp0.value)) |
| 211 | + ? _ssrLooseContain(_temp0.value, "1") |
| 212 | + : _ssrLooseEqual(_temp0.value, "1") |
| 213 | + : false)) ? " selected" : "" |
| 214 | + }></option></select>\`) |
| 215 | + }" |
| 216 | + `) |
| 217 | + |
| 218 | + expect( |
| 219 | + await renderToString( |
| 220 | + createApp({ |
| 221 | + data: () => ({ obj: { value: 1 } }), |
| 222 | + template: `<div><select v-bind="obj" value="2"><option value="1">1</option><option value="2">2</option></select></div>`, |
| 223 | + }), |
| 224 | + ), |
| 225 | + ).toMatchInlineSnapshot( |
| 226 | + `"<div><select value="2"><option value="1">1</option><option value="2" selected>2</option></select></div>"`, |
| 227 | + ) |
| 228 | + }) |
| 229 | + |
74 | 230 | test('multiple _ssrInterpolate at parent and child import dependency once', () => {
|
75 | 231 | expect(
|
76 | 232 | compile(`<div>{{ hello }}<textarea v-bind="a"></textarea></div>`).code,
|
|
0 commit comments