Skip to content
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

bug(mat-tab-nav-bar): Having mat-tab-nav-bar on the page breaks harnesses if browser is not visible #23964

Closed
1 task
ckorherr opened this issue Nov 14, 2021 · 6 comments · Fixed by #24000
Closed
1 task
Assignees
Labels
area: cdk/testing P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@ckorherr
Copy link

ckorherr commented Nov 14, 2021

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

I already had the problem with Angular 12, but then couldn't find the reason.

Description

Having mat-tab-nav-bar on the page breaks harnesses if browser is not visible.

Reproduction

Creating a stackblitz for this problem didn't work, I tried to make the problem locally reproducible as easily as possible:

Steps to reproduce:

  1. create a new Angular project and add Angular Material:
ng new tabs-test --routing=false --style=scss
cd tabs-test
ng add @angular/material
  1. add MatTabsModule to app.module.ts

  2. change app.component.html to:

<nav mat-tab-nav-bar>
  <a mat-tab-link>
      Test
  </a>
</nav>
  1. create file: app.component.harness.ts with following content:
import { ComponentHarness } from '@angular/cdk/testing';

export class AppComponentHarness extends ComponentHarness {
  static readonly hostSelector = 'app-root';
}
  1. change the content of app.component.spec.ts to:
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { TestBed } from '@angular/core/testing';
import { MatTabsModule } from '@angular/material/tabs';
import { BrowserModule } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { AppComponentHarness } from './app.component.harness';

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [
        AppComponent,
      ],
      imports: [
        BrowserModule,
        NoopAnimationsModule,
        MatTabsModule
      ],
    }).compileComponents();
  });

  fit('should create the app', async () => {
    const fixture = TestBed.createComponent(AppComponent);
    const harness = await TestbedHarnessEnvironment.harnessForFixture(fixture, AppComponentHarness);
    expect(harness).toBeTruthy();
  });
});

  1. run tests
ng test

By default the browser will pop up and be visible and the test will execute without a problem.

  1. Click refresh on the browser and minimize it or make sure the browser is not visible by hiding it behind another window like vscode

now the test will fail with following error:
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)

Expected Behavior

The tests will succeed even if the browser is not visible.

Actual Behavior

If the browser is not visible the test will fail with following error:
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)

Environment

  • Angular: 13.0.1
  • CDK/Material: 13.0.1
  • Browser(s): Chrome 95.0.4638.69 (64-Bit)
  • Operating System: Windows 10
@ckorherr ckorherr added the needs triage This issue needs to be triaged by the team label Nov 14, 2021
@crisbeto
Copy link
Member

The tab header has a couple of requestAnimationFrame calls which the browser could pause if it's minimized. I wonder if that is causing the issue.

@ckorherr
Copy link
Author

Yes, you might be right, changing the following line will fix the problem:

typeof requestAnimationFrame !== 'undefined' ? requestAnimationFrame(realign) : realign();

to:
realign();

@ckorherr
Copy link
Author

This workaround is working for me without changing the source code:

let requestAnimationFrameBackup: (callback: FrameRequestCallback) => number;

 beforeEach(() => {
   requestAnimationFrameBackup = requestAnimationFrame;
   (globalThis as any).requestAnimationFrame = undefined;
 });
 
 afterEach(() => {
   globalThis.requestAnimationFrame = requestAnimationFrameBackup;
 });

@crisbeto crisbeto added area: cdk/testing needs investigation A member of the team needs to do further investigation to determine the root cause 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 Nov 17, 2021
crisbeto added a commit to crisbeto/material2 that referenced this issue Nov 22, 2021
The tabs have a couple of `requestAnimationFrame` calls when checking for the size/position of elements which can cause tests to hang when they're in a background tab. The problem is that browsers block `requestAnimationFrame` when the browser is out of focus and test harnesses will wait for the call to resolve.

These changes switch to `NgZone.onStable` in an attempt to resolve the issue.

Fixes angular#23964.
@crisbeto
Copy link
Member

crisbeto commented Nov 22, 2021

I've submitted #24000 to try and avoid the timeouts. Can you try swapping out the requestAnimationRef calls with ngZone.onStable like in the PR to confirm that it resolves your issue?

@crisbeto crisbeto removed the needs investigation A member of the team needs to do further investigation to determine the root cause label Nov 22, 2021
crisbeto added a commit to crisbeto/material2 that referenced this issue Nov 22, 2021
The tabs have a couple of `requestAnimationFrame` calls when checking for the size/position of elements which can cause tests to hang when they're in a background tab. The problem is that browsers block `requestAnimationFrame` when the browser is out of focus and test harnesses will wait for the call to resolve.

These changes switch to `NgZone.onStable` in an attempt to resolve the issue.

Fixes angular#23964.
@ckorherr
Copy link
Author

I removed my workaround and applied your changes, the tests work as expected. Yes it resolves the issue.

@crisbeto crisbeto self-assigned this Nov 28, 2021
crisbeto added a commit to crisbeto/material2 that referenced this issue Mar 9, 2022
The tabs have a couple of `requestAnimationFrame` calls when checking for the size/position of elements which can cause tests to hang when they're in a background tab. The problem is that browsers block `requestAnimationFrame` when the browser is out of focus and test harnesses will wait for the call to resolve.

These changes switch to `NgZone.onStable` in an attempt to resolve the issue.

Fixes angular#23964.
crisbeto added a commit that referenced this issue Mar 10, 2022
The tabs have a couple of `requestAnimationFrame` calls when checking for the size/position of elements which can cause tests to hang when they're in a background tab. The problem is that browsers block `requestAnimationFrame` when the browser is out of focus and test harnesses will wait for the call to resolve.

These changes switch to `NgZone.onStable` in an attempt to resolve the issue.

Fixes #23964.
crisbeto added a commit that referenced this issue Mar 10, 2022
The tabs have a couple of `requestAnimationFrame` calls when checking for the size/position of elements which can cause tests to hang when they're in a background tab. The problem is that browsers block `requestAnimationFrame` when the browser is out of focus and test harnesses will wait for the call to resolve.

These changes switch to `NgZone.onStable` in an attempt to resolve the issue.

Fixes #23964.

(cherry picked from commit 9e06e4a)
forsti0506 pushed a commit to forsti0506/components that referenced this issue Apr 3, 2022
The tabs have a couple of `requestAnimationFrame` calls when checking for the size/position of elements which can cause tests to hang when they're in a background tab. The problem is that browsers block `requestAnimationFrame` when the browser is out of focus and test harnesses will wait for the call to resolve.

These changes switch to `NgZone.onStable` in an attempt to resolve the issue.

Fixes angular#23964.
@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 10, 2022
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
Development

Successfully merging a pull request may close this issue.

2 participants