Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React] improve error handling in resolveReactComponent #2006

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/React/assets/src/register_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export function registerReactControllerComponents(context: __WebpackModuleApi.Re
const possibleValues = Object.keys(reactControllers).map((key) =>
key.replace('./', '').replace('.jsx', '').replace('.tsx', '')
);

if (possibleValues.includes(name)) {
throw new Error(
`React controller "${name}" could not be resolved. Ensure the module exports the controller as default.`
);
Comment on lines +42 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error(
`React controller "${name}" could not be resolved. Ensure the module exports the controller as default.`
);
throw new Error(`React controller "${name}" could not be resolved. Ensure the module exports the controller as default.`);

}

throw new Error(`React controller "${name}" does not exist. Possible values: ${possibleValues.join(', ')}`);
}

Expand Down
8 changes: 8 additions & 0 deletions src/React/assets/test/register_controller.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const createFakeFixturesContext = (): RequireContext => {
const files: any = {
'./MyJsxComponent.jsx': { default: MyJsxComponent },
'./MyTsxComponent.tsx': { default: MyTsxComponent },
'./NoDefaultExportComponent.jsx': { default: undefined },
};

const context = (id: string): any => files[id];
Expand All @@ -45,4 +46,11 @@ describe('registerReactControllerComponents', () => {

expect(() => resolveComponent('MyABCComponent')).toThrow('React controller "MyABCComponent" does not exist. Possible values: MyJsxComponent, MyTsxComponent');
});

it('throws when no default export found in imported module', () => {
registerReactControllerComponents(createFakeFixturesContext());
const resolveComponent = (window as any).resolveReactComponent;

expect(() => resolveComponent('NoDefaultExportComponent')).toThrow('React controller "NoDefaultExportComponent" could not be resolved. Ensure the module exports the controller as default.');
});
});
4 changes: 4 additions & 0 deletions src/React/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ For example:
return <div>Hello {props.fullName}</div>;
}

.. note::

Ensure your module exports the controller as the ``export default``. The default export is used when resolving components.

.. code-block:: html+twig

{# templates/home.html.twig #}
Expand Down
Loading