server.test.tsx
This file
Path
src/jsx/dom/server.test.tsx
199 lines · 6.4 KiB · TypeScript
Last changed by
Merge commit from forkTaku Amanobefore adoption2b8ed40
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 1why, one step back1/** @jsxImportSource ../ */Merge commit from forkTaku Amanobefore adoption2b8ed40
line 2why, one step back2import { raw } from '../../helper/html'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 3–4why, one step back3import { renderToReadableStream, renderToString } from './server'4Merge commit from forkTaku Amanobefore adoption2b8ed40
lines 5–7why, one step back5const unsafeHtml = '<img src=x onerror=alert(1)>'6const escapedUnsafeHtml = '<img src=x onerror=alert(1)>'7Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 8–28why, one step back8describe('renderToString', () => {9 it('Should be able to render HTML element', () => {10 expect(renderToString(<h1>Hello</h1>)).toBe('<h1>Hello</h1>')11 })1213 it('Should be able to render null', () => {14 expect(renderToString(null)).toBe('')15 })1617 it('Should be able to render undefined', () => {18 expect(renderToString(undefined)).toBe('')19 })2021 it('Should be able to render number', () => {22 expect(renderToString(1)).toBe('1')23 })2425 it('Should be able to render string', () => {26 expect(renderToString('Hono')).toBe('Hono')27 })28Merge commit from forkTaku Amanobefore adoption2b8ed40
lines 29–62why, one step back29 it('Should escape a string root', () => {30 expect(renderToString(unsafeHtml)).toBe(escapedUnsafeHtml)31 })3233 it('Should preserve an explicitly trusted root', () => {34 expect(renderToString(raw('<strong>trusted</strong>'))).toBe('<strong>trusted</strong>')35 })3637 it('Should escape nested array roots without separators', () => {38 expect(renderToString([unsafeHtml, [unsafeHtml]])).toBe(39 `${escapedUnsafeHtml}${escapedUnsafeHtml}`40 )41 })4243 it('Should preserve trusted content in an array root', () => {44 expect(renderToString([raw('<strong>trusted</strong>'), unsafeHtml])).toBe(45 `<strong>trusted</strong>${escapedUnsafeHtml}`46 )47 })4849 it('Should reject an asynchronous array root', () => {50 expect(() => renderToString([Promise.resolve(unsafeHtml)])).toThrow(51 'Async component is not supported in renderToString'52 )53 })5455 it('Should keep non-string roots unchanged', () => {56 const object = { toString: () => 'plain-text' }5758 expect(renderToString(10n as never)).toBe('10')59 expect(renderToString(Symbol('value') as never)).toBe('Symbol(value)')60 expect(renderToString(object as never)).toBe('plain-text')61 })62Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 63–111why, one step back63 it('Should omit options', () => {64 expect(renderToString('Hono', { identifierPrefix: 'test' })).toBe('Hono')65 })6667 it('Should raise error for async component', async () => {68 const AsyncComponent = async () => <h1>Hello from async component</h1>69 expect(() => renderToString(<AsyncComponent />)).toThrowError()70 })71})7273describe('renderToReadableStream', () => {74 const textDecoder = new TextDecoder()75 const getStringFromStream = async (stream: ReadableStream<Uint8Array>): Promise<string> => {76 const reader = stream.getReader()77 let str = ''78 for (;;) {79 const { done, value } = await reader.read()80 if (done) {81 break82 }83 str += textDecoder.decode(value)84 }85 return str86 }8788 it('Should be able to render HTML element', async () => {89 const stream = await renderToReadableStream(<h1>Hello</h1>)90 const reader = stream.getReader()91 let { done, value } = await reader.read()92 expect(done).toBe(false)93 expect(textDecoder.decode(value)).toBe('<h1>Hello</h1>')94 done = (await reader.read()).done95 expect(done).toBe(true)96 })9798 it('Should be able to render null', async () => {99 expect(await getStringFromStream(await renderToReadableStream(null))).toBe('')100 })101102 it('Should be able to render undefined', async () => {103 expect(await getStringFromStream(await renderToReadableStream(undefined))).toBe('')104 })105106 it('Should be able to render number', async () => {107 expect(await getStringFromStream(await renderToReadableStream(1))).toBe('1')108 })109110 it('Should be able to render string', async () => {111 expect(await getStringFromStream(await renderToReadableStream('Hono'))).toBe('Hono')Merge commit from forkTaku Amanobefore adoption2b8ed40
lines 112–144why, one step back112 })113114 it('Should escape a string root', async () => {115 expect(await getStringFromStream(await renderToReadableStream(unsafeHtml))).toBe(116 escapedUnsafeHtml117 )118 })119120 it('Should preserve an explicitly trusted root', async () => {121 expect(122 await getStringFromStream(await renderToReadableStream(raw('<strong>trusted</strong>')))123 ).toBe('<strong>trusted</strong>')124 })125126 it('Should escape synchronous and asynchronous array roots', async () => {127 expect(128 await getStringFromStream(await renderToReadableStream([unsafeHtml, [unsafeHtml]]))129 ).toBe(`${escapedUnsafeHtml}${escapedUnsafeHtml}`)130 expect(131 await getStringFromStream(await renderToReadableStream([Promise.resolve(unsafeHtml)]))132 ).toBe(escapedUnsafeHtml)133 })134135 it('Should preserve trusted content in an array root', async () => {136 expect(137 await getStringFromStream(138 await renderToReadableStream([raw('<strong>trusted</strong>'), unsafeHtml])139 )140 ).toBe(`<strong>trusted</strong>${escapedUnsafeHtml}`)141 })142143 it('Should keep non-string roots unchanged', async () => {144 expect(await getStringFromStream(await renderToReadableStream(10n as never))).toBe('10')Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 145–199why, one step back145 })146147 it('Should be called `onError` if there is an error', async () => {148 const ErrorComponent = async () => {149 throw new Error('Server error')150 }151152 const onError = vi.fn()153 expect(154 await getStringFromStream(await renderToReadableStream(<ErrorComponent />, { onError }))155 ).toBe('')156 expect(onError).toBeCalledWith(new Error('Server error'))157 })158159 it('Should not be called `onError` if there is no error', async () => {160 const onError = vi.fn(() => 'error')161 expect(await getStringFromStream(await renderToReadableStream('Hono', { onError }))).toBe(162 'Hono'163 )164 expect(onError).toBeCalledTimes(0)165 })166167 it('Should omit options, except onError', async () => {168 expect(169 await getStringFromStream(await renderToReadableStream('Hono', { identifierPrefix: 'test' }))170 ).toBe('Hono')171 })172173 it('Should be able to render async component', async () => {174 const ChildAsyncComponent = async () => {175 await new Promise((resolve) => setTimeout(resolve, 10))176 return <span>child async component</span>177 }178179 const AsyncComponent = async () => {180 await new Promise((resolve) => setTimeout(resolve, 10))181 return (182 <h1>183 Hello from async component184 <ChildAsyncComponent />185 </h1>186 )187 }188189 const stream = await renderToReadableStream(<AsyncComponent />)190 const reader = stream.getReader()191 let { done, value } = await reader.read()192 expect(done).toBe(false)193 expect(textDecoder.decode(value)).toBe(194 '<h1>Hello from async component<span>child async component</span></h1>'195 )196 done = (await reader.read()).done197 expect(done).toBe(true)198 })199})