|
| 1 | +import { render, screen } from '@testing-library/react' |
| 2 | +import userEvent from '@testing-library/user-event' |
| 3 | +import { FluentProvider, webLightTheme } from '@fluentui/react-components' |
| 4 | +import SystemPromptBanner from './SystemPromptBanner' |
| 5 | + |
| 6 | +const TestWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => ( |
| 7 | + <FluentProvider theme={webLightTheme}>{children}</FluentProvider> |
| 8 | +) |
| 9 | + |
| 10 | +// jsdom has no layout engine, so scrollWidth/clientWidth are 0 by default (no overflow). |
| 11 | +// Force overflow by overriding the prototype getters for the duration of a test. |
| 12 | +function mockOverflow(scrollWidth: number, clientWidth: number) { |
| 13 | + Object.defineProperty(HTMLElement.prototype, 'scrollWidth', { configurable: true, get: () => scrollWidth }) |
| 14 | + Object.defineProperty(HTMLElement.prototype, 'clientWidth', { configurable: true, get: () => clientWidth }) |
| 15 | +} |
| 16 | + |
| 17 | +describe('SystemPromptBanner', () => { |
| 18 | + afterEach(() => { |
| 19 | + delete (HTMLElement.prototype as { scrollWidth?: number }).scrollWidth |
| 20 | + delete (HTMLElement.prototype as { clientWidth?: number }).clientWidth |
| 21 | + }) |
| 22 | + |
| 23 | + it('renders the label and the system prompt content', () => { |
| 24 | + render( |
| 25 | + <TestWrapper> |
| 26 | + <SystemPromptBanner content="You are a pirate." /> |
| 27 | + </TestWrapper> |
| 28 | + ) |
| 29 | + |
| 30 | + expect(screen.getByText('System Prompt')).toBeInTheDocument() |
| 31 | + expect(screen.getByText('You are a pirate.')).toBeInTheDocument() |
| 32 | + }) |
| 33 | + |
| 34 | + it('does not render an expand toggle when the content fits on one line', () => { |
| 35 | + render( |
| 36 | + <TestWrapper> |
| 37 | + <SystemPromptBanner content="Be terse." /> |
| 38 | + </TestWrapper> |
| 39 | + ) |
| 40 | + |
| 41 | + expect(screen.queryByRole('button', { name: /system prompt/i })).not.toBeInTheDocument() |
| 42 | + }) |
| 43 | + |
| 44 | + it('renders a collapsed expand toggle when the content overflows', () => { |
| 45 | + mockOverflow(1000, 200) |
| 46 | + render( |
| 47 | + <TestWrapper> |
| 48 | + <SystemPromptBanner content="A very long system prompt that does not fit on one line." /> |
| 49 | + </TestWrapper> |
| 50 | + ) |
| 51 | + |
| 52 | + expect(screen.getByRole('button', { name: /system prompt/i })).toHaveAttribute('aria-expanded', 'false') |
| 53 | + }) |
| 54 | + |
| 55 | + it('expands when the overflowing header is clicked', async () => { |
| 56 | + const user = userEvent.setup() |
| 57 | + mockOverflow(1000, 200) |
| 58 | + render( |
| 59 | + <TestWrapper> |
| 60 | + <SystemPromptBanner content="A very long system prompt that does not fit on one line." /> |
| 61 | + </TestWrapper> |
| 62 | + ) |
| 63 | + |
| 64 | + const toggle = screen.getByRole('button', { name: /system prompt/i }) |
| 65 | + await user.click(toggle) |
| 66 | + |
| 67 | + expect(toggle).toHaveAttribute('aria-expanded', 'true') |
| 68 | + }) |
| 69 | +}) |
0 commit comments