-
Notifications
You must be signed in to change notification settings - Fork 26.4k
fix(core): infer tag name for test root component #62283
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
base: main
Are you sure you want to change the base?
Conversation
Currently when a component is inserted, the tag name of its host is inferred from the selector, however in tests we always render it out as a `div`. These changes switch to inferring the tag name so that things like `querySelector` work consistently.
const testComponentRenderer = this.inject(TestComponentRenderer); | ||
const componentDef: ComponentDef<T> = (type as any).ɵcmp; | ||
const rootElId = `root${_nextRootElementId++}`; | ||
const defaultTag = componentDef.selectors[0][0] as string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this also work for non-trivial selectors, e.g. button[matButton]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is the selector after it has been parsed by the compiler into a CssSelectorList
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crisbeto it looks like it is roughly the same logic as in
angular/packages/core/src/render3/component_ref.ts
Lines 190 to 192 in b848590
const tagName = ((componentDef.selectors[0][0] as string) || 'div').toLowerCase(); | |
const namespace = | |
tagName === 'svg' ? SVG_NAMESPACE : tagName === 'math' ? MATH_ML_NAMESPACE : null; |
The main difference being that component ref takes namespace into account (?). Maybe it would make sense to factor this out and re-use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm good callout, although I guess there aren't many usages of SVG or MathML for component roots since we haven't heard about this before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeh, I was just looking at it from the "don't duplicate code / concepts" side of things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
reviewed-for: fw-general, public-api
// Note: injecting the renderer before accessing the definition appears to be load-bearing. | ||
const testComponentRenderer = this.inject(TestComponentRenderer); | ||
const componentDef: ComponentDef<T> = (type as any).ɵcmp; | ||
const rootElId = `root${_nextRootElementId++}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional nit: It'd be nice to have a comment for the next two lines explaining what this is doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Reviewed-for: public-api
Currently when a component is inserted, the tag name of its host is inferred from the selector, however in tests we always render it out as a
div
.These changes switch to inferring the tag name so that things like
querySelector
work consistently.