Skip to content

bug(autocomplete): harness does not fire focusin event #23757

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

Closed
Nosfistis opened this issue Oct 15, 2021 · 6 comments · Fixed by #23768 · May be fixed by #23795
Closed

bug(autocomplete): harness does not fire focusin event #23757

Nosfistis opened this issue Oct 15, 2021 · 6 comments · Fixed by #23768 · May be fixed by #23795
Assignees
Labels
area: cdk/testing P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@Nosfistis
Copy link

Nosfistis commented Oct 15, 2021

Reproduction

Use StackBlitz to reproduce your issue:

Steps to reproduce:

  1. Create an autocomplete test with harness
  2. Focus the autocomplete via MatAutocompleteHarness.focus()

Expected Behavior

The input element is focused and the autocomplete options are rendered. They can also be queried in the DOM (e.g. check that an element with text "Choice A" exists). Dispatching focus via a harness should also dispatch focusin beforehand, so that it emulates properly the browser behavior.

Actual Behavior

The input element is focused, but the options are not open. This is because the autocomplete harness fires the focus event, while mat-autocomplete-trigger listens for focusin events, which are not triggered via the JS focus event.

Environment

  • Angular: 12.2
  • CDK/Material: 12.2
  • Browser(s): Chrome 96
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows
@Nosfistis Nosfistis added the needs triage This issue needs to be triaged by the team label Oct 15, 2021
@crisbeto
Copy link
Member

It seems like focusin does get triggered when you call focus programmatically. See https://stackblitz.com/edit/angular-ivy-btn942?file=src%2Fapp%2Fapp.component.html.

@Nosfistis
Copy link
Author

Nosfistis commented Oct 15, 2021

Indeed you are right. I am struggling to create a proper harness Stackblitz with the proper angular version though, so that Ivy is enabled. I will be back with a better reproduction.

Is it possible that jasmine does not emulate that behavior?

@crisbeto
Copy link
Member

There's a checkbox in the Stackblitz settings to enable Ivy, but I don't think that's the issue since we're basically interacting directly with the browser APIs. We've seen similar issues like yours in some of our tests and they ended up being caused by the browser not moving focus if the browser window is blurred/minimized.

@Nosfistis
Copy link
Author

Great tip! Indeed it was an issue of the browser running in the background. I have leaving the browser in the front and the test passed. I also tried with ChromeHeadless and it also worked!

@crisbeto
Copy link
Member

It looks like we have some fallback logic that simulates the focus and blur events manually if the browser doesn't fire them, but it wasn't accounting for focusin and focusout. I've submitted #23757 to sort it out.

@crisbeto crisbeto reopened this Oct 16, 2021
@crisbeto crisbeto added area: cdk/testing has pr P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent and removed needs triage This issue needs to be triaged by the team labels Oct 16, 2021
@crisbeto crisbeto self-assigned this Oct 16, 2021
crisbeto added a commit to crisbeto/material2 that referenced this issue Oct 16, 2021
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes angular#23757.
andrewseguin pushed a commit that referenced this issue Oct 19, 2021
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes #23757.
andrewseguin pushed a commit that referenced this issue Oct 19, 2021
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes #23757.

(cherry picked from commit 3cd505f)
andrewseguin pushed a commit that referenced this issue Oct 19, 2021
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes #23757.

(cherry picked from commit 3cd505f)
crisbeto added a commit to crisbeto/material2 that referenced this issue Oct 20, 2021
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes angular#23757.
forsti0506 pushed a commit to forsti0506/components that referenced this issue Oct 22, 2021
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes angular#23757.
crisbeto added a commit to crisbeto/material2 that referenced this issue Oct 26, 2021
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes angular#23757.
crisbeto added a commit to crisbeto/material2 that referenced this issue Oct 26, 2021
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes angular#23757.
@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 Nov 19, 2021
crisbeto added a commit to crisbeto/material2 that referenced this issue Mar 7, 2022
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes angular#23757.
crisbeto added a commit to crisbeto/material2 that referenced this issue Mar 8, 2022
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes angular#23757.
crisbeto added a commit to crisbeto/material2 that referenced this issue Mar 8, 2022
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes angular#23757.
crisbeto added a commit to crisbeto/material2 that referenced this issue Mar 8, 2022
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well.

Fixes angular#23757.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: cdk/testing P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
2 participants