Skip to content

Commit

Permalink
Example of how types allow incorrect usage
Browse files Browse the repository at this point in the history
  • Loading branch information
danreeves committed Jun 7, 2022
1 parent b2dcdfa commit d8904f7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/trans.render.object.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,32 @@ describe('trans using no children but components (object) - interpolated compone
`);
});
});

describe('trans using no children but components (object) - ReactNodes that error', () => {
const Button = ({ children }) => <button type="button">{children}</button>;
// Suppress console.errors in these tests
let consoleErrorFn;
beforeAll(() => {
consoleErrorFn = jest.spyOn(console, 'error').mockImplementation(() => jest.fn());
});
afterAll(() => {
consoleErrorFn.restoreMocks();
});
it('should throw if you use some valid ReactNodes', () => {
expect(() =>
render(<Trans defaults="hello <ClickMe>Test</ClickMe>" components={{ ClickMe: Button }} />),
).toThrow();

expect(() =>
render(<Trans defaults="hello <ClickMe>Test</ClickMe>" components={{ ClickMe: {} }} />),
).toThrow();

expect(() =>
render(<Trans defaults="hello <ClickMe>Test</ClickMe>" components={{ ClickMe: null }} />),
).toThrow();

expect(() =>
render(<Trans defaults="hello <ClickMe>Test</ClickMe>" components={{ ClickMe: true }} />),
).toThrow();
});
});
23 changes: 23 additions & 0 deletions test/typescript/Trans.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ function objectComponents() {
return <Trans components={{ Btn: <button /> }} defaults="Hello <Btn />" />;
}

function MyComponent(): React.ReactElement {
return <>world</>;
}

function objectCustomComponents() {
return <Trans components={{ Btn: <MyComponent /> }} defaults="Hello <Btn />" />;
}

function objectCustomComponentsShouldError() {
return (
<Trans
// Valid ReactNodes according to types
components={{
Btn: MyComponent,
Btn2: null,
Btn4: true,
Btn5: {},
}}
defaults="Hello <Btn /><Btn2 /><Btn3 /><Btn4 /><Btn5 />"
/>
);
}

function constObjectComponents() {
const constObject = {
Btn: <button />,
Expand Down

0 comments on commit d8904f7

Please sign in to comment.