vue3-yuanma/packages/server-renderer/__tests__/ssrRenderAttrs.spec.ts

183 lines
3.8 KiB
TypeScript
Raw Normal View History

/**
* @jest-environment node
*/
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', () => {
test('ignore reserved props', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderAttrs({
key: 1,
ref: () => {},
onClick: () => {}
})
).toBe('')
2020-01-28 06:23:42 +08:00
})
test('normal attrs', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderAttrs({
id: 'foo',
title: 'bar'
})
).toBe(` id="foo" title="bar"`)
})
test('empty value attrs', () => {
expect(
ssrRenderAttrs({
'data-v-abc': ''
})
).toBe(` data-v-abc`)
})
test('escape attrs', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderAttrs({
id: '"><script'
})
).toBe(` id="&quot;&gt;&lt;script"`)
})
test('boolean attrs', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderAttrs({
checked: true,
multiple: false,
readonly: 0,
disabled: ''
})
).toBe(` checked disabled`) // boolean attr w/ false should be ignored
})
test('ignore falsy values', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderAttrs({
foo: false,
title: null,
baz: undefined
})
).toBe(` foo="false"`) // non boolean should render `false` as is
})
2020-05-01 21:42:58 +08:00
test('ignore non-renderable values', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderAttrs({
foo: {},
bar: [],
baz: () => {}
})
).toBe(``)
})
test('props to attrs', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderAttrs({
readOnly: true, // simple lower case conversion
htmlFor: 'foobar' // special cases
})
).toBe(` readonly for="foobar"`)
})
test('preserve name on custom element', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderAttrs(
{
fooBar: 'ok'
},
'my-el'
)
).toBe(` fooBar="ok"`)
})
})
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>`)}"`
)
})
})
describe('ssr: renderClass', () => {
test('via renderProps', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderAttrs({
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`)
})
test('escape class values', () => {
2020-02-07 01:07:25 +08:00
expect(ssrRenderClass(`"><script`)).toBe(`&quot;&gt;&lt;script`)
})
})
2020-01-28 06:23:42 +08:00
describe('ssr: renderStyle', () => {
test('via renderProps', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderAttrs({
style: {
color: 'red'
}
})
).toBe(` style="color:red;"`)
})
2020-01-28 06:23:42 +08:00
test('standalone', () => {
2020-02-07 01:07:25 +08:00
expect(ssrRenderStyle(`color:red`)).toBe(`color:red`)
expect(
2020-02-07 01:07:25 +08:00
ssrRenderStyle({
color: `red`
})
).toBe(`color:red;`)
expect(
2020-02-07 01:07:25 +08:00
ssrRenderStyle([
{ color: `red` },
{ fontSize: `15px` } // case conversion
])
).toBe(`color:red;font-size:15px;`)
})
2020-01-28 06:23:42 +08:00
test('number handling', () => {
expect(
2020-02-07 01:07:25 +08:00
ssrRenderStyle({
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
test('escape inline CSS', () => {
2020-02-07 01:07:25 +08:00
expect(ssrRenderStyle(`"><script`)).toBe(`&quot;&gt;&lt;script`)
expect(
2020-02-07 01:07:25 +08:00
ssrRenderStyle({
color: `"><script`
})
).toBe(`color:&quot;&gt;&lt;script;`)
})
2020-01-28 06:23:42 +08:00
})