|
1 |
| -describe('ssr: render props', () => { |
2 |
| - test('class', () => {}) |
| 1 | +import { renderProps, renderClass, renderStyle } from '../src' |
3 | 2 |
|
4 |
| - test('style', () => { |
5 |
| - // only render numbers for properties that allow no unit numbers |
| 3 | +describe('ssr: renderProps', () => { |
| 4 | + test('ignore reserved props', () => { |
| 5 | + expect( |
| 6 | + renderProps({ |
| 7 | + key: 1, |
| 8 | + ref: () => {}, |
| 9 | + onClick: () => {} |
| 10 | + }) |
| 11 | + ).toBe('') |
6 | 12 | })
|
7 | 13 |
|
8 |
| - test('normal attrs', () => {}) |
| 14 | + test('normal attrs', () => { |
| 15 | + expect( |
| 16 | + renderProps({ |
| 17 | + id: 'foo', |
| 18 | + title: 'bar' |
| 19 | + }) |
| 20 | + ).toBe(` id="foo" title="bar"`) |
| 21 | + }) |
| 22 | + |
| 23 | + test('escape attrs', () => { |
| 24 | + expect( |
| 25 | + renderProps({ |
| 26 | + id: '"><script' |
| 27 | + }) |
| 28 | + ).toBe(` id=""><script"`) |
| 29 | + }) |
| 30 | + |
| 31 | + test('boolean attrs', () => { |
| 32 | + expect( |
| 33 | + renderProps({ |
| 34 | + checked: true, |
| 35 | + multiple: false |
| 36 | + }) |
| 37 | + ).toBe(` checked`) // boolean attr w/ false should be ignored |
| 38 | + }) |
| 39 | + |
| 40 | + test('ignore falsy values', () => { |
| 41 | + expect( |
| 42 | + renderProps({ |
| 43 | + foo: false, |
| 44 | + title: null, |
| 45 | + baz: undefined |
| 46 | + }) |
| 47 | + ).toBe(` foo="false"`) // non boolean should render `false` as is |
| 48 | + }) |
| 49 | + |
| 50 | + test('props to attrs', () => { |
| 51 | + expect( |
| 52 | + renderProps({ |
| 53 | + readOnly: true, // simple lower case conversion |
| 54 | + htmlFor: 'foobar' // special cases |
| 55 | + }) |
| 56 | + ).toBe(` readonly for="foobar"`) |
| 57 | + }) |
| 58 | +}) |
9 | 59 |
|
10 |
| - test('boolean attrs', () => {}) |
| 60 | +describe('ssr: renderClass', () => { |
| 61 | + test('via renderProps', () => { |
| 62 | + expect( |
| 63 | + renderProps({ |
| 64 | + class: ['foo', 'bar'] |
| 65 | + }) |
| 66 | + ).toBe(` class="foo bar"`) |
| 67 | + }) |
| 68 | + |
| 69 | + test('standalone', () => { |
| 70 | + expect(renderClass(`foo`)).toBe(`foo`) |
| 71 | + expect(renderClass([`foo`, `bar`])).toBe(`foo bar`) |
| 72 | + expect(renderClass({ foo: true, bar: false })).toBe(`foo`) |
| 73 | + expect(renderClass([{ foo: true, bar: false }, `baz`])).toBe(`foo baz`) |
| 74 | + }) |
| 75 | + |
| 76 | + test('escape class values', () => { |
| 77 | + expect(renderClass(`"><script`)).toBe(`"><script`) |
| 78 | + }) |
| 79 | +}) |
11 | 80 |
|
12 |
| - test('enumerated attrs', () => {}) |
| 81 | +describe('ssr: renderStyle', () => { |
| 82 | + test('via renderProps', () => { |
| 83 | + expect( |
| 84 | + renderProps({ |
| 85 | + style: { |
| 86 | + color: 'red' |
| 87 | + } |
| 88 | + }) |
| 89 | + ).toBe(` style="color:red;"`) |
| 90 | + }) |
13 | 91 |
|
14 |
| - test('ignore falsy values', () => {}) |
| 92 | + test('standalone', () => { |
| 93 | + expect(renderStyle(`color:red`)).toBe(`color:red`) |
| 94 | + expect( |
| 95 | + renderStyle({ |
| 96 | + color: `red` |
| 97 | + }) |
| 98 | + ).toBe(`color:red;`) |
| 99 | + expect( |
| 100 | + renderStyle([ |
| 101 | + { color: `red` }, |
| 102 | + { fontSize: `15px` } // case conversion |
| 103 | + ]) |
| 104 | + ).toBe(`color:red;font-size:15px;`) |
| 105 | + }) |
15 | 106 |
|
16 |
| - test('props to attrs', () => {}) |
| 107 | + test('number handling', () => { |
| 108 | + expect( |
| 109 | + renderStyle({ |
| 110 | + fontSize: 15, // should be ignored since font-size requires unit |
| 111 | + opacity: 0.5 |
| 112 | + }) |
| 113 | + ).toBe(`opacity:0.5;`) |
| 114 | + }) |
17 | 115 |
|
18 |
| - test('ignore non-renderable props', () => {}) |
| 116 | + test('escape inline CSS', () => { |
| 117 | + expect(renderStyle(`"><script`)).toBe(`"><script`) |
| 118 | + expect( |
| 119 | + renderStyle({ |
| 120 | + color: `"><script` |
| 121 | + }) |
| 122 | + ).toBe(`color:"><script;`) |
| 123 | + }) |
19 | 124 | })
|
0 commit comments