|
| 1 | +import React from 'react' |
| 2 | +import { render, screen, fireEvent } from 'uiSrc/utils/test-utils' |
| 3 | + |
| 4 | +import CodeBlock from './CodeBlock' |
| 5 | + |
| 6 | +const originalClipboard = { ...global.navigator.clipboard } |
| 7 | +describe('CodeBlock', () => { |
| 8 | + beforeEach(() => { |
| 9 | + // @ts-ignore |
| 10 | + global.navigator.clipboard = { |
| 11 | + writeText: jest.fn(), |
| 12 | + } |
| 13 | + }) |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + jest.resetAllMocks() |
| 17 | + // @ts-ignore |
| 18 | + global.navigator.clipboard = originalClipboard |
| 19 | + }) |
| 20 | + |
| 21 | + it('should render', () => { |
| 22 | + expect(render(<CodeBlock>text</CodeBlock>)).toBeTruthy() |
| 23 | + }) |
| 24 | + |
| 25 | + it('should render proper content', () => { |
| 26 | + render(<CodeBlock data-testid="code">text</CodeBlock>) |
| 27 | + expect(screen.getByTestId('code')).toHaveTextContent('text') |
| 28 | + }) |
| 29 | + |
| 30 | + it('should not render copy button by default', () => { |
| 31 | + render(<CodeBlock data-testid="code">text</CodeBlock>) |
| 32 | + expect(screen.queryByTestId('copy-code-btn')).not.toBeInTheDocument() |
| 33 | + }) |
| 34 | + |
| 35 | + it('should copy proper text', () => { |
| 36 | + render(<CodeBlock data-testid="code" isCopyable>text</CodeBlock>) |
| 37 | + fireEvent.click(screen.getByTestId('copy-code-btn')) |
| 38 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith('text') |
| 39 | + }) |
| 40 | + |
| 41 | + it('should copy proper text when children is ReactNode', () => { |
| 42 | + render(<CodeBlock data-testid="code" isCopyable><span>text2</span></CodeBlock>) |
| 43 | + fireEvent.click(screen.getByTestId('copy-code-btn')) |
| 44 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith('text2') |
| 45 | + }) |
| 46 | +}) |
0 commit comments