Skip to content

Commit

Permalink
Fix preact issue with Trans when both children and components are und…
Browse files Browse the repository at this point in the history
…efined (#1368)

* Fix preact issue with Trans when both children and components are undefined

* Add test case for the bug
  • Loading branch information
visualfanatic committed Sep 1, 2021
1 parent 04b2bca commit ca410ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Trans.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
// call mapAST with having react nodes nested into additional node like
// we did for the string ast from translation
// return the children of that extra node to get expected result
const result = mapAST([{ dummy: true, children }], ast, getAsArray(children || []));
const result = mapAST(
[{ dummy: true, children: children || [] }],
ast,
getAsArray(children || []),
);
return getChildren(result[0]);
}

Expand Down
17 changes: 17 additions & 0 deletions test/trans.render.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,23 @@ describe('trans with only content from translation file - no children', () => {
});
});

describe('trans with only html content from translation file - no children', () => {
const TestComponent = () => <Trans i18nKey="transTest1_customHtml2" />;
it('should render translated string', () => {
const { container } = render(<TestComponent />);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
<strong>
Go
</strong>
<br />
there.
</div>
`);
});
});

describe('trans should not break on invalid node from translations', () => {
const TestComponent = () => <Trans i18nKey="testInvalidHtml" />;
it('should render translated string', () => {
Expand Down

0 comments on commit ca410ff

Please sign in to comment.