locale.spec.tsx 2.5 KB

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