question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When using `user.click` in `@testing-library/angular`, an error is thrown

See original GitHub issue

I have the following test written to test a pagination component:

import user from '@testing-library/user-event';

...

    const nextPageBtn = screen.getByLabelText('Go to the next page');
    user.click(nextPageBtn);
    expect(pageIndexChange).toHaveBeenCalledWith(3);

    const previousPageBtn = screen.getByLabelText('Go to the previous page');
    user.click(previousPageBtn);
    expect(pageIndexChange).toHaveBeenCalledWith(2);

    const firstPageBtn = screen.getByLabelText('Go to the first page');
    user.click(firstPageBtn);
    expect(pageIndexChange).toHaveBeenCalledWith(0);

    const lastPageBtn = screen.getByLabelText('Go to the last page');
    user.click(lastPageBtn);
    expect(pageIndexChange).toHaveBeenCalledWith(4);

With one of the buttons being formed like this:

  <button
    type="button"
    class="btn-paginator btn-side ml-1"
    [disabled]="pageIndex === lastPage"
    (click)="gotoPage(lastPage)"
    [attr.aria-label]="'Go to the last page' | translate"
  >
    <i class="fas fa-angle-double-right" aria-hidden="true"></i>
  </button>

(With the translate pipe coming from ngx-translate)

However, when I run the tests, I get the following error.

● PaginatorComponent › should handle paging forward and backward properly

    TypeError: Cannot read property '_defaultView' of undefined

      90 |
      91 |     const previousPageBtn = screen.getByLabelText('Go to the previous page');
    > 92 |     user.click(previousPageBtn);
         |          ^
      93 |     expect(pageIndexChange).toHaveBeenCalledWith(2);
      94 |
      95 |     const firstPageBtn = screen.getByLabelText('Go to the first page');

      at Object.exports.fireFocusEventWithTargetAdjustment (node_modules/jsdom/lib/jsdom/living/helpers/focusing.js:71:33)
      at HTMLButtonElementImpl.blur (node_modules/jsdom/lib/jsdom/living/nodes/HTMLAndSVGElementShared-impl.js:52:14)
      at HTMLButtonElement.blur (node_modules/jsdom/lib/jsdom/living/generated/HTMLElement.js:38:23)
      at clickElement (node_modules/@testing-library/user-event/dist/index.js:71:49)
      at Object.click (node_modules/@testing-library/user-event/dist/index.js:214:9)
      at src/app/@shared/paginator/paginator.component.spec.ts:92:10
      at fulfilled (node_modules/tslib/tslib.js:110:62)
      at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:386:30)
      at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:117:43)

However, if I switch from user.click to fireEvent from @testing-library/angular, I get no such error and the tests pass

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
crutchcorncommented, May 15, 2020

I agree. I don’t see anything that’s implementation specific to user-event here.

I’ll make a issue on jsdom outlining my findings and link it here

0reactions
timdeschryvercommented, Jun 6, 2020

🎉 This issue has been resolved in version 9.3.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

When using user.click in @testing-library/angular , an error is ...
I have the following test written to test a pagination component: import user from '@testing-library/user-event'; ... const nextPageBtn ...
Read more >
Input type of date is not working on testing library Angular ...
im trying to do this test oover an input of type date in angular using Testing Library. But keep receiving this error:
Read more >
Good testing practices with Angular Testing Library
If we don't want Angular Testing Library to throw an error we can use the queryBy and queryAllBy queries instead, these queries return...
Read more >
Good Testing Practices with Angular Testing Library -- newline
If we don't want Angular Testing Library to throw an error we can use the queryBy and queryAllBy queries instead. The error will...
Read more >
Angular Unit Test: Mock Service and Observables — Part 1
If isApiFailed is true (mean its Failed to fetch user data), show error message. Then we have a button with click action. Which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found