|
| 1 | +import { render, screen } from '@testing-library/react' |
| 2 | +import * as React from 'react' |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | +import PreCode from './pre-code' |
| 5 | + |
| 6 | +describe('PreCode Component', () => { |
| 7 | + it('renders children correctly inside the pre tag', () => { |
| 8 | + const { container } = render( |
| 9 | + <PreCode> |
| 10 | + <code data-testid="test-code">console.log("hello world")</code> |
| 11 | + </PreCode>, |
| 12 | + ) |
| 13 | + |
| 14 | + const preElement = container.querySelector('pre') |
| 15 | + const codeElement = screen.getByTestId('test-code') |
| 16 | + |
| 17 | + expect(preElement).toBeInTheDocument() |
| 18 | + expect(codeElement).toBeInTheDocument() |
| 19 | + // Verify code is a descendant of pre |
| 20 | + expect(preElement).toContainElement(codeElement) |
| 21 | + expect(codeElement.textContent).toBe('console.log("hello world")') |
| 22 | + }) |
| 23 | + |
| 24 | + it('contains the copy button span for CSS targeting', () => { |
| 25 | + const { container } = render( |
| 26 | + <PreCode> |
| 27 | + <code>test content</code> |
| 28 | + </PreCode>, |
| 29 | + ) |
| 30 | + |
| 31 | + const copySpan = container.querySelector('.copy-code-button') |
| 32 | + expect(copySpan).toBeInTheDocument() |
| 33 | + expect(copySpan?.tagName).toBe('SPAN') |
| 34 | + }) |
| 35 | + |
| 36 | + it('renders as a <pre> element', () => { |
| 37 | + const { container } = render(<PreCode>Content</PreCode>) |
| 38 | + expect(container.querySelector('pre')).toBeInTheDocument() |
| 39 | + }) |
| 40 | + |
| 41 | + it('handles multiple children correctly', () => { |
| 42 | + render( |
| 43 | + <PreCode> |
| 44 | + <span>Line 1</span> |
| 45 | + <span>Line 2</span> |
| 46 | + </PreCode>, |
| 47 | + ) |
| 48 | + |
| 49 | + expect(screen.getByText('Line 1')).toBeInTheDocument() |
| 50 | + expect(screen.getByText('Line 2')).toBeInTheDocument() |
| 51 | + }) |
| 52 | + |
| 53 | + it('correctly instantiates the pre element node', () => { |
| 54 | + const { container } = render(<PreCode>Ref check</PreCode>) |
| 55 | + const pre = container.querySelector('pre') |
| 56 | + |
| 57 | + // Verifies the node is an actual HTMLPreElement, |
| 58 | + // confirming the ref-linked element rendered correctly. |
| 59 | + expect(pre).toBeInstanceOf(HTMLPreElement) |
| 60 | + }) |
| 61 | +}) |
0 commit comments