|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | + |
| 3 | +import { Trans, TransMacro } from './trans'; |
| 4 | + |
| 5 | +jest.mock('@lingui/react', () => ({ |
| 6 | + Trans: jest.fn(({ children, ...props }) => ( |
| 7 | + <span |
| 8 | + data-testid="mock-trans" |
| 9 | + {...props} |
| 10 | + > |
| 11 | + {children} |
| 12 | + </span> |
| 13 | + )), |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('@lingui/react/macro', () => ({ |
| 17 | + Trans: jest.fn(({ children, ...props }) => ( |
| 18 | + <span |
| 19 | + data-testid="mock-macro" |
| 20 | + {...props} |
| 21 | + > |
| 22 | + {children} |
| 23 | + </span> |
| 24 | + )), |
| 25 | +})); |
| 26 | + |
| 27 | +describe('Trans Component', () => { |
| 28 | + it('should render the Trans component with passed props', () => { |
| 29 | + const { getByTestId } = render(<Trans id="test.message" />); |
| 30 | + const element = getByTestId('mock-trans'); |
| 31 | + |
| 32 | + expect(element).toBeInTheDocument(); |
| 33 | + expect(element).toHaveAttribute('id', 'test.message'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should render children correctly', () => { |
| 37 | + const { getByTestId, getByText } = render( |
| 38 | + <Trans id="test.message">Hello, world!</Trans>, |
| 39 | + ); |
| 40 | + const element = getByTestId('mock-trans'); |
| 41 | + |
| 42 | + expect(element).toBeInTheDocument(); |
| 43 | + expect(getByText('Hello, world!')).toBeInTheDocument(); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('TransMacro Component', () => { |
| 48 | + it('should render the TransMacro component with passed props', () => { |
| 49 | + const { getByTestId } = render(<TransMacro id="macro.message">Test</TransMacro>); |
| 50 | + const element = getByTestId('mock-macro'); |
| 51 | + |
| 52 | + expect(element).toBeInTheDocument(); |
| 53 | + expect(element).toHaveAttribute('id', 'macro.message'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should render children correctly', () => { |
| 57 | + const { getByTestId, getByText } = render( |
| 58 | + <TransMacro id="macro.message">This is a macro!</TransMacro>, |
| 59 | + ); |
| 60 | + const element = getByTestId('mock-macro'); |
| 61 | + |
| 62 | + expect(element).toBeInTheDocument(); |
| 63 | + expect(getByText('This is a macro!')).toBeInTheDocument(); |
| 64 | + }); |
| 65 | +}); |
0 commit comments