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

Html code block not displayed inside defer block in Angular v17 #28306

Closed
MattiaMalandrone opened this issue Nov 10, 2023 · 8 comments
Closed

Comments

@MattiaMalandrone
Copy link

Current behavior

Trying testing new angular control flow the html inside a defer block is not displayed when running test in component testing inside cypress

describe('AppComponent.cy.ts', () => {
  it('should mount', () => {
    cy.mount(AppComponent)
  })
})

app.component.html

outside defer

@defer() {
  inside defer
}

@defer(on timer(500ms)) {
  inside defer with condition
}

Only string "outside defer" is displayed

Desired behavior

I expect that html code inside a defer block will be displayed

Test code to reproduce

https://github.com/MattiaMalandrone/cypress-defer-block-angular17

launching npx cypress open -> component testing -> chrome -> AppComponent.cy.ts

Cypress Version

13.5.0

Node version

18.18.2

Operating System

windows 10

Debug Logs

No response

Other

No response

@kanidjar
Copy link

Same issue here. Critical bug on my side since i cannot test my components.

@MattiaMalandrone
Copy link
Author

any news about it?

@felixgeissler
Copy link

We are experiencing the same behaviour in our component tests running v13.5.1

@psrebrny
Copy link

I confirm this behaviour in cypress v13.6.1 when can we expect to handle defer in component testing?

@psrebrny
Copy link

psrebrny commented Dec 21, 2023

I have written a workaround for this issue

Service:

import { Injectable, signal, Signal } from '@angular/core';

@Injectable({
    providedIn: 'root',
})
export class DeferrableStateService {
    defer: Signal<boolean> = signal(true);
    constructor() {}
}

Component:

import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { DeferrableStateService } from '../../services/deferrable-state.service';
import { NgTemplateOutlet } from '@angular/common';

@Component({
    selector: 'app-deferrer',
    standalone: true,
    imports: [NgTemplateOutlet],
    templateUrl: './deferrer.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DeferrerComponent {
    isdeferred = this.deferrableStateService.defer;
    constructor(private deferrableStateService: DeferrableStateService) {}
}

and HTML for that component:

<ng-template #content>
    <ng-content></ng-content>
</ng-template>

@if (isdeferred()) { @defer (on viewport) {
<ng-container *ngTemplateOutlet="content"></ng-container>
} @placeholder () {
 // your placeholder
} } @else {
<ng-container *ngTemplateOutlet="content"></ng-container>
}

then in component test add configuration

cy.mount(Component, {
  imports: [
      // YourComponents and/or modules
  ],
  providers: [
      {
          provide: DeferrableStateService,
          useValue: {
              defer: signal(false),
          },
      },
  ],
  declarations: [],
  componentProperties: {
      // your properties
  },
 
});

@jordanpowell88
Copy link
Contributor

Hey @MattiaMalandrone thanks for opening this issue. I went ahead and created an example repo and blog article to highlight how you can do this. https://dev.to/angular/testing-defer-blocks-in-angular-with-cypress-21j8

Hopefully this helps!

@jordanpowell88
Copy link
Contributor

I'm going to close this for now but we may create something more "official" in the future

@MattiaMalandrone
Copy link
Author

Hey @MattiaMalandrone thanks for opening this issue. I went ahead and created an example repo and blog article to highlight how you can do this. https://dev.to/angular/testing-defer-blocks-in-angular-with-cypress-21j8

Hopefully this helps!

Thanks Jordan!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants