Skip to content

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

Description

@Nosfistis

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

Activity

  1. added
    needs triage
    on Oct 15, 2021
  2. crisbeto commented on Oct 15, 2021

    @crisbeto
    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.

  3. Nosfistis commented on Oct 15, 2021

    @Nosfistis
    Author

    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?

  4. crisbeto commented on Oct 15, 2021

    @crisbeto
    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.

  5. Nosfistis commented on Oct 15, 2021

    @Nosfistis
    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!

  6. crisbeto commented on Oct 16, 2021

    @crisbeto
    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.

  7. added
    P3
    and removed
    needs triage
    on Oct 16, 2021
  8. self-assigned this
    on Oct 16, 2021
  9. added a commit that references this issue on Oct 16, 2021
    ef7d9e0
  10. added 3 commits that reference this issue on Oct 19, 2021
    3cd505f
    59eee5d
    11e591b
  11. added a commit that references this issue on Oct 20, 2021
    5cddadf
  12. added a commit that references this issue on Oct 22, 2021
    c3007ea
  13. added 2 commits that reference this issue on Oct 26, 2021
    09a46af
    d5f5022
  14. angular-automatic-lock-bot commented on Nov 19, 2021

    @angular-automatic-lock-bot

    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.

  15. locked and limited conversation to collaborators on Nov 19, 2021
  16. added 4 commits that reference this issue on Mar 7, 2022
    2f649ef
    ac78558
    c0cbdad
    5fefbd8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area: cdk/testing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions