2020-02-04 07:16:09 +08:00
|
|
|
import {
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs,
|
|
|
|
ssrRenderClass,
|
|
|
|
ssrRenderStyle,
|
|
|
|
ssrRenderAttr
|
|
|
|
} from '../src/helpers/ssrRenderAttrs'
|
2020-02-05 07:42:13 +08:00
|
|
|
import { escapeHtml } from '@vue/shared'
|
2020-01-28 06:23:42 +08:00
|
|
|
|
2020-02-05 07:42:13 +08:00
|
|
|
describe('ssr: renderAttrs', () => {
|
2020-01-30 04:10:45 +08:00
|
|
|
test('ignore reserved props', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs({
|
2020-01-30 04:10:45 +08:00
|
|
|
key: 1,
|
|
|
|
ref: () => {},
|
|
|
|
onClick: () => {}
|
|
|
|
})
|
|
|
|
).toBe('')
|
2020-01-28 06:23:42 +08:00
|
|
|
})
|
|
|
|
|
2020-01-30 04:10:45 +08:00
|
|
|
test('normal attrs', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs({
|
2020-01-30 04:10:45 +08:00
|
|
|
id: 'foo',
|
|
|
|
title: 'bar'
|
|
|
|
})
|
|
|
|
).toBe(` id="foo" title="bar"`)
|
|
|
|
})
|
|
|
|
|
2020-02-21 21:44:13 +08:00
|
|
|
test('empty value attrs', () => {
|
|
|
|
expect(
|
|
|
|
ssrRenderAttrs({
|
|
|
|
'data-v-abc': ''
|
|
|
|
})
|
|
|
|
).toBe(` data-v-abc`)
|
|
|
|
})
|
|
|
|
|
2020-01-30 04:10:45 +08:00
|
|
|
test('escape attrs', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs({
|
2020-01-30 04:10:45 +08:00
|
|
|
id: '"><script'
|
|
|
|
})
|
|
|
|
).toBe(` id=""><script"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('boolean attrs', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs({
|
2020-01-30 04:10:45 +08:00
|
|
|
checked: true,
|
|
|
|
multiple: false
|
|
|
|
})
|
|
|
|
).toBe(` checked`) // boolean attr w/ false should be ignored
|
|
|
|
})
|
|
|
|
|
|
|
|
test('ignore falsy values', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs({
|
2020-01-30 04:10:45 +08:00
|
|
|
foo: false,
|
|
|
|
title: null,
|
|
|
|
baz: undefined
|
|
|
|
})
|
|
|
|
).toBe(` foo="false"`) // non boolean should render `false` as is
|
|
|
|
})
|
|
|
|
|
2020-02-06 04:21:20 +08:00
|
|
|
test('ingore non-renderable values', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs({
|
2020-02-06 04:21:20 +08:00
|
|
|
foo: {},
|
|
|
|
bar: [],
|
|
|
|
baz: () => {}
|
|
|
|
})
|
|
|
|
).toBe(``)
|
|
|
|
})
|
|
|
|
|
2020-01-30 04:10:45 +08:00
|
|
|
test('props to attrs', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs({
|
2020-01-30 04:10:45 +08:00
|
|
|
readOnly: true, // simple lower case conversion
|
|
|
|
htmlFor: 'foobar' // special cases
|
|
|
|
})
|
|
|
|
).toBe(` readonly for="foobar"`)
|
|
|
|
})
|
2020-01-30 06:36:06 +08:00
|
|
|
|
|
|
|
test('preserve name on custom element', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs(
|
2020-01-30 06:36:06 +08:00
|
|
|
{
|
|
|
|
fooBar: 'ok'
|
|
|
|
},
|
|
|
|
'my-el'
|
|
|
|
)
|
|
|
|
).toBe(` fooBar="ok"`)
|
|
|
|
})
|
2020-01-30 04:10:45 +08:00
|
|
|
})
|
2020-01-28 06:23:42 +08:00
|
|
|
|
2020-02-05 07:42:13 +08:00
|
|
|
describe('ssr: renderAttr', () => {
|
|
|
|
test('basic', () => {
|
2020-02-07 01:07:25 +08:00
|
|
|
expect(ssrRenderAttr('foo', 'bar')).toBe(` foo="bar"`)
|
2020-02-05 07:42:13 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('null and undefined', () => {
|
2020-02-07 01:07:25 +08:00
|
|
|
expect(ssrRenderAttr('foo', null)).toBe(``)
|
|
|
|
expect(ssrRenderAttr('foo', undefined)).toBe(``)
|
2020-02-05 07:42:13 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('escape', () => {
|
2020-02-07 01:07:25 +08:00
|
|
|
expect(ssrRenderAttr('foo', '<script>')).toBe(
|
2020-02-05 07:42:13 +08:00
|
|
|
` foo="${escapeHtml(`<script>`)}"`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-01-30 04:10:45 +08:00
|
|
|
describe('ssr: renderClass', () => {
|
|
|
|
test('via renderProps', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs({
|
2020-01-30 04:10:45 +08:00
|
|
|
class: ['foo', 'bar']
|
|
|
|
})
|
|
|
|
).toBe(` class="foo bar"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('standalone', () => {
|
2020-02-07 01:07:25 +08:00
|
|
|
expect(ssrRenderClass(`foo`)).toBe(`foo`)
|
|
|
|
expect(ssrRenderClass([`foo`, `bar`])).toBe(`foo bar`)
|
|
|
|
expect(ssrRenderClass({ foo: true, bar: false })).toBe(`foo`)
|
|
|
|
expect(ssrRenderClass([{ foo: true, bar: false }, `baz`])).toBe(`foo baz`)
|
2020-01-30 04:10:45 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('escape class values', () => {
|
2020-02-07 01:07:25 +08:00
|
|
|
expect(ssrRenderClass(`"><script`)).toBe(`"><script`)
|
2020-01-30 04:10:45 +08:00
|
|
|
})
|
|
|
|
})
|
2020-01-28 06:23:42 +08:00
|
|
|
|
2020-01-30 04:10:45 +08:00
|
|
|
describe('ssr: renderStyle', () => {
|
|
|
|
test('via renderProps', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderAttrs({
|
2020-01-30 04:10:45 +08:00
|
|
|
style: {
|
|
|
|
color: 'red'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
).toBe(` style="color:red;"`)
|
|
|
|
})
|
2020-01-28 06:23:42 +08:00
|
|
|
|
2020-01-30 04:10:45 +08:00
|
|
|
test('standalone', () => {
|
2020-02-07 01:07:25 +08:00
|
|
|
expect(ssrRenderStyle(`color:red`)).toBe(`color:red`)
|
2020-01-30 04:10:45 +08:00
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderStyle({
|
2020-01-30 04:10:45 +08:00
|
|
|
color: `red`
|
|
|
|
})
|
|
|
|
).toBe(`color:red;`)
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderStyle([
|
2020-01-30 04:10:45 +08:00
|
|
|
{ color: `red` },
|
|
|
|
{ fontSize: `15px` } // case conversion
|
|
|
|
])
|
|
|
|
).toBe(`color:red;font-size:15px;`)
|
|
|
|
})
|
2020-01-28 06:23:42 +08:00
|
|
|
|
2020-01-30 04:10:45 +08:00
|
|
|
test('number handling', () => {
|
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderStyle({
|
2020-01-30 04:10:45 +08:00
|
|
|
fontSize: 15, // should be ignored since font-size requires unit
|
|
|
|
opacity: 0.5
|
|
|
|
})
|
|
|
|
).toBe(`opacity:0.5;`)
|
|
|
|
})
|
2020-01-28 06:23:42 +08:00
|
|
|
|
2020-01-30 04:10:45 +08:00
|
|
|
test('escape inline CSS', () => {
|
2020-02-07 01:07:25 +08:00
|
|
|
expect(ssrRenderStyle(`"><script`)).toBe(`"><script`)
|
2020-01-30 04:10:45 +08:00
|
|
|
expect(
|
2020-02-07 01:07:25 +08:00
|
|
|
ssrRenderStyle({
|
2020-01-30 04:10:45 +08:00
|
|
|
color: `"><script`
|
|
|
|
})
|
|
|
|
).toBe(`color:"><script;`)
|
|
|
|
})
|
2020-01-28 06:23:42 +08:00
|
|
|
})
|