/** * @jest-environment node */ import { renderToString } from '../src/renderToString' import { createApp, h, withDirectives, vShow, vModelText, vModelRadio, vModelCheckbox, vModelDynamic, resolveDirective } from 'vue' import { ssrGetDirectiveProps, ssrRenderAttrs } from '../src' describe('ssr: directives', () => { describe('template v-show', () => { test('basic', async () => { expect( await renderToString( createApp({ template: `
` }) ) ).toBe(`
`) expect( await renderToString( createApp({ template: `
` }) ) ).toBe(`
`) }) test('with static style', async () => { expect( await renderToString( createApp({ template: `
` }) ) ).toBe(`
`) }) test('with dynamic style', async () => { expect( await renderToString( createApp({ data: () => ({ style: { color: 'red' } }), template: `
` }) ) ).toBe(`
`) }) test('with static + dynamic style', async () => { expect( await renderToString( createApp({ data: () => ({ style: { color: 'red' } }), template: `
` }) ) ).toBe(`
`) }) }) describe('template v-model', () => { test('text', async () => { expect( await renderToString( createApp({ data: () => ({ text: 'hello' }), template: `` }) ) ).toBe(``) }) test('radio', async () => { expect( await renderToString( createApp({ data: () => ({ selected: 'foo' }), template: `` }) ) ).toBe(``) expect( await renderToString( createApp({ data: () => ({ selected: 'foo' }), template: `` }) ) ).toBe(``) // non-string values expect( await renderToString( createApp({ data: () => ({ selected: 'foo' }), template: `` }) ) ).toBe(``) }) test('checkbox', async () => { expect( await renderToString( createApp({ data: () => ({ checked: true }), template: `` }) ) ).toBe(``) expect( await renderToString( createApp({ data: () => ({ checked: false }), template: `` }) ) ).toBe(``) expect( await renderToString( createApp({ data: () => ({ checked: ['foo'] }), template: `` }) ) ).toBe(``) expect( await renderToString( createApp({ data: () => ({ checked: [] }), template: `` }) ) ).toBe(``) }) test('textarea', async () => { expect( await renderToString( createApp({ data: () => ({ foo: 'hello' }), template: ``) }) test('dynamic type', async () => { expect( await renderToString( createApp({ data: () => ({ type: 'text', model: 'hello' }), template: `` }) ) ).toBe(``) expect( await renderToString( createApp({ data: () => ({ type: 'checkbox', model: true }), template: `` }) ) ).toBe(``) expect( await renderToString( createApp({ data: () => ({ type: 'checkbox', model: false }), template: `` }) ) ).toBe(``) expect( await renderToString( createApp({ data: () => ({ type: 'checkbox', model: ['hello'] }), template: `` }) ) ).toBe(``) expect( await renderToString( createApp({ data: () => ({ type: 'checkbox', model: [] }), template: `` }) ) ).toBe(``) expect( await renderToString( createApp({ data: () => ({ type: 'radio', model: 'hello' }), template: `` }) ) ).toBe(``) expect( await renderToString( createApp({ data: () => ({ type: 'radio', model: 'hello' }), template: `` }) ) ).toBe(``) }) test('with v-bind', async () => { expect( await renderToString( createApp({ data: () => ({ obj: { type: 'radio', value: 'hello' }, model: 'hello' }), template: `` }) ) ).toBe(``) }) }) describe('vnode v-show', () => { test('basic', async () => { expect( await renderToString( createApp({ render() { return withDirectives(h('div'), [[vShow, true]]) } }) ) ).toBe(`
`) expect( await renderToString( createApp({ render() { return withDirectives(h('div'), [[vShow, false]]) } }) ) ).toBe(`
`) }) test('with merge', async () => { expect( await renderToString( createApp({ render() { return withDirectives( h('div', { style: { color: 'red' } }), [[vShow, false]] ) } }) ) ).toBe(`
`) }) }) describe('vnode v-model', () => { test('text', async () => { expect( await renderToString( createApp({ render() { return withDirectives(h('input'), [[vModelText, 'hello']]) } }) ) ).toBe(``) }) test('radio', async () => { expect( await renderToString( createApp({ render() { return withDirectives( h('input', { type: 'radio', value: 'hello' }), [[vModelRadio, 'hello']] ) } }) ) ).toBe(``) expect( await renderToString( createApp({ render() { return withDirectives( h('input', { type: 'radio', value: 'hello' }), [[vModelRadio, 'foo']] ) } }) ) ).toBe(``) }) test('checkbox', async () => { expect( await renderToString( createApp({ render() { return withDirectives(h('input', { type: 'checkbox' }), [ [vModelCheckbox, true] ]) } }) ) ).toBe(``) expect( await renderToString( createApp({ render() { return withDirectives(h('input', { type: 'checkbox' }), [ [vModelCheckbox, false] ]) } }) ) ).toBe(``) expect( await renderToString( createApp({ render() { return withDirectives( h('input', { type: 'checkbox', value: 'foo' }), [[vModelCheckbox, ['foo']]] ) } }) ) ).toBe(``) expect( await renderToString( createApp({ render() { return withDirectives( h('input', { type: 'checkbox', value: 'foo' }), [[vModelCheckbox, []]] ) } }) ) ).toBe(``) }) }) describe('vnode v-model dynamic', () => { test('text', async () => { expect( await renderToString( createApp({ render() { return withDirectives(h('input'), [[vModelDynamic, 'hello']]) } }) ) ).toBe(``) }) test('radio', async () => { expect( await renderToString( createApp({ render() { return withDirectives( h('input', { type: 'radio', value: 'hello' }), [[vModelDynamic, 'hello']] ) } }) ) ).toBe(``) expect( await renderToString( createApp({ render() { return withDirectives( h('input', { type: 'radio', value: 'hello' }), [[vModelDynamic, 'foo']] ) } }) ) ).toBe(``) }) test('checkbox', async () => { expect( await renderToString( createApp({ render() { return withDirectives(h('input', { type: 'checkbox' }), [ [vModelDynamic, true] ]) } }) ) ).toBe(``) expect( await renderToString( createApp({ render() { return withDirectives(h('input', { type: 'checkbox' }), [ [vModelDynamic, false] ]) } }) ) ).toBe(``) expect( await renderToString( createApp({ render() { return withDirectives( h('input', { type: 'checkbox', value: 'foo' }), [[vModelDynamic, ['foo']]] ) } }) ) ).toBe(``) expect( await renderToString( createApp({ render() { return withDirectives( h('input', { type: 'checkbox', value: 'foo' }), [[vModelDynamic, []]] ) } }) ) ).toBe(``) }) }) test('custom directive w/ getSSRProps (vdom)', async () => { expect( await renderToString( createApp({ render() { return withDirectives(h('div'), [ [ { getSSRProps({ value }) { return { id: value } } }, 'foo' ] ]) } }) ) ).toBe(`
`) }) test('custom directive w/ getSSRProps (optimized)', async () => { expect( await renderToString( createApp({ data() { return { x: 'foo' } }, directives: { xxx: { getSSRProps({ value, arg, modifiers }) { return { id: [value, arg, modifiers.ok].join('-') } } } }, ssrRender(_ctx, _push, _parent, _attrs) { const _directive_xxx = resolveDirective('xxx')! _push( `
` ) } }) ) ).toBe(`
`) }) })