An open source design system that's fully customizable and agent ready
TypeScript
fix(Section): add xstyle prop and comprehensive tests (#721)
Cindy Zhang7ad0eae94ca1
diff --git a/packages/core/src/Section/XDSSection.test.tsx b/packages/core/src/Section/XDSSection.test.tsxnew file mode 100644index 0000000000..7808a6b777--- /dev/null+++ b/packages/core/src/Section/XDSSection.test.tsx@@ -0,0 +1,153 @@+import {describe, it, expect} from 'vitest';+import {render, screen} from '@testing-library/react';+import {XDSSection} from './XDSSection';++describe('XDSSection', () => {+ it('renders with default props', () => {+ const {container} = render(<XDSSection>Default section</XDSSection>);+ expect(container.firstElementChild).toBeInTheDocument();+ expect(screen.getByText('Default section')).toBeInTheDocument();+ });++ it('renders children', () => {+ render(+ <XDSSection>+ <span data-testid="child">Hello</span>+ </XDSSection>,+ );+ expect(screen.getByTestId('child')).toBeInTheDocument();+ expect(screen.getByText('Hello')).toBeInTheDocument();+ });++ it('renders with variant="section" (default)', () => {+ const {container} = render(<XDSSection>Content</XDSSection>);+ const root = container.firstElementChild!;+ expect(root.className).toContain('xds-section');+ expect(root.className).toContain('section');+ });++ it('renders with variant="transparent"', () => {+ const {container} = render(+ <XDSSection variant="transparent">Content</XDSSection>,+ );+ const root = container.firstElementChild!;+ expect(root.className).toContain('xds-section');+ expect(root.className).toContain('transparent');+ });++ it('renders with variant="wash"', () => {+ const {container} = render(<XDSSection variant="wash">Content</XDSSection>);+ const root = container.firstElementChild!;+ expect(root.className).toContain('xds-section');+ expect(root.className).toContain('wash');+ });++ it('renders with dividers', () => {+ const {container} = render(+ <XDSSection dividers={['top', 'bottom']}>Content</XDSSection>,+ );+ // The component should render without error+ expect(container.firstElementChild).toBeInTheDocument();+ expect(screen.getByText('Content')).toBeInTheDocument();+ });++ it('renders with padding prop', () => {+ const {container} = render(<XDSSection padding={2}>Content</XDSSection>);+ expect(container.firstElementChild).toBeInTheDocument();+ expect(screen.getByText('Content')).toBeInTheDocument();+ });++ it('renders with width and height without error', () => {+ const {container} = render(+ <XDSSection width={400} height={300}>+ Content+ </XDSSection>,+ );+ const root = container.firstElementChild as HTMLElement;+ expect(root).toBeInTheDocument();+ // Sizing is applied via stylex dynamic styles (CSS custom properties)+ // which aren't reflected in element.style in test environments+ expect(screen.getByText('Content')).toBeInTheDocument();+ });++ it('renders with maxWidth and minHeight without error', () => {+ const {container} = render(+ <XDSSection maxWidth={600} minHeight={200}>+ Content+ </XDSSection>,+ );+ const root = container.firstElementChild as HTMLElement;+ expect(root).toBeInTheDocument();+ expect(screen.getByText('Content')).toBeInTheDocument();+ });++ it('renders with string size values without error', () => {+ const {container} = render(+ <XDSSection width="50%" height="auto">+ Content+ </XDSSection>,+ );+ const root = container.firstElementChild as HTMLElement;+ expect(root).toBeInTheDocument();+ expect(screen.getByText('Content')).toBeInTheDocument();+ });++ it('forwards ref', () => {+ const ref = {current: null as HTMLElement | null};+ render(<XDSSection ref={ref}>Content</XDSSection>);+ expect(ref.current).toBeInstanceOf(HTMLDivElement);+ });++ it('renders xds-* class names for theme targeting', () => {+ const {container} = render(<XDSSection>Content</XDSSection>);+ const root = container.firstElementChild!;+ expect(root.className).toContain('xds-section');+ });++ it('renders variant in xds class names', () => {+ const {container} = render(<XDSSection variant="wash">Content</XDSSection>);+ const root = container.firstElementChild!;+ expect(root.className).toContain('xds-section');+ expect(root.className).toContain('wash');+ });++ it('accepts xstyle prop without error', () => {+ // xstyle is a StyleXStyles type; in tests stylex.create returns objects+ // that may not produce runtime styles, but the prop should be accepted+ const {container} = render(+ <XDSSection xstyle={undefined}>Content</XDSSection>,+ );+ expect(container.firstElementChild).toBeInTheDocument();+ });++ it('accepts className prop', () => {+ const {container} = render(+ <XDSSection className="custom-class">Content</XDSSection>,+ );+ const root = container.firstElementChild!;+ expect(root.className).toContain('custom-class');+ });++ it('accepts style prop', () => {+ const {container} = render(+ <XDSSection style={{opacity: 0.5}}>Content</XDSSection>,+ );+ const root = container.firstElementChild as HTMLElement;+ expect(root.style.opacity).toBe('0.5');+ });++ it('has two-div structure (outer + inner)', () => {+ const {container} = render(<XDSSection>Content</XDSSection>);+ const outer = container.firstElementChild!;+ const inner = outer.firstElementChild!;+ expect(outer.tagName).toBe('DIV');+ expect(inner.tagName).toBe('DIV');+ // Children are inside the inner div+ expect(inner.textContent).toBe('Content');+ });++ it('spreads additional props', () => {+ render(<XDSSection data-testid="custom-section">Content</XDSSection>);+ expect(screen.getByTestId('custom-section')).toBeInTheDocument();+ });+});diff --git a/packages/core/src/Section/XDSSection.tsx b/packages/core/src/Section/XDSSection.tsxindex ce5d734639..2aca042f56 100644--- a/packages/core/src/Section/XDSSection.tsx+++ b/packages/core/src/Section/XDSSection.tsx@@ -14,6 +14,7 @@import {type ReactNode} from 'react';import * as stylex from '@stylexjs/stylex';+import type {StyleXStyles} from '@stylexjs/stylex';import {colorVars} from '../theme/tokens.stylex';import {container} from '../Layout/container.stylex';import type {SpacingToken} from '../Layout/container.stylex';@@ -107,6 +108,25 @@ const dynamicStyles = stylex.create({export interface XDSSectionProps {ref?: React.Ref<HTMLElement>;+ /**+ * StyleX styles created via `stylex.create()`. Merged with the component's+ * base styles inside a single `stylex.props()` call on the outer wrapper.+ *+ * @example+ * ```+ * const overrides = stylex.create({ root: { marginBottom: 8 } });+ * <XDSSection xstyle={overrides.root} />+ * ```+ */+ xstyle?: StyleXStyles;+ /**+ * CSS class name(s) appended to the root element.+ */+ className?: string;+ /**+ * Inline styles to apply to the root element.+ */+ style?: React.CSSProperties;/*** Visual variant of the section.* - 'section': Surface background color@@ -191,6 +211,9 @@ export function XDSSection({children,dividers,padding,+ xstyle,+ className,+ style,ref,...props}: XDSSectionProps) {@@ -209,7 +232,10 @@ export function XDSSection({maxWidth ?? null,minHeight ?? null,),+ xstyle,),+ className,+ style,)}{...props}><div