Skip to content

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

Merged
merged 6 commits into from
Mar 5, 2022

Conversation

andrewseguin
Copy link
Contributor

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

@andrewseguin andrewseguin requested a review from a team as a code owner March 4, 2022 22:28
@andrewseguin andrewseguin added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent target: patch This PR is targeted for the next patch release labels Mar 4, 2022
Copy link
Member

@devversion devversion left a 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;
Copy link
Member

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?

Copy link
Contributor Author

@andrewseguin andrewseguin Mar 5, 2022

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?

Copy link
Member

@devversion devversion Mar 5, 2022

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 ExampleComponent (T), saving on one type parameter hmm looks like it will always require two params to be specified then.. (microsoft/TypeScript#26242)

Copy link
Member

@devversion devversion left a 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

@andrewseguin
Copy link
Contributor Author

andrewseguin commented Mar 5, 2022

(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>>;
  }

@devversion
Copy link
Member

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 😆

@andrewseguin andrewseguin added the action: merge The PR is ready for merge by the caretaker label Mar 5, 2022
@andrewseguin
Copy link
Contributor Author

Updated to make the generics optional (e.g. T = unknown)

@andrewseguin andrewseguin merged commit 2aaa9d4 into angular:master Mar 5, 2022
andrewseguin added a commit that referenced this pull request Mar 5, 2022
)

* 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)
forsti0506 pushed a commit to forsti0506/components that referenced this pull request Apr 3, 2022
…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
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants