|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import copy from 'copy-to-clipboard'; |
| 4 | +import MarkdownRenderer from '../src/MarkdownRenderer'; |
| 5 | + |
| 6 | +// Mock the copy-to-clipboard library |
| 7 | +jest.mock('copy-to-clipboard'); |
| 8 | + |
| 9 | +// Mock document.execCommand which is used as a fallback |
| 10 | +beforeEach(() => { |
| 11 | + document.execCommand = jest.fn(); |
| 12 | + document.createRange = () => ({ |
| 13 | + setStart: () => true, |
| 14 | + setEnd: () => true, |
| 15 | + commonAncestorContainer: { |
| 16 | + nodeName: 'BODY', |
| 17 | + ownerDocument: document, |
| 18 | + }, |
| 19 | + }); |
| 20 | + window.getSelection = () => ({ |
| 21 | + removeAllRanges: () => undefined, |
| 22 | + addRange: () => undefined, |
| 23 | + toString: () => '', |
| 24 | + }); |
| 25 | + |
| 26 | + // Mock the clipboard API |
| 27 | + Object.defineProperty(navigator, 'clipboard', { |
| 28 | + value: { |
| 29 | + writeText: jest.fn().mockResolvedValue(undefined), |
| 30 | + }, |
| 31 | + writable: true, |
| 32 | + configurable: true, |
| 33 | + }); |
| 34 | + |
| 35 | + // Reset all mocks |
| 36 | + jest.clearAllMocks(); |
| 37 | +}); |
| 38 | + |
| 39 | +afterEach(() => { |
| 40 | + jest.clearAllMocks(); |
| 41 | + jest.restoreAllMocks(); |
| 42 | +}); |
| 43 | + |
| 44 | +describe('MarkdownRenderer', () => { |
| 45 | + it('renders without crashing', () => { |
| 46 | + render(<MarkdownRenderer>{'<h2>Test</h2>'}</MarkdownRenderer>); |
| 47 | + expect(screen.getByText('Test').tagName).toBe('H2'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('renders code blocks with copy button', () => { |
| 51 | + const { container } = render( |
| 52 | + <MarkdownRenderer> |
| 53 | + {`<div class="rcv-code-renderer"><pre><code>const a = 1;</code></pre></div>`} |
| 54 | + </MarkdownRenderer> |
| 55 | + ); |
| 56 | + |
| 57 | + const codeBlock = container.querySelector('.rcv-code-renderer'); |
| 58 | + expect(codeBlock).toBeInTheDocument(); |
| 59 | + |
| 60 | + // Check if copy button is rendered with the correct class |
| 61 | + const copyButton = container.querySelector('button[title="Copy code"]'); |
| 62 | + expect(copyButton).toBeInTheDocument(); |
| 63 | + expect(copyButton).toHaveClass('btn-copy-code'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('applies custom copy button props', () => { |
| 67 | + const customProps = { |
| 68 | + 'data-testid': 'custom-copy-button', |
| 69 | + class: 'custom-button-class', |
| 70 | + onClick: jest.fn(), |
| 71 | + }; |
| 72 | + |
| 73 | + const { container } = render( |
| 74 | + <MarkdownRenderer copyButtonProps={customProps}> |
| 75 | + {`<div class="rcv-code-renderer"><pre><code>const a = 1;</code></pre></div>`} |
| 76 | + </MarkdownRenderer> |
| 77 | + ); |
| 78 | + |
| 79 | + const copyButton = container.querySelector('button[title="Copy code"]'); |
| 80 | + expect(copyButton).toHaveAttribute('data-testid', 'custom-copy-button'); |
| 81 | + expect(copyButton.getAttribute('class')).toEqual('custom-button-class'); |
| 82 | + expect(copyButton.getAttribute('data-testid')).toEqual('custom-copy-button'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('copies code to clipboard when copy button is clicked', () => { |
| 86 | + const { container } = render( |
| 87 | + <MarkdownRenderer> |
| 88 | + {`<div class="rcv-code-renderer"><pre><code>const test = 'test';</code></pre></div>`} |
| 89 | + </MarkdownRenderer> |
| 90 | + ); |
| 91 | + |
| 92 | + const copyButton = container.querySelector('button[title="Copy code"]'); |
| 93 | + fireEvent.click(copyButton); |
| 94 | + |
| 95 | + // Check if copy-to-clipboard was called with the correct code |
| 96 | + expect(copy).toHaveBeenCalledWith('const test = \'test\';'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('copies code using copy-to-clipboard', () => { |
| 100 | + const { container } = render( |
| 101 | + <MarkdownRenderer> |
| 102 | + {`<div class="rcv-code-renderer"><pre><code>const test = 'test';</code></pre></div>`} |
| 103 | + </MarkdownRenderer> |
| 104 | + ); |
| 105 | + |
| 106 | + const copyButton = container.querySelector('button[title="Copy code"]'); |
| 107 | + fireEvent.click(copyButton); |
| 108 | + |
| 109 | + // Verify copy was called with the correct text (including the semicolon) |
| 110 | + expect(copy).toHaveBeenCalledWith('const test = \'test\';'); |
| 111 | + |
| 112 | + // Verify the copy button exists and has the correct title |
| 113 | + expect(copyButton).toBeInTheDocument(); |
| 114 | + expect(copyButton).toHaveAttribute('title', 'Copy code'); |
| 115 | + |
| 116 | + // Verify the copy function was called with the correct text |
| 117 | + expect(copy).toHaveBeenCalledWith('const test = \'test\';'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('adds copy button by default', () => { |
| 121 | + const { container } = render( |
| 122 | + <MarkdownRenderer> |
| 123 | + {`<div class="rcv-code-renderer"><pre><code>with copy</code></pre></div>`} |
| 124 | + </MarkdownRenderer> |
| 125 | + ); |
| 126 | + |
| 127 | + const copyButton = container.querySelector('button[title="Copy code"]'); |
| 128 | + expect(copyButton).toBeInTheDocument(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('handles multiple code blocks', () => { |
| 132 | + const { container } = render( |
| 133 | + <MarkdownRenderer> |
| 134 | + {` |
| 135 | + <div class="rcv-code-renderer"><pre><code>first block</code></pre></div> |
| 136 | + <div class="rcv-code-renderer"><pre><code>second block</code></pre></div> |
| 137 | + `} |
| 138 | + </MarkdownRenderer> |
| 139 | + ); |
| 140 | + |
| 141 | + const copyButtons = container.querySelectorAll('button[title="Copy code"]'); |
| 142 | + expect(copyButtons).toHaveLength(2); |
| 143 | + |
| 144 | + // Test first button |
| 145 | + fireEvent.click(copyButtons[0]); |
| 146 | + expect(copy).toHaveBeenCalledWith('first block'); |
| 147 | + |
| 148 | + // Test second button |
| 149 | + fireEvent.click(copyButtons[1]); |
| 150 | + expect(copy).toHaveBeenCalledWith('second block'); |
| 151 | + }); |
| 152 | +}); |
0 commit comments