/** * @jest-environment node */ import { renderToString } from '../src/renderToString' import { createApp, h, withDirectives, vShow, vModelText, vModelRadio, vModelCheckbox } from 'vue' 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(``) }) }) test('custom directive w/ getSSRProps', async () => { expect( await renderToString( createApp({ render() { return withDirectives(h('div'), [ [ { getSSRProps({ value }) { return { id: value } } }, 'foo' ] ]) } }) ) ).toBe(`
`) }) })