Skip to content

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

Closed
testing-library/user-event
#302
@crutchcorn

Description

@crutchcorn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions