-
Notifications
You must be signed in to change notification settings - Fork 6.8k
test: provide a test component that opens components in a dialog #24522
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
Conversation
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.
Nice, that is pretty cool!
dialogRef: MatDialogRef<unknown>; | ||
|
||
/** Data passed to the `MatDialog` close method. */ | ||
closedResult: unknown; |
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.
We should be able to type component
and closedResult
, allowing for even easier access to the close result:
const dialogFixture = TestBed.createComponent(
Opener.withComponent<DialogResult>(ExampleComponent));
dialogFixture.detectChanges();
dialogFixture.componentInstance.closeResult; // is `DialogResult` instead of `unknown`.
what do you think?
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.
Yeah that's helpful. I added typing for withComponent
to include the same signature we give the MatDIalogRef
, which is the dialog component type T
and the result data R
.
I wish I could avoid typing the T
and instead inferring that from the parameter type, but I couldn't figure it out. I have a feeling there's a clever way to only need to pass the result data type?
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.
I was thinking we could at least automatically infer the hmm looks like it will always require two params to be specified then.. (microsoft/TypeScript#26242)ExampleComponent
(T), saving on one type parameter
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, just one little minor comment from my side
(wont let me respond on the thread) Yeah that inference is what I was trying to figure out, but I couldn't make it work. Any idea how to infer it? I tried something like this but no luck /** Static method that prepares this class to open the provided component. */
static withComponent<R, T = any>(component: ComponentType<T>, config?: MatDialogConfig) {
_MatTestDialogOpenerBase.component = component;
_MatTestDialogOpenerBase.config = config;
return MatTestDialogOpener as ComponentType<MatTestDialogOpener<ComponentType<T>, R>>;
} |
yeah, I just experimented as well, but no luck. Tried using an overload and similar, but seems like a limitation in TS currently. Maybe others have more ideas though 😆 |
Updated to make the generics optional (e.g. |
) * test: provide a test component that opens components in a dialog * test: update goldens * test: protected static, remove DI, noop animations * test: add typing * test: make generics optional * test: update goldens (cherry picked from commit 2aaa9d4)
…ular#24522) * test: provide a test component that opens components in a dialog * test: update goldens * test: protected static, remove DI, noop animations * test: add typing * test: make generics optional * test: update goldens
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This makes it trivial for users to open components in a dialog, avoiding the need to mock or inject dialog properties. See tests for how users could apply it