locale.spec.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import {textWithMarkupMatcher} from 'sentry-test/utils';
  3. // eslint-disable-next-line no-restricted-imports
  4. import {tct} from 'sentry/locale';
  5. describe('locale.gettextComponentTemplate', () => {
  6. it('should not wrap translated text in span', () => {
  7. // spaces are removed because pretter keeps trying to remove them in the snapshot
  8. expect(
  9. tct('hello[one]two[three:3]', {
  10. one: ' one',
  11. three: <code />,
  12. })
  13. ).toMatchInlineSnapshot(`
  14. <React.Fragment>
  15. <React.Fragment>
  16. <React.Fragment>
  17. hello
  18. </React.Fragment>
  19. <React.Fragment>
  20. one
  21. </React.Fragment>
  22. <React.Fragment>
  23. two
  24. </React.Fragment>
  25. <code>
  26. <React.Fragment>
  27. 3
  28. </React.Fragment>
  29. </code>
  30. </React.Fragment>
  31. </React.Fragment>
  32. `);
  33. });
  34. it('should render two component templates inside the same parent', () => {
  35. render(
  36. <div>
  37. {tct('1st: [one]', {
  38. one: 'one',
  39. })}
  40. {tct('2nd: [two]', {
  41. two: 'two',
  42. })}
  43. </div>
  44. );
  45. expect(
  46. screen.getByText(textWithMarkupMatcher('1st: one2nd: two'))
  47. ).toBeInTheDocument();
  48. });
  49. it('should render multiple groups with the same name', () => {
  50. const RenderChildren = ({children}: {children?: React.ReactNode}) => children;
  51. render(
  52. <div>
  53. {tct('[render:one] [render:two] [render:three]', {
  54. render: <RenderChildren />,
  55. })}
  56. </div>
  57. );
  58. expect(screen.getByText(textWithMarkupMatcher('one two three'))).toBeInTheDocument();
  59. });
  60. it('should render multiple groups with the same name in an HTML tag', () => {
  61. const {container} = render(
  62. <div>
  63. {tct('[render:one] [render:two] [render:three]', {
  64. render: <b />,
  65. })}
  66. </div>
  67. );
  68. expect(screen.getByText(textWithMarkupMatcher('one two three'))).toBeInTheDocument();
  69. expect(container.innerHTML).toBe('<div><b>one</b> <b>two</b> <b>three</b></div>');
  70. });
  71. it('should render nested goups', () => {
  72. const {container} = render(
  73. <div>
  74. {tct('[bold:text with [link:another] group]', {
  75. bold: <b />,
  76. link: <a href="/link" />,
  77. })}
  78. </div>
  79. );
  80. expect(
  81. screen.getByText(textWithMarkupMatcher('text with another group'))
  82. ).toBeInTheDocument();
  83. expect(container.innerHTML).toBe(
  84. '<div><b>text with <a href="/link">another</a> group</b></div>'
  85. );
  86. });
  87. });