|
16 | 16 | * specific language governing permissions and limitations |
17 | 17 | * under the License. |
18 | 18 | */ |
19 | | -import { render, screen } from '../../spec'; |
| 19 | +import { render, screen, fireEvent } from '../../spec'; |
20 | 20 | import CodeSyntaxHighlighter from './index'; |
21 | 21 |
|
22 | 22 | // Simple mock that just returns the content |
@@ -153,4 +153,43 @@ describe('CodeSyntaxHighlighter', () => { |
153 | 153 |
|
154 | 154 | expect(screen.getByText('SELECT * FROM users;')).toBeInTheDocument(); |
155 | 155 | }); |
| 156 | + |
| 157 | + test('shows copy button by default', () => { |
| 158 | + render( |
| 159 | + <CodeSyntaxHighlighter language="sql">SELECT 1;</CodeSyntaxHighlighter>, |
| 160 | + ); |
| 161 | + |
| 162 | + expect(screen.getByTitle('Copy to clipboard')).toBeInTheDocument(); |
| 163 | + }); |
| 164 | + |
| 165 | + test('hides copy button when showCopyButton is false', () => { |
| 166 | + render( |
| 167 | + <CodeSyntaxHighlighter language="sql" showCopyButton={false}> |
| 168 | + SELECT 1; |
| 169 | + </CodeSyntaxHighlighter>, |
| 170 | + ); |
| 171 | + |
| 172 | + expect(screen.queryByTitle('Copy to clipboard')).not.toBeInTheDocument(); |
| 173 | + }); |
| 174 | + |
| 175 | + test('copy button does not throw when clipboard API is unavailable', () => { |
| 176 | + const originalClipboard = navigator.clipboard; |
| 177 | + Object.defineProperty(navigator, 'clipboard', { |
| 178 | + value: undefined, |
| 179 | + configurable: true, |
| 180 | + }); |
| 181 | + |
| 182 | + render( |
| 183 | + <CodeSyntaxHighlighter language="sql">SELECT 1;</CodeSyntaxHighlighter>, |
| 184 | + ); |
| 185 | + |
| 186 | + expect(() => |
| 187 | + fireEvent.click(screen.getByTitle('Copy to clipboard')), |
| 188 | + ).not.toThrow(); |
| 189 | + |
| 190 | + Object.defineProperty(navigator, 'clipboard', { |
| 191 | + value: originalClipboard, |
| 192 | + configurable: true, |
| 193 | + }); |
| 194 | + }); |
156 | 195 | }); |
0 commit comments