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

feat: support componentImports option for standalone components #307

Conversation

jadengis
Copy link
Contributor

@jadengis jadengis commented Jul 6, 2022

Closes #306

This is a first stab at adding the ability to override the imports metadata field for standalone components.

This PR adds the componentImports field to the render function options. These imports are used to override the existing imports on a standalone component. The reasons for completely overriding imports are:

  • It's the most simple option API-wise. It's possible you could be more granular and allow the user to specify specific imports to override but this is likely overkill.
  • This is more similar to the 'blank slate' model of imports that all Angular component tests used prior to standalone components where you would need to specify all your dependencies in TestBed.configureTestModule.

As implemented, this only works for the render(FooComponent) style rendering and throws an error if componentImports option is passed when the subject-under-test is template or non-standalone component.

* A collection of imports to override a standalone component's imports with.
*
* @default
* undefined
Copy link
Member

Choose a reason for hiding this comment

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

To be consistent with the other default properties, I usually use an empty array.

Suggested change
* undefined
* []

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I responded to this comment below.

@@ -55,6 +55,7 @@ export async function render<SutType, WrapperType = SutType>(
wrapper = WrapperComponent as Type<WrapperType>,
componentProperties = {},
componentProviders = [],
componentImports,
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
componentImports,
componentImports = [],

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The rationale for leaving this undefined by default is so we can optionally overwrite the imports when needed instead of overwriting them all the time. For a simple presentational component you might not need to mock any dependencies.

projects/testing-library/tests/render.spec.ts Show resolved Hide resolved
* ]
* })
*/
componentImports?: (Type<any> | any[])[];
Copy link
Member

Choose a reason for hiding this comment

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

I believe this could change in the future, and I would like to be able to just do that without justifications 😅
Maybe we should mark it as experimental as well in the TS Docs?

Suggested change
componentImports?: (Type<any> | any[])[];
ɵcomponentImports?: (Type<any> | any[])[];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure np I can do that. Makes sense. I think the docs for standalone and imports mark them as experimental.

@jadengis jadengis force-pushed the johndengis/add-component-imports branch from 57c77e2 to d8d009d Compare July 6, 2022 07:18
Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

Thanks again!

@timdeschryver timdeschryver merged commit 459b500 into testing-library:main Jul 6, 2022
@github-actions
Copy link

github-actions bot commented Jul 6, 2022

🎉 This PR is included in version 12.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No automatic override for imports on standalone components
2 participants