base.test.tsx
This file
Path
src/jsx/base.test.tsx
79 lines · 2.8 KiB · TypeScript
Last changed by
fix(jsx): allow JSXNode function component results (#5476)Shaurya Sariabefore adoptionf23b146
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–2why, one step back1/** @jsxImportSource ./ */2Merge commit from forkTaku Amanobefore adoption2b8ed40
line 3why, one step back3import type { HtmlEscapedString } from '../utils/html'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 4–39why, one step back4import type { Child, JSXNode } from './base'5// eslint-disable-next-line @typescript-eslint/no-unused-vars6import { cloneElement, jsx, Fragment } from './base'78describe('cloneElement', () => {9 it('should clone an element with new props', () => {10 const element = <div className='original'>Hello</div>11 const clonedElement = cloneElement(element, { className: 'cloned' })12 expect((clonedElement as unknown as JSXNode).props.className).toBe('cloned')13 expect((clonedElement as unknown as JSXNode).props.children).toBe('Hello')14 })1516 it('should clone a children element', () => {17 const fnElement = ({ message }: { message: string }) => <div>{message}</div>18 const element = fnElement({ message: 'Hello' })19 const clonedElement = cloneElement(element, {})20 expect(element.toString()).toBe('<div>Hello</div>')21 expect(clonedElement.toString()).toBe('<div>Hello</div>')22 })2324 it('should clone an element with new children', () => {25 const fnElement = ({ message }: { message: string }) => <div>{message}</div>26 const element = fnElement({ message: 'Hello' })27 const clonedElement = cloneElement(element, {}, 'World')28 expect(element.toString()).toBe('<div>Hello</div>')29 expect(clonedElement.toString()).toBe('<div>World</div>')30 })3132 it('should self-close a wrapped empty tag', () => {33 const Hr = ({ ...props }) => <hr {...props} />34 const element = <Hr />35 expect(element.toString()).toBe('<hr/>')36 })37})3839describe('createElement', () => {fix(jsx): allow JSXNode function component results (#5476)Shaurya Sariabefore adoptionf23b146
lines 40–45why, one step back40 it('should accept a component that returns a JSXNode', () => {41 const Partial = ({ name }: { name: string }) => jsx('div', { 'x-partial': name }, name)4243 expect((<Partial name='foo' />).toString()).toBe('<div x-partial="foo">foo</div>')44 })45Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 46–57why, one step back46 it('should preserve the SVG element shape', () => {47 const ref = { current: null }48 const element = jsx('svg', { ref }) as unknown as JSXNode49 expect(element.tag).toBe('svg')50 expect(element.type).toBe('svg')51 expect(element.ref).toBe(ref)52 })5354 it('should accept a Child-typed value as a child', () => {55 const child: Child = <span>inner</span>56 const element = jsx('div', null, child) as unknown as JSXNode57 expect(element.toString()).toBe('<div><span>inner</span></div>')Merge commit from forkTaku Amanobefore adoption2b8ed40
lines 58–77why, one step back58 })5960 it('should escape a string returned by an asynchronous component', async () => {61 const Async = async () => '<img src=x onerror=alert(1)>' as unknown as HtmlEscapedString6263 expect(64 String(65 await (66 <div>67 <Async />68 </div>69 ).toString()70 )71 ).toBe('<div><img src=x onerror=alert(1)></div>')72 })7374 it('should omit an unsupported object child', () => {75 const object = { toString: () => 'plain-text' }7677 expect((<div>{object as never}</div>).toString()).toBe('<div></div>')Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 78–79why, one step back78 })79})